// 
// Decompiled by Procyon v0.6.0
// 

package io.netty.handler.stream;

import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.ObjectUtil;
import java.io.IOException;
import java.io.File;
import java.io.RandomAccessFile;
import io.netty.buffer.ByteBuf;

public class ChunkedFile implements ChunkedInput<ByteBuf>
{
    private final RandomAccessFile file;
    private final long startOffset;
    private final long endOffset;
    private final int chunkSize;
    private long offset;
    
    public ChunkedFile(final File file) throws IOException {
        this(file, 8192);
    }
    
    public ChunkedFile(final File file, final int chunkSize) throws IOException {
        this(new RandomAccessFile(file, "r"), chunkSize);
    }
    
    public ChunkedFile(final RandomAccessFile file) throws IOException {
        this(file, 8192);
    }
    
    public ChunkedFile(final RandomAccessFile file, final int chunkSize) throws IOException {
        this(file, 0L, file.length(), chunkSize);
    }
    
    public ChunkedFile(final RandomAccessFile file, final long offset, final long length, final int chunkSize) throws IOException {
        ObjectUtil.checkNotNull(file, "file");
        ObjectUtil.checkPositiveOrZero(offset, "offset");
        ObjectUtil.checkPositiveOrZero(length, "length");
        ObjectUtil.checkPositive(chunkSize, "chunkSize");
        this.file = file;
        this.startOffset = offset;
        this.offset = offset;
        this.endOffset = offset + length;
        this.chunkSize = chunkSize;
        file.seek(offset);
    }
    
    public long startOffset() {
        return this.startOffset;
    }
    
    public long endOffset() {
        return this.endOffset;
    }
    
    public long currentOffset() {
        return this.offset;
    }
    
    @Override
    public boolean isEndOfInput() throws Exception {
        return this.offset >= this.endOffset || !this.file.getChannel().isOpen();
    }
    
    @Override
    public void close() throws Exception {
        this.file.close();
    }
    
    @Deprecated
    @Override
    public ByteBuf readChunk(final ChannelHandlerContext ctx) throws Exception {
        return this.readChunk(ctx.alloc());
    }
    
    @Override
    public ByteBuf readChunk(final ByteBufAllocator allocator) throws Exception {
        final long offset = this.offset;
        if (offset >= this.endOffset) {
            return null;
        }
        final int chunkSize = (int)Math.min(this.chunkSize, this.endOffset - offset);
        final ByteBuf buf = allocator.heapBuffer(chunkSize);
        boolean release = true;
        try {
            this.file.readFully(buf.array(), buf.arrayOffset(), chunkSize);
            buf.writerIndex(chunkSize);
            this.offset = offset + chunkSize;
            release = false;
            return buf;
        }
        finally {
            if (release) {
                buf.release();
            }
        }
    }
    
    @Override
    public long length() {
        return this.endOffset - this.startOffset;
    }
    
    @Override
    public long progress() {
        return this.offset - this.startOffset;
    }
}
