Top Description Fields Constructors Methods
nonapi.io.github.classgraph.fileslice.reader

public Class ClassfileReader

extends Object
implements RandomAccessReader, SequentialReader, Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable, nonapi.io.github.classgraph.fileslice.reader.SequentialReader, nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader
Imports
java.io.Closeable, .IOException, .InputStream, java.nio.Buffer, .BufferUnderflowException, .ByteBuffer, .ReadOnlyBufferException, java.util.Arrays, io.github.classgraph.Resource, nonapi.io.github.classgraph.fileslice.ArraySlice, .FileSlice, .Slice, nonapi.io.github.classgraph.utils.FileUtils, .StringUtils

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

Modifier and TypeField and Description
private byte[]
arr

Buffer.

private int
arrUsed

The number of bytes used in arr.

private static final int
BUF_CHUNK_SIZE

Read this many bytes each time there is a buffer underrun.

private int
classfileLengthHint

The length of the classfile if known (because it is not deflated), or -1 if unknown (because it is deflated).

private int
currIdx

The current read index within the slice.

private InputStream
inflaterInputStream

If slice is deflated, a wrapper for InflateInputStream.

private static final int
INITIAL_BUF_SIZE

Initial buffer size.

private RandomAccessReader
randomAccessReader

If slice is not deflated, a RandomAccessReader for either the ArraySlice or FileSlice concrete subclass.

private Resource
resourceToClose

The underlying resource to close when ClassfileReader#close() is called.

Constructor Summary

AccessConstructor and Description
public
ClassfileReader(final Slice
the Slice to read.
slice
,
final Resource
the resource to close when ClassfileReader#close() is called, or null.
resourceToClose
)

Constructor.

public
ClassfileReader(final InputStream
the InputStream to read from.
inputStream
,
final Resource
the underlying resource to close when ClassfileReader#close() is called, or null.
resourceToClose
)

Constructor for reader of module InputStream (which is not deflated).

Method Summary

Modifier and TypeMethod and Description
public byte[]

Returns:

the buffer.
buf
()

Buf.

public void
bufferTo(final int
the number of bytes to ensure have been buffered
numBytes
)

Ensure that the given number of bytes have been read into the buffer from the beginning of the slice.

public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

public int

Returns:

the current read position.
currPos
()

Curr pos.

