Class DefaultFileRegion

java.lang.Object
io.netty.util.AbstractReferenceCounted
io.netty.channel.DefaultFileRegion
All Implemented Interfaces:
FileRegion, ReferenceCounted

public class DefaultFileRegion extends AbstractReferenceCounted implements FileRegion
Default FileRegion implementation which transfer data from a FileChannel or File. Be aware that the FileChannel will be automatically closed once AbstractReferenceCounted.refCnt() returns 0.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final long
     
    private final java.io.File
     
    private java.nio.channels.FileChannel
     
    private static final InternalLogger
     
    private final long
     
    private long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultFileRegion(java.io.File file, long position, long count)
    Create a new instance using the given File.
    DefaultFileRegion(java.nio.channels.FileChannel fileChannel, long position, long count)
    Create a new instance
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the number of bytes to transfer.
    protected void
    Called once AbstractReferenceCounted.refCnt() is equals 0.
    boolean
    Returns true if the FileRegion has a open file-descriptor
    void
    Explicitly open the underlying file-descriptor if not done yet.
    long
    Returns the offset in the file where the transfer began.
    Increases the reference count by 1.
    retain(int increment)
    Increases the reference count by the specified increment.
    Records the current access location of this object for debugging purposes.
    touch(java.lang.Object hint)
    Records the current access location of this object with an additional arbitrary information for debugging purposes.
    long
    Deprecated.
    long
    Returns the bytes which was transferred already.
    long
    transferTo(java.nio.channels.WritableByteChannel target, long position)
    Transfers the content of this file region to the specified channel.
    (package private) static void
    validate(DefaultFileRegion region, long position)
     

    Methods inherited from class AbstractReferenceCounted

    refCnt, release, release, setRefCnt

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface ReferenceCounted

    refCnt, release, release
  • Field Details

    • logger

      private static final InternalLogger logger
    • f

      private final java.io.File f
    • position

      private final long position
    • count

      private final long count
    • transferred

      private long transferred
    • file

      private java.nio.channels.FileChannel file
  • Constructor Details

    • DefaultFileRegion

      public DefaultFileRegion(java.nio.channels.FileChannel fileChannel, long position, long count)
      Create a new instance
      Parameters:
      fileChannel - the FileChannel which should be transferred
      position - the position from which the transfer should start
      count - the number of bytes to transfer
    • DefaultFileRegion

      public DefaultFileRegion(java.io.File file, long position, long count)
      Create a new instance using the given File. The File will be opened lazily or explicitly via open().
      Parameters:
      file - the File which should be transferred
      position - the position from which the transfer should start
      count - the number of bytes to transfer
  • Method Details

    • isOpen

      public boolean isOpen()
      Returns true if the FileRegion has a open file-descriptor
    • open

      public void open() throws java.io.IOException
      Explicitly open the underlying file-descriptor if not done yet.
      Throws:
      java.io.IOException
    • position

      public long position()
      Description copied from interface: FileRegion
      Returns the offset in the file where the transfer began.
      Specified by:
      position in interface FileRegion
    • count

      public long count()
      Description copied from interface: FileRegion
      Returns the number of bytes to transfer.
      Specified by:
      count in interface FileRegion
    • transfered

      @Deprecated public long transfered()
      Deprecated.
      Description copied from interface: FileRegion
      Returns the bytes which was transferred already.
      Specified by:
      transfered in interface FileRegion
    • transferred

      public long transferred()
      Description copied from interface: FileRegion
      Returns the bytes which was transferred already.

      Note: some asynchronous transports (such as the io_uring transport when falling back to a chunked send for non-DefaultFileRegion implementations) advance this counter when bytes have been queued for submission, which may be before they reach the peer. If the channel is closed or the write fails after queuing, the reported value may overstate the number of bytes actually delivered.

      Specified by:
      transferred in interface FileRegion
    • transferTo

      public long transferTo(java.nio.channels.WritableByteChannel target, long position) throws java.io.IOException
      Description copied from interface: FileRegion
      Transfers the content of this file region to the specified channel.
      Specified by:
      transferTo in interface FileRegion
      Parameters:
      target - the destination of the transfer
      position - the relative offset of the file where the transfer begins from. For example, 0 will make the transfer start from FileRegion.position()th byte and FileRegion.count() - 1 will make the last byte of the region transferred.
      Throws:
      java.io.IOException
    • deallocate

      protected void deallocate()
      Description copied from class: AbstractReferenceCounted
      Called once AbstractReferenceCounted.refCnt() is equals 0.
      Specified by:
      deallocate in class AbstractReferenceCounted
    • retain

      public FileRegion retain()
      Description copied from interface: ReferenceCounted
      Increases the reference count by 1.
      Specified by:
      retain in interface FileRegion
      Specified by:
      retain in interface ReferenceCounted
      Overrides:
      retain in class AbstractReferenceCounted
    • retain

      public FileRegion retain(int increment)
      Description copied from interface: ReferenceCounted
      Increases the reference count by the specified increment.
      Specified by:
      retain in interface FileRegion
      Specified by:
      retain in interface ReferenceCounted
      Overrides:
      retain in class AbstractReferenceCounted
    • touch

      public FileRegion touch()
      Description copied from interface: ReferenceCounted
      Records the current access location of this object for debugging purposes. If this object is determined to be leaked, the information recorded by this operation will be provided to you via ResourceLeakDetector. This method is a shortcut to touch(null).
      Specified by:
      touch in interface FileRegion
      Specified by:
      touch in interface ReferenceCounted
      Overrides:
      touch in class AbstractReferenceCounted
    • touch

      public FileRegion touch(java.lang.Object hint)
      Description copied from interface: ReferenceCounted
      Records the current access location of this object with an additional arbitrary information for debugging purposes. If this object is determined to be leaked, the information recorded by this operation will be provided to you via ResourceLeakDetector.
      Specified by:
      touch in interface FileRegion
      Specified by:
      touch in interface ReferenceCounted
    • validate

      static void validate(DefaultFileRegion region, long position) throws java.io.IOException
      Throws:
      java.io.IOException