Class RandomAccessFileChannelReader

java.lang.Object
nonapi.io.github.classgraph.fileslice.reader.RandomAccessFileChannelReader
All Implemented Interfaces:
RandomAccessReader

public class RandomAccessFileChannelReader extends Object implements RandomAccessReader
RandomAccessReader for a File. Reads in little endian order, as required by the zipfile format.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final FileChannel
    The file channel.
    private ByteBuffer
    The reusable byte buffer.
    private final byte[]
    The scratch arr.
    private final ByteBuffer
    The scratch byte buf.
    private final long
    The slice length.
    private final long
    The slice start pos.
    private byte[]
    The utf 8 bytes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    RandomAccessFileChannelReader(FileChannel fileChannel, long sliceStartPos, long sliceLength)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    read(long srcOffset, byte[] dstArr, int dstArrStart, int numBytes)
    Read bytes into a byte array.
    int
    read(long srcOffset, ByteBuffer dstBuf, int dstBufStart, int numBytes)
    Read bytes into a ByteBuffer.
    byte
    readByte(long offset)
    Read a byte at a specific offset (without changing the current cursor offset).
    int
    readInt(long offset)
    Read a int at a specific offset (without changing the current cursor offset).
    long
    readLong(long offset)
    Read a long at a specific offset (without changing the current cursor offset).
    short
    readShort(long offset)
    Read a short at a specific offset (without changing the current cursor offset).
    readString(long offset, int numBytes)
    Reads the "modified UTF8" format defined in the Java classfile spec.
    readString(long offset, int numBytes, boolean replaceSlashWithDot, boolean stripLSemicolon)
    Reads the "modified UTF8" format defined in the Java classfile spec, optionally replacing '/' with '.', and optionally removing the prefix "L" and the suffix ";".
    int
    readUnsignedByte(long offset)
    Read an unsigned byte at a specific offset (without changing the current cursor offset).
    long
    readUnsignedInt(long offset)
    Read a unsigned int at a specific offset (without changing the current cursor offset).
    int
    readUnsignedShort(long offset)
    Read a unsigned short at a specific offset (without changing the current cursor offset).

    Methods inherited from class java.lang.Object

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

    • fileChannel

      private final FileChannel fileChannel
      The file channel.
    • sliceStartPos

      private final long sliceStartPos
      The slice start pos.
    • sliceLength

      private final long sliceLength
      The slice length.
    • reusableByteBuffer

      private ByteBuffer reusableByteBuffer
      The reusable byte buffer.
    • scratchArr

      private final byte[] scratchArr
      The scratch arr.
    • scratchByteBuf

      private final ByteBuffer scratchByteBuf
      The scratch byte buf.
    • utf8Bytes

      private byte[] utf8Bytes
      The utf 8 bytes.
  • Constructor Details

    • RandomAccessFileChannelReader

      public RandomAccessFileChannelReader(FileChannel fileChannel, long sliceStartPos, long sliceLength)
      Constructor.
      Parameters:
      fileChannel - the file channel
      sliceStartPos - the slice start pos
      sliceLength - the slice length
  • Method Details

    • read

      public int read(long srcOffset, ByteBuffer dstBuf, int dstBufStart, int numBytes) throws IOException
      Description copied from interface: RandomAccessReader
      Read bytes into a ByteBuffer.
      Specified by:
      read in interface RandomAccessReader
      Parameters:
      srcOffset - The offset to start reading from.
      dstBuf - The ByteBuffer to write into.
      dstBufStart - The offset within the destination buffer to start writing at.
      numBytes - The number of bytes to read.
      Returns:
      The number of bytes actually read, or -1 if no more bytes could be read.
      Throws:
      IOException - If there was an exception while reading.
    • read

      public int read(long srcOffset, byte[] dstArr, int dstArrStart, int numBytes) throws IOException
      Description copied from interface: RandomAccessReader
      Read bytes into a byte array.
      Specified by:
      read in interface RandomAccessReader
      Parameters:
      srcOffset - The offset to start reading from.
      dstArr - The byte array to write into.
      dstArrStart - The offset within the destination array to start writing at.
      numBytes - The number of bytes to read.
      Returns:
      The number of bytes actually read, or -1 if no more bytes could be read.
      Throws:
      IOException - If there was an exception while reading.
    • readByte

      public byte readByte(long offset) throws IOException
      Description copied from interface: RandomAccessReader
      Read a byte at a specific offset (without changing the current cursor offset).
      Specified by:
      readByte in interface RandomAccessReader
      Parameters:
      offset - The buffer offset to read from.
      Returns:
      The byte at the offset.
      Throws:
      IOException - If there was an exception while reading.
    • readUnsignedByte

      public int readUnsignedByte(long offset) throws IOException
      Description copied from interface: RandomAccessReader
      Read an unsigned byte at a specific offset (without changing the current cursor offset).
      Specified by:
      readUnsignedByte in interface RandomAccessReader
      Parameters:
      offset - The buffer offset to read from.
      Returns:
      The unsigned byte at the offset.
      Throws:
      IOException - If there was an exception while reading.
    • readShort

      public short readShort(long offset) throws IOException
      Description copied from interface: RandomAccessReader
      Read a short at a specific offset (without changing the current cursor offset).
      Specified by:
      readShort in interface RandomAccessReader
      Parameters:
      offset - The buffer offset to read from.
      Returns:
      The short at the offset.
      Throws:
      IOException - If there was an exception while reading.
    • readUnsignedShort

      public int readUnsignedShort(long offset) throws IOException
      Description copied from interface: RandomAccessReader
      Read a unsigned short at a specific offset (without changing the current cursor offset).
      Specified by:
      readUnsignedShort in interface RandomAccessReader
      Parameters:
      offset - The buffer offset to read from.
      Returns:
      The unsigned short at the offset.
      Throws:
      IOException - If there was an exception while reading.
    • readInt

      public int readInt(long offset) throws IOException
      Description copied from interface: RandomAccessReader
      Read a int at a specific offset (without changing the current cursor offset).
      Specified by:
      readInt in interface RandomAccessReader
      Parameters:
      offset - The buffer offset to read from.
      Returns:
      The int at the offset.
      Throws:
      IOException - If there was an exception while reading.
    • readUnsignedInt

      public long readUnsignedInt(long offset) throws IOException
      Description copied from interface: RandomAccessReader
      Read a unsigned int at a specific offset (without changing the current cursor offset).
      Specified by:
      readUnsignedInt in interface RandomAccessReader
      Parameters:
      offset - The buffer offset to read from.
      Returns:
      The int at the offset, as a long.
      Throws:
      IOException - If there was an exception while reading.
    • readLong

      public long readLong(long offset) throws IOException
      Description copied from interface: RandomAccessReader
      Read a long at a specific offset (without changing the current cursor offset).
      Specified by:
      readLong in interface RandomAccessReader
      Parameters:
      offset - The buffer offset to read from.
      Returns:
      The long at the offset.
      Throws:
      IOException - If there was an exception while reading.
    • readString

      public String readString(long offset, int numBytes, boolean replaceSlashWithDot, boolean stripLSemicolon) throws IOException
      Description copied from interface: RandomAccessReader
      Reads the "modified UTF8" format defined in the Java classfile spec, optionally replacing '/' with '.', and optionally removing the prefix "L" and the suffix ";".
      Specified by:
      readString in interface RandomAccessReader
      Parameters:
      offset - The start offset of the string.
      numBytes - The number of bytes of the UTF8 encoding of the string.
      replaceSlashWithDot - If true, replace '/' with '.'.
      stripLSemicolon - If true, string final ';' character.
      Returns:
      The string.
      Throws:
      IOException - If an I/O exception occurs.
    • readString

      public String readString(long offset, int numBytes) throws IOException
      Description copied from interface: RandomAccessReader
      Reads the "modified UTF8" format defined in the Java classfile spec.
      Specified by:
      readString in interface RandomAccessReader
      Parameters:
      offset - The start offset of the string.
      numBytes - The number of bytes of the UTF8 encoding of the string.
      Returns:
      The string.
      Throws:
      IOException - If an I/O exception occurs.