Class ClassfileReader
java.lang.Object
nonapi.io.github.classgraph.fileslice.reader.ClassfileReader
- All Implemented Interfaces:
Closeable,AutoCloseable,RandomAccessReader,SequentialReader
public class ClassfileReader
extends Object
implements RandomAccessReader, SequentialReader, Closeable
A
Slice reader that works as either a RandomAccessReader or a SequentialReader. The file
is buffered up to the point it has been read so far. Reads in big endian order, as required by the
classfile format.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]Buffer.private intThe number of bytes used in arr.private static final intRead this many bytes each time there is a buffer underrun.private intThe length of the classfile if known (because it is not deflated), or -1 if unknown (because it is deflated).private intThe current read index within the slice.private InputStreamIf slice is deflated, a wrapper for.invalid reference
InflateInputStreamprivate static final intInitial buffer size.private RandomAccessReaderIf slice is not deflated, aRandomAccessReaderfor either theArraySliceorFileSliceconcrete subclass.private ResourceThe underlying resource to close whenclose()is called. -
Constructor Summary
ConstructorsConstructorDescriptionClassfileReader(InputStream inputStream, Resource resourceToClose) Constructor for reader of moduleInputStream(which is not deflated).ClassfileReader(Slice slice, Resource resourceToClose) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]buf()Buf.voidbufferTo(int numBytes) Ensure that the given number of bytes have been read into the buffer from the beginning of the slice.voidclose()intcurrPos()Curr pos.intread(long srcOffset, byte[] dstArr, int dstArrStart, int numBytes) Read bytes into a byte array.intread(long srcOffset, ByteBuffer dstBuf, int dstBufStart, int numBytes) Read bytes into aByteBuffer.bytereadByte()Read a byte at the current cursor position.bytereadByte(long offset) Read a byte at a specific offset (without changing the current cursor offset).intreadInt()Read a int at the current cursor position.intreadInt(long offset) Read a int at a specific offset (without changing the current cursor offset).longreadLong()Read a long at the current cursor position.longreadLong(long offset) Read a long at a specific offset (without changing the current cursor offset).shortRead a short at the current cursor position.shortreadShort(long offset) Read a short at a specific offset (without changing the current cursor offset).readString(int numBytes) Reads the "modified UTF8" format defined in the Java classfile spec.readString(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 ";".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 ";".private voidreadTo(int targetArrUsed) Called when there is a buffer underrun to ensure there are sufficient bytes available in the array to read the given number of bytes at the given start index.intRead an unsigned byte at the current cursor position.intreadUnsignedByte(long offset) Read an unsigned byte at a specific offset (without changing the current cursor offset).longRead a unsigned int at the current cursor position.longreadUnsignedInt(long offset) Read a unsigned int at a specific offset (without changing the current cursor offset).intRead a unsigned short at the current cursor position.intreadUnsignedShort(long offset) Read a unsigned short at a specific offset (without changing the current cursor offset).voidskip(int bytesToSkip) Skip the given number of bytes.
-
Field Details
-
resourceToClose
The underlying resource to close whenclose()is called. -
inflaterInputStream
If slice is deflated, a wrapper for.invalid reference
InflateInputStream -
randomAccessReader
If slice is not deflated, aRandomAccessReaderfor either theArraySliceorFileSliceconcrete subclass. -
arr
private byte[] arrBuffer. -
arrUsed
private int arrUsedThe number of bytes used in arr. -
currIdx
private int currIdxThe current read index within the slice. -
classfileLengthHint
private int classfileLengthHintThe length of the classfile if known (because it is not deflated), or -1 if unknown (because it is deflated). -
INITIAL_BUF_SIZE
private static final int INITIAL_BUF_SIZEInitial buffer size. For most classfiles, only the first 16-64kb needs to be read (we don't read the bytecodes).- See Also:
-
BUF_CHUNK_SIZE
private static final int BUF_CHUNK_SIZERead this many bytes each time there is a buffer underrun. This is smaller than 8k by 8 bytes to prevent the doubling of the array size when the last chunk doesn't quite fit within the 16kb of INITIAL_BUF_SIZE, since the number of bytes that can be requested is up to 8 (for longs). Otherwise we could request to read to (8kb * 2 + 8), which would double the size of the buffer to 32kb, but if we only need to read between 8kb and 16kb, then we unnecessarily copied the buffer content one extra time.- See Also:
-
-
Constructor Details
-
ClassfileReader
Constructor.- Parameters:
slice- theSliceto read.resourceToClose- the resource to close whenclose()is called, or null.- Throws:
IOException- If an inflater cannot be opened on theSlice.
-
ClassfileReader
Constructor for reader of moduleInputStream(which is not deflated).- Parameters:
inputStream- theInputStreamto read from.resourceToClose- the underlying resource to close whenclose()is called, or null.- Throws:
IOException- If an inflater cannot be opened on theSlice.
-
-
Method Details
-
currPos
public int currPos()Curr pos.- Returns:
- the current read position.
-
buf
public byte[] buf()Buf.- Returns:
- the buffer.
-
readTo
Called when there is a buffer underrun to ensure there are sufficient bytes available in the array to read the given number of bytes at the given start index.- Parameters:
targetArrUsed- the target value forarrUsed(i.e. the number of bytes that must be filled in the array)- Throws:
IOException- Signals that an I/O exception has occurred.
-
bufferTo
Ensure that the given number of bytes have been read into the buffer from the beginning of the slice.- Parameters:
numBytes- the number of bytes to ensure have been buffered- Throws:
IOException- on EOF or if the bytes could not be read.
-
read
Description copied from interface:RandomAccessReaderRead bytes into a byte array.- Specified by:
readin interfaceRandomAccessReader- 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.
-
read
public int read(long srcOffset, ByteBuffer dstBuf, int dstBufStart, int numBytes) throws IOException Description copied from interface:RandomAccessReaderRead bytes into aByteBuffer.- Specified by:
readin interfaceRandomAccessReader- Parameters:
srcOffset- The offset to start reading from.dstBuf- TheByteBufferto 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.
-
readByte
Description copied from interface:RandomAccessReaderRead a byte at a specific offset (without changing the current cursor offset).- Specified by:
readBytein interfaceRandomAccessReader- Parameters:
offset- The buffer offset to read from.- Returns:
- The byte at the offset.
- Throws:
IOException- If there was an exception while reading.
-
readUnsignedByte
Description copied from interface:RandomAccessReaderRead an unsigned byte at a specific offset (without changing the current cursor offset).- Specified by:
readUnsignedBytein interfaceRandomAccessReader- 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
Description copied from interface:RandomAccessReaderRead a short at a specific offset (without changing the current cursor offset).- Specified by:
readShortin interfaceRandomAccessReader- Parameters:
offset- The buffer offset to read from.- Returns:
- The short at the offset.
- Throws:
IOException- If there was an exception while reading.
-
readUnsignedShort
Description copied from interface:RandomAccessReaderRead a unsigned short at a specific offset (without changing the current cursor offset).- Specified by:
readUnsignedShortin interfaceRandomAccessReader- 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
Description copied from interface:RandomAccessReaderRead a int at a specific offset (without changing the current cursor offset).- Specified by:
readIntin interfaceRandomAccessReader- Parameters:
offset- The buffer offset to read from.- Returns:
- The int at the offset.
- Throws:
IOException- If there was an exception while reading.
-
readUnsignedInt
Description copied from interface:RandomAccessReaderRead a unsigned int at a specific offset (without changing the current cursor offset).- Specified by:
readUnsignedIntin interfaceRandomAccessReader- 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
Description copied from interface:RandomAccessReaderRead a long at a specific offset (without changing the current cursor offset).- Specified by:
readLongin interfaceRandomAccessReader- Parameters:
offset- The buffer offset to read from.- Returns:
- The long at the offset.
- Throws:
IOException- If there was an exception while reading.
-
readByte
Description copied from interface:SequentialReaderRead a byte at the current cursor position.- Specified by:
readBytein interfaceSequentialReader- Returns:
- The byte at the current cursor position.
- Throws:
IOException- If there was an exception while reading.
-
readUnsignedByte
Description copied from interface:SequentialReaderRead an unsigned byte at the current cursor position.- Specified by:
readUnsignedBytein interfaceSequentialReader- Returns:
- The unsigned byte at the current cursor position.
- Throws:
IOException- If there was an exception while reading.
-
readShort
Description copied from interface:SequentialReaderRead a short at the current cursor position.- Specified by:
readShortin interfaceSequentialReader- Returns:
- The short at the current cursor position.
- Throws:
IOException- If there was an exception while reading.
-
readUnsignedShort
Description copied from interface:SequentialReaderRead a unsigned short at the current cursor position.- Specified by:
readUnsignedShortin interfaceSequentialReader- Returns:
- The unsigned shortat the current cursor position.
- Throws:
IOException- If there was an exception while reading.
-
readInt
Description copied from interface:SequentialReaderRead a int at the current cursor position.- Specified by:
readIntin interfaceSequentialReader- Returns:
- The int at the current cursor position.
- Throws:
IOException- If there was an exception while reading.
-
readUnsignedInt
Description copied from interface:SequentialReaderRead a unsigned int at the current cursor position.- Specified by:
readUnsignedIntin interfaceSequentialReader- Returns:
- The int at the current cursor position, as a long.
- Throws:
IOException- If there was an exception while reading.
-
readLong
Description copied from interface:SequentialReaderRead a long at the current cursor position.- Specified by:
readLongin interfaceSequentialReader- Returns:
- The long at the current cursor position.
- Throws:
IOException- If there was an exception while reading.
-
skip
Description copied from interface:SequentialReaderSkip the given number of bytes.- Specified by:
skipin interfaceSequentialReader- Parameters:
bytesToSkip- The number of bytes to skip.- 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:RandomAccessReaderReads the "modified UTF8" format defined in the Java classfile spec, optionally replacing '/' with '.', and optionally removing the prefix "L" and the suffix ";".- Specified by:
readStringin interfaceRandomAccessReader- 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(int numBytes, boolean replaceSlashWithDot, boolean stripLSemicolon) throws IOException Description copied from interface:SequentialReaderReads the "modified UTF8" format defined in the Java classfile spec, optionally replacing '/' with '.', and optionally removing the prefix "L" and the suffix ";".- Specified by:
readStringin interfaceSequentialReader- Parameters:
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
Description copied from interface:RandomAccessReaderReads the "modified UTF8" format defined in the Java classfile spec.- Specified by:
readStringin interfaceRandomAccessReader- 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.
-
readString
Description copied from interface:SequentialReaderReads the "modified UTF8" format defined in the Java classfile spec.- Specified by:
readStringin interfaceSequentialReader- Parameters:
numBytes- The number of bytes of the UTF8 encoding of the string.- Returns:
- The string.
- Throws:
IOException- If an I/O exception occurs.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-