public int
read(final long
The offset to start reading from.
srcOffset
,
final byte[]
The byte array to write into.
dstArr
,
final int
The offset within the destination array to start writing at.
dstArrStart
,
final int
The number of bytes to read.
numBytes
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.read.

Read bytes into a byte array.

public int
read(final long
The offset to start reading from.
srcOffset
,
final ByteBuffer
The ByteBuffer to write into.
dstBuf
,
final int
The offset within the destination buffer to start writing at.
dstBufStart
,
final int
The number of bytes to read.
numBytes
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.read.

Read bytes into a ByteBuffer.

public byte
readByte(final long
The buffer offset to read from.
offset
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readByte.

Read a byte at a specific offset (without changing the current cursor offset).

public byte
readByte()

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readByte.

Read a byte at the current cursor position.

public int
readInt(final long
The buffer offset to read from.
offset
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readInt.

Read a int at a specific offset (without changing the current cursor offset).

public int
readInt()

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readInt.

Read a int at the current cursor position.

public long
readLong(final long
The buffer offset to read from.
offset
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readLong.

Read a long at a specific offset (without changing the current cursor offset).

public long
readLong()

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readLong.

Read a long at the current cursor position.

public short
readShort(final long
The buffer offset to read from.
offset
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readShort.

Read a short at a specific offset (without changing the current cursor offset).

public short
readShort()

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readShort.

Read a short at the current cursor position.

public String
readString(final long
The start offset of the string.
offset
,
final int
The number of bytes of the UTF8 encoding of the string.
numBytes
,
final boolean
If true, replace '/' with '.'.
replaceSlashWithDot
,
final boolean
If true, string final ';' character.
stripLSemicolon
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readString.

Reads the "modified UTF8" format defined in the Java classfile spec, optionally replacing '/' with '.', and optionally removing the prefix "L" and the suffix ";".

public String
readString(final int
The number of bytes of the UTF8 encoding of the string.
numBytes
,
final boolean
If true, replace '/' with '.'.
replaceSlashWithDot
,
final boolean
If true, string final ';' character.
stripLSemicolon
)

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readString.

Reads the "modified UTF8" format defined in the Java classfile spec, optionally replacing '/' with '.', and optionally removing the prefix "L" and the suffix ";".

public String
readString(final long
The start offset of the string.
offset
,
final int
The number of bytes of the UTF8 encoding of the string.
numBytes
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readString.

Reads the "modified UTF8" format defined in the Java classfile spec.

public String
readString(final int
The number of bytes of the UTF8 encoding of the string.
numBytes
)

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readString.

Reads the "modified UTF8" format defined in the Java classfile spec.

private void
readTo(final int
the target value for arrUsed (i.e. the number of bytes that must be filled in the array)
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.

public int
readUnsignedByte(final long
The buffer offset to read from.
offset
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedByte.

Read an unsigned byte at a specific offset (without changing the current cursor offset).

public int
readUnsignedByte()

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedByte.

Read an unsigned byte at the current cursor position.

public long
readUnsignedInt(final long
The buffer offset to read from.
offset
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedInt.

Read a unsigned int at a specific offset (without changing the current cursor offset).

public long
readUnsignedInt()

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedInt.

Read a unsigned int at the current cursor position.

public int
readUnsignedShort(final long
The buffer offset to read from.
offset
)

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedShort.

Read a unsigned short at a specific offset (without changing the current cursor offset).

public int
readUnsignedShort()

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedShort.

Read a unsigned short at the current cursor position.

public void
skip(final int
The number of bytes to skip.
bytesToSkip
)

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.skip.

Skip the given number of bytes.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

arrback to summary
private byte[] arr

Buffer.

arrUsedback to summary
private int arrUsed

The number of bytes used in arr.

BUF_CHUNK_SIZEback to summary
private static final int BUF_CHUNK_SIZE

Read 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.

classfileLengthHintback to summary
private int classfileLengthHint

The length of the classfile if known (because it is not deflated), or -1 if unknown (because it is deflated).

currIdxback to summary
private int currIdx

The current read index within the slice.

inflaterInputStreamback to summary
private InputStream inflaterInputStream

If slice is deflated, a wrapper for InflateInputStream.

INITIAL_BUF_SIZEback to summary
private static final int INITIAL_BUF_SIZE

Initial buffer size. For most classfiles, only the first 16-64kb needs to be read (we don't read the bytecodes).

randomAccessReaderback to summary
private RandomAccessReader randomAccessReader

If slice is not deflated, a RandomAccessReader for either the ArraySlice or FileSlice concrete subclass.

resourceToCloseback to summary
private Resource resourceToClose

The underlying resource to close when ClassfileReader#close() is called.

Constructor Detail

ClassfileReaderback to summary
public ClassfileReader(final Slice slice, final Resource resourceToClose) throws IOException

Constructor.

Parameters
slice:Slice

the Slice to read.

resourceToClose:Resource

the resource to close when ClassfileReader#close() is called, or null.

Exceptions
IOException:
If an inflater cannot be opened on the Slice.
ClassfileReaderback to summary
public ClassfileReader(final InputStream inputStream, final Resource resourceToClose) throws IOException

Constructor for reader of module InputStream (which is not deflated).

Parameters
inputStream:InputStream

the InputStream to read from.

resourceToClose:Resource

the underlying resource to close when ClassfileReader#close() is called, or null.

Exceptions
IOException:
If an inflater cannot be opened on the Slice.

Method Detail

bufback to summary
public byte[] buf()

Buf.

Returns:byte[]

the buffer.

bufferToback to summary
public void bufferTo(final int numBytes) throws IOException

Ensure that the given number of bytes have been read into the buffer from the beginning of the slice.

Parameters
numBytes:int

the number of bytes to ensure have been buffered

Exceptions
IOException:
on EOF or if the bytes could not be read.
closeback to summary
public void close()

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
currPosback to summary
public int currPos()

Curr pos.

Returns:int

the current read position.

readback to summary
public int read(final long srcOffset, final byte[] dstArr, final int dstArrStart, final int numBytes) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.read.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.read.

Read bytes into a byte array.

Parameters
srcOffset:long

The offset to start reading from.

dstArr:byte[]

The byte array to write into.

dstArrStart:int

The offset within the destination array to start writing at.

numBytes:int

The number of bytes to read.

Returns:int

The number of bytes actually read, or -1 if no more bytes could be read.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readback to summary
public int read(final long srcOffset, final ByteBuffer dstBuf, final int dstBufStart, final int numBytes) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.read.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.read.

Read bytes into a ByteBuffer.

Parameters
srcOffset:long

The offset to start reading from.

dstBuf:ByteBuffer

The ByteBuffer to write into.

dstBufStart:int

The offset within the destination buffer to start writing at.

numBytes:int

The number of bytes to read.

Returns:int

The number of bytes actually read, or -1 if no more bytes could be read.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readByteback to summary
public byte readByte(final long offset) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readByte.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readByte.

Read a byte at a specific offset (without changing the current cursor offset).

Parameters
offset:long

The buffer offset to read from.

Returns:byte

The byte at the offset.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readByteback to summary
public byte readByte() throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readByte.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readByte.

Read a byte at the current cursor position.

Returns:byte

The byte at the current cursor position.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readIntback to summary
public int readInt(final long offset) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readInt.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readInt.

Read a int at a specific offset (without changing the current cursor offset).

Parameters
offset:long

The buffer offset to read from.

Returns:int

The int at the offset.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readIntback to summary
public int readInt() throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readInt.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readInt.

Read a int at the current cursor position.

Returns:int

The int at the current cursor position.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readLongback to summary
public long readLong(final long offset) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readLong.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readLong.

Read a long at a specific offset (without changing the current cursor offset).

Parameters
offset:long

The buffer offset to read from.

Returns:long

The long at the offset.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readLongback to summary
public long readLong() throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readLong.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readLong.

Read a long at the current cursor position.

Returns:long

The long at the current cursor position.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readShortback to summary
public short readShort(final long offset) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readShort.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readShort.

Read a short at a specific offset (without changing the current cursor offset).

Parameters
offset:long

The buffer offset to read from.

Returns:short

The short at the offset.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readShortback to summary
public short readShort() throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readShort.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readShort.

Read a short at the current cursor position.

Returns:short

The short at the current cursor position.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readStringback to summary
public String readString(final long offset, final int numBytes, final boolean replaceSlashWithDot, final boolean stripLSemicolon) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readString.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readString.

Reads the "modified UTF8" format defined in the Java classfile spec, optionally replacing '/' with '.', and optionally removing the prefix "L" and the suffix ";".

Parameters
offset:long

The start offset of the string.

numBytes:int

The number of bytes of the UTF8 encoding of the string.

replaceSlashWithDot:boolean

If true, replace '/' with '.'.

stripLSemicolon:boolean

If true, string final ';' character.

Returns:String

The string.

Annotations
@Override
Exceptions
IOException:
If an I/O exception occurs.
readStringback to summary
public String readString(final int numBytes, final boolean replaceSlashWithDot, final boolean stripLSemicolon) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readString.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readString.

Reads the "modified UTF8" format defined in the Java classfile spec, optionally replacing '/' with '.', and optionally removing the prefix "L" and the suffix ";".

Parameters
numBytes:int

The number of bytes of the UTF8 encoding of the string.

replaceSlashWithDot:boolean

If true, replace '/' with '.'.

stripLSemicolon:boolean

If true, string final ';' character.

Returns:String

The string.

Annotations
@Override
Exceptions
IOException:
If an I/O exception occurs.
readStringback to summary
public String readString(final long offset, final int numBytes) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readString.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readString.

Reads the "modified UTF8" format defined in the Java classfile spec.

Parameters
offset:long

The start offset of the string.

numBytes:int

The number of bytes of the UTF8 encoding of the string.

Returns:String

The string.

Annotations
@Override
Exceptions
IOException:
If an I/O exception occurs.
readStringback to summary
public String readString(final int numBytes) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readString.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readString.

Reads the "modified UTF8" format defined in the Java classfile spec.

Parameters
numBytes:int

The number of bytes of the UTF8 encoding of the string.

Returns:String

The string.

Annotations
@Override
Exceptions
IOException:
If an I/O exception occurs.
readToback to summary
private void readTo(final int targetArrUsed) throws IOException

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:int

the target value for arrUsed (i.e. the number of bytes that must be filled in the array)

Exceptions
IOException:
Signals that an I/O exception has occurred.
readUnsignedByteback to summary
public int readUnsignedByte(final long offset) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedByte.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedByte.

Read an unsigned byte at a specific offset (without changing the current cursor offset).

Parameters
offset:long

The buffer offset to read from.

Returns:int

The unsigned byte at the offset.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readUnsignedByteback to summary
public int readUnsignedByte() throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedByte.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedByte.

Read an unsigned byte at the current cursor position.

Returns:int

The unsigned byte at the current cursor position.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readUnsignedIntback to summary
public long readUnsignedInt(final long offset) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedInt.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedInt.

Read a unsigned int at a specific offset (without changing the current cursor offset).

Parameters
offset:long

The buffer offset to read from.

Returns:long

The int at the offset, as a long.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readUnsignedIntback to summary
public long readUnsignedInt() throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedInt.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedInt.

Read a unsigned int at the current cursor position.

Returns:long

The int at the current cursor position, as a long.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readUnsignedShortback to summary
public int readUnsignedShort(final long offset) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedShort.

Doc from nonapi.io.github.classgraph.fileslice.reader.RandomAccessReader.readUnsignedShort.

Read a unsigned short at a specific offset (without changing the current cursor offset).

Parameters
offset:long

The buffer offset to read from.

Returns:int

The unsigned short at the offset.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
readUnsignedShortback to summary
public int readUnsignedShort() throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedShort.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.readUnsignedShort.

Read a unsigned short at the current cursor position.

Returns:int

The unsigned shortat the current cursor position.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.
skipback to summary
public void skip(final int bytesToSkip) throws IOException

Implements nonapi.io.github.classgraph.fileslice.reader.SequentialReader.skip.

Doc from nonapi.io.github.classgraph.fileslice.reader.SequentialReader.skip.

Skip the given number of bytes.

Parameters
bytesToSkip:int

The number of bytes to skip.

Annotations
@Override
Exceptions
IOException:
If there was an exception while reading.