Class ChunkedNioFile

java.lang.Object
io.netty.handler.stream.ChunkedNioFile
All Implemented Interfaces:
ChunkedInput<ByteBuf>

public class ChunkedNioFile extends java.lang.Object implements ChunkedInput<ByteBuf>
A ChunkedInput that fetches data from a file chunk by chunk using NIO FileChannel.

If your operating system supports zero-copy file transfer such as sendfile(), you might want to use FileRegion instead.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
     
    private final long
     
    private final java.nio.channels.FileChannel
     
    private long
     
    private final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    ChunkedNioFile(java.io.File in)
    Creates a new instance that fetches data from the specified file.
    ChunkedNioFile(java.io.File in, int chunkSize)
    Creates a new instance that fetches data from the specified file.
    ChunkedNioFile(java.nio.channels.FileChannel in)
    Creates a new instance that fetches data from the specified file.
    ChunkedNioFile(java.nio.channels.FileChannel in, int chunkSize)
    Creates a new instance that fetches data from the specified file.
    ChunkedNioFile(java.nio.channels.FileChannel in, long offset, long length, int chunkSize)
    Creates a new instance that fetches data from the specified file.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Releases the resources associated with the input.
    long
    Returns the offset in the file where the transfer is happening currently.
    long
    Returns the offset in the file where the transfer will end.
    boolean
    Return true if and only if there is no data left in the stream and the stream has reached at its end.
    long
    Returns the length of the input.
    long
    Returns current transfer progress.
    Fetches a chunked data from the stream.
    Deprecated.
    long
    Returns the offset in the file where the transfer began.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • in

      private final java.nio.channels.FileChannel in
    • startOffset

      private final long startOffset
    • endOffset

      private final long endOffset
    • chunkSize

      private final int chunkSize
    • offset

      private long offset
  • Constructor Details

    • ChunkedNioFile

      public ChunkedNioFile(java.io.File in) throws java.io.IOException
      Creates a new instance that fetches data from the specified file.
      Throws:
      java.io.IOException
    • ChunkedNioFile

      public ChunkedNioFile(java.io.File in, int chunkSize) throws java.io.IOException
      Creates a new instance that fetches data from the specified file.
      Parameters:
      chunkSize - the number of bytes to fetch on each readChunk(ChannelHandlerContext) call
      Throws:
      java.io.IOException
    • ChunkedNioFile

      public ChunkedNioFile(java.nio.channels.FileChannel in) throws java.io.IOException
      Creates a new instance that fetches data from the specified file.
      Throws:
      java.io.IOException
    • ChunkedNioFile

      public ChunkedNioFile(java.nio.channels.FileChannel in, int chunkSize) throws java.io.IOException
      Creates a new instance that fetches data from the specified file.
      Parameters:
      chunkSize - the number of bytes to fetch on each readChunk(ChannelHandlerContext) call
      Throws:
      java.io.IOException
    • ChunkedNioFile

      public ChunkedNioFile(java.nio.channels.FileChannel in, long offset, long length, int chunkSize) throws java.io.IOException
      Creates a new instance that fetches data from the specified file.
      Parameters:
      offset - the offset of the file where the transfer begins
      length - the number of bytes to transfer
      chunkSize - the number of bytes to fetch on each readChunk(ChannelHandlerContext) call
      Throws:
      java.io.IOException
  • Method Details

    • startOffset

      public long startOffset()
      Returns the offset in the file where the transfer began.
    • endOffset

      public long endOffset()
      Returns the offset in the file where the transfer will end.
    • currentOffset

      public long currentOffset()
      Returns the offset in the file where the transfer is happening currently.
    • isEndOfInput

      public boolean isEndOfInput() throws java.lang.Exception
      Description copied from interface: ChunkedInput
      Return true if and only if there is no data left in the stream and the stream has reached at its end.
      Specified by:
      isEndOfInput in interface ChunkedInput<ByteBuf>
      Throws:
      java.lang.Exception
    • close

      public void close() throws java.lang.Exception
      Description copied from interface: ChunkedInput
      Releases the resources associated with the input.
      Specified by:
      close in interface ChunkedInput<ByteBuf>
      Throws:
      java.lang.Exception
    • readChunk

      @Deprecated public ByteBuf readChunk(ChannelHandlerContext ctx) throws java.lang.Exception
      Deprecated.
      Specified by:
      readChunk in interface ChunkedInput<ByteBuf>
      Parameters:
      ctx - The context which provides a ByteBufAllocator if buffer allocation is necessary.
      Returns:
      the fetched chunk. null if there is no data left in the stream. Please note that null does not necessarily mean that the stream has reached at its end. In a slow stream, the next chunk might be unavailable just momentarily.
      Throws:
      java.lang.Exception
    • readChunk

      public ByteBuf readChunk(ByteBufAllocator allocator) throws java.lang.Exception
      Description copied from interface: ChunkedInput
      Fetches a chunked data from the stream. Once this method returns the last chunk and thus the stream has reached at its end, any subsequent ChunkedInput.isEndOfInput() call must return true.
      Specified by:
      readChunk in interface ChunkedInput<ByteBuf>
      Parameters:
      allocator - ByteBufAllocator if buffer allocation is necessary.
      Returns:
      the fetched chunk. null if there is no data left in the stream. Please note that null does not necessarily mean that the stream has reached at its end. In a slow stream, the next chunk might be unavailable just momentarily.
      Throws:
      java.lang.Exception
    • length

      public long length()
      Description copied from interface: ChunkedInput
      Returns the length of the input.
      Specified by:
      length in interface ChunkedInput<ByteBuf>
      Returns:
      the length of the input if the length of the input is known. a negative value if the length of the input is unknown.
    • progress

      public long progress()
      Description copied from interface: ChunkedInput
      Returns current transfer progress.
      Specified by:
      progress in interface ChunkedInput<ByteBuf>