Package net.mtrop.doom.struct.io
Class SerialReader
java.lang.Object
net.mtrop.doom.struct.io.SerialReader
Assists in endian reading from an input stream.
- Author:
- Matthew Tropiano
-
Field Summary
Modifier and TypeFieldDescriptionstatic final boolean
static final boolean
-
Constructor Summary
ConstructorDescriptionSerialReader
(boolean endianMode) Wraps a super reader around an InputStream. -
Method Summary
Modifier and TypeMethodDescriptionprotected int
byteRead
(InputStream in) Reads a byte from the bound stream.protected int
byteRead
(InputStream in, byte[] b) Reads a series of bytes from the bound stream into a byte array until end of stream is reached or the array is filled with bytes.protected int
byteRead
(InputStream in, byte[] b, int maxlen) Reads a series of bytes from the bound stream into a byte array until end of stream is reached ormaxlen
bytes have been read.int
Reads in a 24-bit integer.boolean
Reads a byte as a boolean value.boolean[]
Reads in an array of boolean values.byte
readByte
(InputStream in) Reads in a single byte.byte[]
Reads a byte array in from the reader.int
readBytes
(InputStream in, byte[] b) Reads a series of bytes from the bound stream into a byte array until end of stream is reached or the array is filled with bytes.int
readBytes
(InputStream in, byte[] b, int maxlen) Reads a series of bytes from the bound stream into a byte array until end of stream is reached ormaxlen
bytes have been read.byte[]
readBytes
(InputStream in, int n) Reads in a specified amount of bytes, returned as an array.char
readChar
(InputStream in) Reads in a character.char[]
Reads in an array of characters.char[]
readChars
(InputStream in, int n) Reads in a specific amount of characters.double
Reads in a 64-bit float.double[]
Reads in an array 64-bit floats.double[]
readDoubles
(InputStream in, int n) Reads in a specified amount of 64-bit floats.float
readFloat
(InputStream in) Reads in a 32-bit float.float[]
Reads in an array 32-bit floats.float[]
readFloats
(InputStream in, int n) Reads in a specified amount of 32-bit floats.boolean
readFor
(InputStream in, byte[] b) Reads a bunch of bytes and checks to see if a set of bytes match completely with the input byte string.int
readInt
(InputStream in) Reads in an integer.int[]
Reads in an array of integers.int[]
readInts
(InputStream in, int n) Reads in a specified amount of integers.long
readLong
(InputStream in) Reads in a long value.long[]
Reads in an array of long values.long[]
readLongs
(InputStream in, int n) Reads in an amount of long values specified by the user.short
readShort
(InputStream in) Reads in a short.short[]
Reads in an array of shorts.short[]
readShorts
(InputStream in, int n) Reads in a specified amount of shorts.Reads a char array and returns it as a String.readString
(InputStream in, int bytes, String encoding) Reads a byte vector of specific length and returns it as a String in a particular encoding.readString
(InputStream in, int bytes, Charset charset) Reads a byte vector of specific length and returns it as a String in a particular encoding.readString
(InputStream in, String encoding) Reads a byte vector (an int followed by a series of bytes) and returns it as a String in a particular encoding.readString
(InputStream in, Charset charset) Reads a byte vector (an int followed by a series of bytes) and returns it as a String in a particular encoding.String[]
Reads in an array of strings.short
Reads in a single byte, cast to a short to eliminate sign.long
Reads in a integer, cast to a long, discarding sign.int
Reads in a short, cast to an integer, discarding sign.int
Reads an integer from an input stream that is variable-length encoded.long
Reads a long from an input stream that is variable-length encoded.boolean
seekToPattern
(InputStream in, byte[] b) Keeps reading until it hits a specific byte pattern.void
setEndianMode
(boolean mode) Sets the byte endian mode for the byte conversion methods.
-
Field Details
-
LITTLE_ENDIAN
public static final boolean LITTLE_ENDIAN- See Also:
-
BIG_ENDIAN
public static final boolean BIG_ENDIAN- See Also:
-
-
Constructor Details
-
SerialReader
public SerialReader(boolean endianMode) Wraps a super reader around an InputStream.- Parameters:
endianMode
- an _ENDIAN mode.
-
-
Method Details
-
setEndianMode
public void setEndianMode(boolean mode) Sets the byte endian mode for the byte conversion methods. LITTLE_ENDIAN (Intel), the default, orients values from lowest byte to highest, while BIG_ENDIAN (Motorola, VAX) orients values from highest byte to lowest.- Parameters:
mode
- an _ENDIAN mode.
-
byteRead
Reads a byte from the bound stream.- Parameters:
in
- the input stream to read from.- Returns:
- the byte read or -1 if the end of the stream is reached.
- Throws:
IOException
- if a read error occurs.
-
byteRead
Reads a series of bytes from the bound stream into a byte array until end of stream is reached or the array is filled with bytes.- Parameters:
in
- the input stream to read from.b
- the target array to fill with bytes.- Returns:
- the amount of bytes read or -1 if the end of the stream is reached before a single byte is read.
- Throws:
IOException
- if a read error occurs.
-
byteRead
Reads a series of bytes from the bound stream into a byte array until end of stream is reached ormaxlen
bytes have been read.- Parameters:
in
- the input stream to read from.b
- the target array to fill with bytes.maxlen
- the maximum amount of bytes to read.- Returns:
- the amount of bytes read or -1 if the end of the stream is reached before a single byte is read.
- Throws:
IOException
- if a read error occurs.
-
seekToPattern
Keeps reading until it hits a specific byte pattern.- Parameters:
in
- the input stream to read from.b
- the pattern to search for.- Returns:
- true if the pattern is found, returns false if the end of the stream is reached before the pattern is matched.
- Throws:
IOException
- if a read error occurs.
-
readFor
Reads a bunch of bytes and checks to see if a set of bytes match completely with the input byte string. It reads up to the length of b before it starts the check.- Parameters:
in
- the input stream to read from.b
- the input byte string.- Returns:
- true if the bytes read equal the the same bytes in the input array.
- Throws:
IOException
- if a read error occurs.
-
readByteArray
Reads a byte array in from the reader.- Parameters:
in
- the input stream to read from.- Returns:
- an array of bytes
- Throws:
IOException
- if the end of the stream is reached prematurely.
-
readString
Reads a char array and returns it as a String.- Parameters:
in
- the input stream to read from.- Returns:
- the resulting String.
- Throws:
IOException
- if an I/O error occurs.
-
readString
Reads a byte vector (an int followed by a series of bytes) and returns it as a String in a particular encoding.- Parameters:
in
- the input stream to read from.encoding
- the name of the encoding scheme.- Returns:
- the decoded string.
- Throws:
IOException
- if an I/O error occurs.
-
readString
Reads a byte vector (an int followed by a series of bytes) and returns it as a String in a particular encoding.- Parameters:
in
- the input stream to read from.charset
- the name of the charset to use.- Returns:
- the decoded string.
- Throws:
IOException
- if an I/O error occurs.
-
readString
Reads a byte vector of specific length and returns it as a String in a particular encoding.- Parameters:
in
- the input stream to read from.bytes
- the amount of bytes to read.encoding
- the name of the encoding scheme.- Returns:
- the decoded string.
- Throws:
IOException
- if an I/O error occurs.
-
readString
Reads a byte vector of specific length and returns it as a String in a particular encoding.- Parameters:
in
- the input stream to read from.bytes
- the amount of bytes to read.charset
- the name of the charset to use.- Returns:
- the decoded string.
- Throws:
IOException
- if an I/O error occurs.
-
readStringArray
Reads in an array of strings. Basically reads an integer length which is the length of the array and then reads that many strings.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded string array.
- Throws:
IOException
- if an error occurred during the read.
-
readBoolean
Reads a byte as a boolean value.- Parameters:
in
- the input stream to read from.- Returns:
- a boolean value.
- Throws:
IOException
- if an error occurred during the read.
-
readBooleanArray
Reads in an array of boolean values. Basically reads an integer length which is the amount of booleans and then reads in an integer at a time scanning bits for the boolean values.- Parameters:
in
- the input stream to read from.- Returns:
- the boolean array.
- Throws:
IOException
- if an error occurred during the read.
-
readLong
Reads in a long value.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readLongs
Reads in an amount of long values specified by the user.- Parameters:
in
- the input stream to read from.n
- the amount of long integers to read.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readLongArray
Reads in an array of long values. Basically reads an integer length which is the length of the array and then reads that many longs.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readByte
Reads in a single byte.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readUnsignedByte
Reads in a single byte, cast to a short to eliminate sign.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readBytes
Reads a series of bytes from the bound stream into a byte array until end of stream is reached or the array is filled with bytes.- Parameters:
in
- the input stream to read from.b
- the target array to fill with bytes.- Returns:
- the amount of bytes read or END_OF_STREAM if the end of the stream is reached before a single byte is read.
- Throws:
IOException
- if an error occurred during the read.
-
readBytes
Reads a series of bytes from the bound stream into a byte array until end of stream is reached ormaxlen
bytes have been read.- Parameters:
in
- the input stream to read from.b
- the target array to fill with bytes.maxlen
- the maximum amount of bytes to read.- Returns:
- the amount of bytes read or END_OF_STREAM if the end of the stream is reached before a single byte is read.
- Throws:
IOException
- if an error occurred during the read.
-
readBytes
Reads in a specified amount of bytes, returned as an array.- Parameters:
in
- the input stream to read from.n
- the amount of bytes to read.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readUnsignedInt
Reads in a integer, cast to a long, discarding sign.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readInt
Reads in an integer.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
read24BitInt
Reads in a 24-bit integer.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readInts
Reads in a specified amount of integers.- Parameters:
in
- the input stream to read from.n
- the amount of integers to read.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readIntArray
Reads in an array of integers. Basically reads an integer length which is the length of the array and then reads that many integers.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readFloat
Reads in a 32-bit float.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readFloats
Reads in a specified amount of 32-bit floats.- Parameters:
in
- the input stream to read from.n
- the amount of floats to read.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readFloatArray
Reads in an array 32-bit floats. Basically reads an integer length which is the length of the array and then reads that many floats.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readDouble
Reads in a 64-bit float.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readDoubles
Reads in a specified amount of 64-bit floats.- Parameters:
in
- the input stream to read from.n
- the amount of doubles to read.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readDoubleArray
Reads in an array 64-bit floats. Basically reads an integer length which is the length of the array and then reads that many doubles.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readShort
Reads in a short.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readUnsignedShort
Reads in a short, cast to an integer, discarding sign.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readShorts
Reads in a specified amount of shorts.- Parameters:
in
- the input stream to read from.n
- the amount of shorts to read.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readShortArray
Reads in an array of shorts. Basically reads an integer length which is the length of the array and then reads that many shorts.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readChar
Reads in a character.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readChars
Reads in a specific amount of characters.- Parameters:
in
- the input stream to read from.n
- the amount of characters to read (16-bit).- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readCharArray
Reads in an array of characters. Basically reads an integer length which is the length of the array and then reads that many characters.- Parameters:
in
- the input stream to read from.- Returns:
- the decoded value.
- Throws:
IOException
- if an error occurred during the read.
-
readVariableLengthInt
Reads an integer from an input stream that is variable-length encoded. Reads up to four bytes. Due to the nature of this value, it is always read in a Big-Endian fashion.- Parameters:
in
- the input stream to read from.- Returns:
- an int value from 0x00000000 to 0x0FFFFFFF.
- Throws:
IOException
- if the next byte to read is not available.
-
readVariableLengthLong
Reads a long from an input stream that is variable-length encoded. Reads up to eight bytes. Due to the nature of this value, it is always read in a Big-Endian fashion.- Parameters:
in
- the input stream to read from.- Returns:
- a long value from 0x0000000000000000 to 0x7FFFFFFFFFFFFFFF.
- Throws:
IOException
- if the next byte to read is not available.
-