Class ChannelTransferSupport

java.lang.Object
com.e1c.g5.v8.internal.fastzip.ChannelTransferSupport

public class ChannelTransferSupport extends Object
Enables optimized data transfer from one byte channel to another.

If a thread-local buffer is enabled for calling thread with enableThreadLocalBufferForCurrentThread(), transfer(ReadableByteChannel, WritableByteChannel, long) will copy data using a single lazily allocated thread-local direct byte buffer. Otherwise the implementation fallbacks to less efficient procedures.

Implementations of FileChannel.transferFrom(ReadableByteChannel, long, long) and FileChannel.transferTo(long, long, WritableByteChannel) at OpenJDK 11 allocate a new indirect byte buffer on each invocation if they are called with a custom "untrusted" channel. The idea of this class is to optimize bulk data transfer operations by using reasonably large direct byte buffers and avoiding multiple allocations.

Currently thread-local buffers are enabled only for short-living worker threads which perform compression and decompression.

  • Constructor Details

    • ChannelTransferSupport

      public ChannelTransferSupport()
  • Method Details

    • transfer

      public static long transfer(ReadableByteChannel src, WritableByteChannel dst, long size) throws IOException
      Transfers bytes from the source byte channel to the target byte channel.

      An attempt is made to read up to count bytes from the source channel and write them to the target channel. If the source channel contains fewer than count bytes, that many bytes will be transferred.

      If any of the given channels has a position, bytes will be read or written starting at that position. An invocation of this method may or may not modify the channel's position. Thus the channel's position remains unspecified after invocation of this method. If the source or target channel has a position and it is modified concurrently with an invocation of this method, the behaviour is unspecified. If the channel's current position is greater than it's size (which is possible e.g. for FileChannel), the behaviour is unspecified.

      If the source or target channel is non-blocking, an invocation of this method may spin.

      If both channels are known to be standard library channels and one of them is known to be a file channel, prefer using FileChannel.transferFrom(ReadableByteChannel, long, long) or FileChannel.transferTo(long, long, WritableByteChannel) directly. Also avoid using this method if any of the given channels is not optimized for direct byte buffers.

      Parameters:
      src - the source channel, cannot be null
      dst - the target channel, cannot be null
      size - the maximum number of bytes to be transferred, must be non-negative
      Returns:
      the number of bytes, possibly zero, that were actually transferred
      Throws:
      IOException - if an IO error occurs