Package org.jppf.io

Class ChannelInputSource

java.lang.Object
org.jppf.io.ChannelInputSource
All Implemented Interfaces:
Closeable, AutoCloseable, InputSource, IO
Direct Known Subclasses:
FileInputSource

public class ChannelInputSource extends Object implements InputSource
Input source backed by a ReadableByteChannel.
Author:
Laurent Cohen
  • Field Details

  • Constructor Details

    • ChannelInputSource

      public ChannelInputSource(ReadableByteChannel channel)
      Initialize this input source with the specified SocketWrapper.
      Parameters:
      channel - the backing SocketWrapper.
  • Method Details

    • read

      public int read(byte[] data, int offset, int len) throws Exception
      Read data from this input source into an array of bytes.
      Specified by:
      read in interface InputSource
      Parameters:
      data - the buffer into which to write.
      offset - the position in the buffer where to start storing the data.
      len - the size in bytes of the data to read.
      Returns:
      the number of bytes actually read, or -1 if end of stream was reached.
      Throws:
      Exception - if an IO error occurs.
    • read

      public int read(ByteBuffer data) throws Exception
      Read data from this input source into a byte buffer.

      Implementation details:
      We read the data by small chunks of max IO.TEMP_BUFFER_SIZE bytes wrapped in a direct ByteBuffer, to work around the fact that Sun NIO implementation of SocketChannelImpl.read() attempts to allocate a direct buffer of the requested data size (i.e. data.remaining() in our case), if the destination ByteBuffer is not direct.
      This implementation can result in a "OutOfMemoryError: Direct buffer space" when the size of the data to read is too large.
      See Sun Bug ID: 4879883 for details.

      Specified by:
      read in interface InputSource
      Parameters:
      data - the buffer into which to write.
      Returns:
      the number of bytes actually read, or -1 if end of stream was reached.
      Throws:
      Exception - if an IO error occurs.
    • readInt

      public int readInt() throws Exception
      Read an int value from this input source.
      Specified by:
      readInt in interface InputSource
      Returns:
      the value read, or -1 if an end of file condition was reached.
      Throws:
      Exception - if an IO error occurs.
    • skip

      public int skip(int n) throws Exception
      Skip n bytes of data form this input source.
      Specified by:
      skip in interface InputSource
      Parameters:
      n - the number of bytes to skip.
      Returns:
      the number of bytes actually skipped.
      Throws:
      Exception - if an IO error occurs.
    • close

      public void close() throws IOException
      This method does nothing.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an IO error occurs.
    • toString

      public String toString()
      Overrides:
      toString in class Object