/*
 * Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package org.asynchttpclient.generators;

import org.asynchttpclient.BodyGenerator;
import org.asynchttpclient.RandomAccessBody;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * Creates a request body from the contents of a file.
 */
//Not used by Netty
public class FileBodyGenerator implements BodyGenerator {

    private final File file;
    private final long regionSeek;
    private final long regionLength;

    public FileBodyGenerator(File file) {
        this(file, 0L, file.length());
    }

    public FileBodyGenerator(File file, long regionSeek, long regionLength) {
        if (file == null) {
            throw new NullPointerException("file");
        }
        this.file = file;
        this.regionLength = regionLength;
        this.regionSeek = regionSeek;
    }

    public File getFile() {
        return file;
    }

    public long getRegionLength() {
        return regionLength;
    }

    public long getRegionSeek() {
        return regionSeek;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RandomAccessBody createBody() throws IOException {
        return new FileBody(file, regionSeek, regionLength);
    }

    private static class FileBody implements RandomAccessBody {

        private final RandomAccessFile raf;

        private final FileChannel channel;

        private final long length;

        private FileBody(File file, long regionSeek, long regionLength) throws IOException {
            raf = new RandomAccessFile(file, "r");
            channel = raf.getChannel();
            length = regionLength;
            if (regionSeek > 0) {
                raf.seek(regionSeek);
            }
        }

        public long getContentLength() {
            return length;
        }

        public long read(ByteBuffer buffer) throws IOException {
            return channel.read(buffer);
        }

        public long transferTo(long position, WritableByteChannel target) throws IOException {
            return channel.transferTo(position, length, target);
        }

        public void close() throws IOException {
            raf.close();
        }
    }
}

@@ -734,96 +734,8 @@
 s;%0A%0A
-import org.asynchttpclient.BodyGenerator;%0Aimport org.asynchttpclient.RandomAccessBody;%0A%0A
 impo
@@ -783,151 +783,94 @@
 on;%0A
+%0A
 import 
-java.io.RandomAccessFile;%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.channels.FileChannel;%0Aimport java.nio.channels.WritableByteChannel
+org.asynchttpclient.BodyGenerator;%0Aimport org.asynchttpclient.RandomAccessBody
 ;%0A%0A/
@@ -935,34 +935,20 @@
  */%0A
-//Not used by Netty%0A
 public
+ final
  cla
@@ -1788,1024 +1788,121 @@
     
-return new FileBody(file, regionSeek, regionLength);%0A    %7D%0A%0A    private static class FileBody implements RandomAccessBody %7B%0A%0A        private final RandomAccessFile raf;%0A%0A        private final FileChannel channel;%0A%0A        private final long length;%0A%0A        private FileBody(File file, long regionSeek, long regionLength) throws IOException %7B%0A            raf = new RandomAccessFile(file, %22r%22);%0A            channel = raf.getChannel();%0A            length = regionLength;%0A            if (regionSeek %3E 0) %7B%0A                raf.seek(regionSeek);%0A            %7D%0A        %7D%0A%0A        public long getContentLength() %7B%0A            return length;%0A        %7D%0A%0A        public long read(ByteBuffer buffer) throws IOException %7B%0A            return channel.read(buffer);%0A        %7D%0A%0A        public long transferTo(long position, WritableByteChannel target) throws IOException %7B%0A            return channel.transferTo(position, length, target);%0A        %7D%0A%0A        public void close() throws IOException %7B%0A            raf.close();%0A        %7D
+throw new UnsupportedOperationException(%22FileBodyGenerator.createBody isn't used, Netty direclt sends the file%22);
 %0A   

Make FileBodyGenerator final and remove unused stuff, see #896