Package com.drew.lang
Class SequentialReader
java.lang.Object
com.drew.lang.SequentialReader
- Direct Known Subclasses:
SequentialByteArrayReader,StreamReader
- Author:
- Drew Noakes https://drewnoakes.com
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract intReturns an estimate of the number of bytes that can be read (or skipped over) from thisSequentialReaderwithout blocking by the next invocation of a method for this input stream.abstract bytegetByte()Gets the next byte in the sequence.abstract voidgetBytes(byte[] buffer, int offset, int count) Retrieves bytes, writing them into a caller-provided buffer.abstract byte[]getBytes(int count) Returns the required number of bytes from the sequence.doublefloatshortgetInt16()Returns a signed 16-bit int calculated from two bytes of data (MSB, LSB).intgetInt32()Returns a signed 32-bit integer from four bytes of data.longgetInt64()Get a signed 64-bit integer from the buffer.bytegetInt8()Returns a signed 8-bit int calculated from the next byte the sequence.byte[]getNullTerminatedBytes(int maxLengthBytes) Returns the sequence of bytes punctuated by a\0value.getNullTerminatedString(int maxLengthBytes, Charset charset) Creates a String from the stream, ending wherebyte=='\0'or wherelength==maxLength.getNullTerminatedStringValue(int maxLengthBytes, Charset charset) Creates a String from the stream, ending wherebyte=='\0'or wherelength==maxLength.abstract longfloatGets a s15.16 fixed point float from the buffer.getString(int bytesRequested) getStringValue(int bytesRequested, Charset charset) intReturns an unsigned 16-bit int calculated from the next two bytes of the sequence.longGet a 32-bit unsigned integer from the buffer, returning it as a long.shortgetUInt8()Returns an unsigned 8-bit int calculated from the next byte of the sequence.booleanGets the endianness of this reader.voidsetMotorolaByteOrder(boolean motorolaByteOrder) Sets the endianness of this reader.abstract voidskip(long n) Skips forward in the sequence.abstract booleantrySkip(long n) Skips forward in the sequence, returning a boolean indicating whether the skip succeeded, or whether the sequence ended.
-
Constructor Details
-
SequentialReader
public SequentialReader()
-
-
Method Details
-
getPosition
- Throws:
IOException
-
getByte
Gets the next byte in the sequence.- Returns:
- The read byte value
- Throws:
IOException
-
getBytes
Returns the required number of bytes from the sequence.- Parameters:
count- The number of bytes to be returned- Returns:
- The requested bytes
- Throws:
IOException
-
getBytes
Retrieves bytes, writing them into a caller-provided buffer.- Parameters:
buffer- The array to write bytes to.offset- The starting position within buffer to write to.count- The number of bytes to be written.- Throws:
IOException
-
skip
Skips forward in the sequence. If the sequence ends, anEOFExceptionis thrown.- Parameters:
n- the number of byte to skip. Must be zero or greater.- Throws:
EOFException- the end of the sequence is reached.IOException- an error occurred reading from the underlying source.
-
trySkip
Skips forward in the sequence, returning a boolean indicating whether the skip succeeded, or whether the sequence ended.- Parameters:
n- the number of byte to skip. Must be zero or greater.- Returns:
- a boolean indicating whether the skip succeeded, or whether the sequence ended.
- Throws:
IOException- an error occurred reading from the underlying source.
-
available
public abstract int available()Returns an estimate of the number of bytes that can be read (or skipped over) from thisSequentialReaderwithout blocking by the next invocation of a method for this input stream. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.Note that while some implementations of
SequentialReaderlikeSequentialByteArrayReaderwill return the total remaining number of bytes in the stream, others will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.- Returns:
- an estimate of the number of bytes that can be read (or skipped
over) from this
SequentialReaderwithout blocking or0when it reaches the end of the input stream.
-
setMotorolaByteOrder
public void setMotorolaByteOrder(boolean motorolaByteOrder) Sets the endianness of this reader.truefor Motorola (or big) endianness (also known as network byte order), with MSB before LSB.falsefor Intel (or little) endianness, with LSB before MSB.
- Parameters:
motorolaByteOrder-truefor Motorola/big endian,falsefor Intel/little endian
-
isMotorolaByteOrder
public boolean isMotorolaByteOrder()Gets the endianness of this reader.truefor Motorola (or big) endianness (also known as network byte order), with MSB before LSB.falsefor Intel (or little) endianness, with LSB before MSB.
-
getUInt8
Returns an unsigned 8-bit int calculated from the next byte of the sequence.- Returns:
- the 8 bit int value, between 0 and 255
- Throws:
IOException
-
getInt8
Returns a signed 8-bit int calculated from the next byte the sequence.- Returns:
- the 8 bit int value, between 0x00 and 0xFF
- Throws:
IOException
-
getUInt16
Returns an unsigned 16-bit int calculated from the next two bytes of the sequence.- Returns:
- the 16 bit int value, between 0x0000 and 0xFFFF
- Throws:
IOException
-
getInt16
Returns a signed 16-bit int calculated from two bytes of data (MSB, LSB).- Returns:
- the 16 bit int value, between 0x0000 and 0xFFFF
- Throws:
IOException- the buffer does not contain enough bytes to service the request
-
getUInt32
Get a 32-bit unsigned integer from the buffer, returning it as a long.- Returns:
- the unsigned 32-bit int value as a long, between 0x00000000 and 0xFFFFFFFF
- Throws:
IOException- the buffer does not contain enough bytes to service the request
-
getInt32
Returns a signed 32-bit integer from four bytes of data.- Returns:
- the signed 32 bit int value, between 0x00000000 and 0xFFFFFFFF
- Throws:
IOException- the buffer does not contain enough bytes to service the request
-
getInt64
Get a signed 64-bit integer from the buffer.- Returns:
- the 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF
- Throws:
IOException- the buffer does not contain enough bytes to service the request
-
getS15Fixed16
Gets a s15.16 fixed point float from the buffer.This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.
- Returns:
- the floating point value
- Throws:
IOException- the buffer does not contain enough bytes to service the request
-
getFloat32
- Throws:
IOException
-
getDouble64
- Throws:
IOException
-
getString
- Throws:
IOException
-
getString
- Throws:
IOException
-
getString
- Throws:
IOException
-
getStringValue
- Throws:
IOException
-
getNullTerminatedString
Creates a String from the stream, ending wherebyte=='\0'or wherelength==maxLength.- Parameters:
maxLengthBytes- The maximum number of bytes to read. If a zero-byte is not reached within this limit, reading will stop and the string will be truncated to this length.- Returns:
- The read string.
- Throws:
IOException- The buffer does not contain enough bytes to satisfy this request.
-
getNullTerminatedStringValue
public StringValue getNullTerminatedStringValue(int maxLengthBytes, Charset charset) throws IOException Creates a String from the stream, ending wherebyte=='\0'or wherelength==maxLength.- Parameters:
maxLengthBytes- The maximum number of bytes to read. If a\0byte is not reached within this limit, reading will stop and the string will be truncated to this length.charset- TheCharsetto register with the returnedStringValue, ornullif the encoding is unknown- Returns:
- The read string.
- Throws:
IOException- The buffer does not contain enough bytes to satisfy this request.
-
getNullTerminatedBytes
Returns the sequence of bytes punctuated by a\0value.- Parameters:
maxLengthBytes- The maximum number of bytes to read. If a\0byte is not reached within this limit, the returned array will bemaxLengthByteslong.- Returns:
- The read byte array, excluding the null terminator.
- Throws:
IOException- The buffer does not contain enough bytes to satisfy this request.
-