Class ChannelUtil

java.lang.Object
com.pnfsoftware.jeb.util.io.ChannelUtil

public class ChannelUtil extends Object
Utility methods for SeekableByteChannels.
  • Method Details

    • read

      public static ByteBuffer read(SeekableByteChannel channel, long position, int size, boolean isBigEndian) throws IOException
      Read exactly size bytes from a channel at an absolute position.
      Parameters:
      channel - input channel
      position - absolute channel position
      size - number of bytes to read
      isBigEndian - true to return a big-endian buffer, false for little-endian
      Returns:
      byte buffer containing the requested bytes
      Throws:
      IOException - if the requested bytes cannot be fully read
    • read

      public static ByteBuffer read(SeekableByteChannel channel, long position, int size, boolean isBigEndian, ByteBuffer buffer) throws IOException
      Read exactly size bytes from a channel at an absolute position.
      Parameters:
      channel - input channel
      position - absolute channel position
      size - number of bytes to read
      isBigEndian - true to return a big-endian buffer, false for little-endian
      buffer - optional reusable destination buffer
      Returns:
      byte buffer containing the requested bytes
      Throws:
      IOException - if the requested bytes cannot be fully read
    • readBestEffort

      public static ByteBuffer readBestEffort(SeekableByteChannel channel, long position, int size, boolean isBigEndian, ByteBuffer buffer) throws IOException
      Read up to size bytes from a channel at an absolute position.
      Parameters:
      channel - input channel
      position - absolute channel position
      size - maximum number of bytes to read
      isBigEndian - true to return a big-endian buffer, false for little-endian
      buffer - optional reusable destination buffer
      Returns:
      byte buffer containing all bytes that could be read
      Throws:
      IOException - if the channel cannot be read
    • getAllFrom

      public static byte[] getAllFrom(SeekableByteChannel channel, long position) throws IOException
      Read all remaining bytes from a channel starting at an absolute position.
      Parameters:
      channel - input channel
      position - absolute start position
      Returns:
      remaining bytes
      Throws:
      IOException - if the bytes cannot be read or exceed 2 GiB
    • get

      public static byte get(SeekableByteChannel channel, long position) throws IOException
      Read one byte at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      Returns:
      byte value
      Throws:
      IOException - if the byte cannot be read
    • get

      public static byte get(SeekableByteChannel channel) throws IOException
      Read one byte at the current channel position.
      Parameters:
      channel - input channel
      Returns:
      byte value
      Throws:
      IOException - if the byte cannot be read
    • getBytes

      public static byte[] getBytes(SeekableByteChannel channel, long position, int size) throws IOException
      Read bytes at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      size - number of bytes to read
      Returns:
      byte array
      Throws:
      IOException - if the requested bytes cannot be fully read
    • getBytes

      public static byte[] getBytes(SeekableByteChannel channel, int size) throws IOException
      Read bytes at the current channel position.
      Parameters:
      channel - input channel
      size - number of bytes to read
      Returns:
      byte array
      Throws:
      IOException - if the requested bytes cannot be fully read
    • getShort

      public static short getShort(SeekableByteChannel channel, long position, ByteOrder endianness) throws IOException
      Read a 16-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      endianness - byte order
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getInt

      public static int getInt(SeekableByteChannel channel, long position, ByteOrder endianness) throws IOException
      Read a 32-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      endianness - byte order
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getLong

      public static long getLong(SeekableByteChannel channel, long position, ByteOrder endianness) throws IOException
      Read a 64-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      endianness - byte order
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getShort

      public static short getShort(SeekableByteChannel channel, ByteOrder endianness) throws IOException
      Read a 16-bit integer at the current channel position.
      Parameters:
      channel - input channel
      endianness - byte order
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getInt

      public static int getInt(SeekableByteChannel channel, ByteOrder endianness) throws IOException
      Read a 32-bit integer at the current channel position.
      Parameters:
      channel - input channel
      endianness - byte order
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getLong

      public static long getLong(SeekableByteChannel channel, ByteOrder endianness) throws IOException
      Read a 64-bit integer at the current channel position.
      Parameters:
      channel - input channel
      endianness - byte order
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getLEShort

      public static short getLEShort(SeekableByteChannel channel, long position) throws IOException
      Read a little-endian 16-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getLEInt

      public static int getLEInt(SeekableByteChannel channel, long position) throws IOException
      Read a little-endian 32-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getLELong

      public static long getLELong(SeekableByteChannel channel, long position) throws IOException
      Read a little-endian 64-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getBEShort

      public static short getBEShort(SeekableByteChannel channel, long position) throws IOException
      Read a big-endian 16-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getBEInt

      public static int getBEInt(SeekableByteChannel channel, long position) throws IOException
      Read a big-endian 32-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getBELong

      public static long getBELong(SeekableByteChannel channel, long position) throws IOException
      Read a big-endian 64-bit integer at an absolute channel position.
      Parameters:
      channel - input channel
      position - absolute channel position
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getLEShort

      public static short getLEShort(SeekableByteChannel channel) throws IOException
      Read a little-endian 16-bit integer at the current channel position.
      Parameters:
      channel - input channel
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getLEInt

      public static int getLEInt(SeekableByteChannel channel) throws IOException
      Read a little-endian 32-bit integer at the current channel position.
      Parameters:
      channel - input channel
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getLELong

      public static long getLELong(SeekableByteChannel channel) throws IOException
      Read a little-endian 64-bit integer at the current channel position.
      Parameters:
      channel - input channel
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getBEShort

      public static short getBEShort(SeekableByteChannel channel) throws IOException
      Read a big-endian 16-bit integer at the current channel position.
      Parameters:
      channel - input channel
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getBEInt

      public static int getBEInt(SeekableByteChannel channel) throws IOException
      Read a big-endian 32-bit integer at the current channel position.
      Parameters:
      channel - input channel
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getBELong

      public static long getBELong(SeekableByteChannel channel) throws IOException
      Read a big-endian 64-bit integer at the current channel position.
      Parameters:
      channel - input channel
      Returns:
      decoded value
      Throws:
      IOException - if the value cannot be read
    • getBytesUntil

      public static byte[] getBytesUntil(SeekableByteChannel channel, byte stopper, int maxsize, boolean throwOnEOS) throws IOException
      Read from the channel until the provided byte stopped is encountered.
      Parameters:
      channel - channel
      stopper - stopper byte
      maxsize - maximum allowed size to read
      throwOnEOS - instruct the method will throw an IOException if the end of input is reached
      Returns:
      the resulting array, whose last character may be the byte stopper (unless it was not encountered or the maximum allowed size was read); the channel's position will be on the subsequent byte (or EOS)
      Throws:
      IOException - if the bytes cannot be read