Package com.blackrook.expression.struct
Class SerialReader
- java.lang.Object
-
- com.blackrook.expression.struct.SerialReader
-
public class SerialReader extends Object
Assists in endian reading from an input stream.- Author:
- Matthew Tropiano
-
-
Field Summary
Fields Modifier and Type Field Description static booleanBIG_ENDIANstatic booleanLITTLE_ENDIAN
-
Constructor Summary
Constructors Constructor Description SerialReader(boolean endianMode)Wraps a super reader around an InputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intbyteRead(InputStream in)Reads a byte from the bound stream.protected intbyteRead(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 intbyteRead(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 ormaxlenbytes have been read.intread24BitInt(InputStream in)Reads in a 24-bit integer.booleanreadBoolean(InputStream in)Reads a byte as a boolean value.boolean[]readBooleanArray(InputStream in)Reads in an array of boolean values.bytereadByte(InputStream in)Reads in a single byte.byte[]readByteArray(InputStream in)Reads a byte array in from the reader.intreadBytes(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.intreadBytes(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 ormaxlenbytes have been read.byte[]readBytes(InputStream in, int n)Reads in a specified amount of bytes, returned as an array.charreadChar(InputStream in)Reads in a character.char[]readCharArray(InputStream in)Reads in an array of characters.char[]readChars(InputStream in, int n)Reads in a specific amount of characters.doublereadDouble(InputStream in)Reads in a 64-bit float.double[]readDoubleArray(InputStream in)Reads in an array 64-bit floats.double[]readDoubles(InputStream in, int n)Reads in a specified amount of 64-bit floats.floatreadFloat(InputStream in)Reads in a 32-bit float.float[]readFloatArray(InputStream in)Reads in an array 32-bit floats.float[]readFloats(InputStream in, int n)Reads in a specified amount of 32-bit floats.booleanreadFor(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.intreadInt(InputStream in)Reads in an integer.int[]readIntArray(InputStream in)Reads in an array of integers.int[]readInts(InputStream in, int n)Reads in a specified amount of integers.longreadLong(InputStream in)Reads in a long value.long[]readLongArray(InputStream in)Reads in an array of long values.long[]readLongs(InputStream in, int n)Reads in an amount of long values specified by the user.shortreadShort(InputStream in)Reads in a short.short[]readShortArray(InputStream in)Reads in an array of shorts.short[]readShorts(InputStream in, int n)Reads in a specified amount of shorts.StringreadString(InputStream in)Reads a char array and returns it as a String.StringreadString(InputStream in, int bytes, String encoding)Reads a byte vector of specific length and returns it as a String in a particular encoding.StringreadString(InputStream in, int bytes, Charset charset)Reads a byte vector of specific length and returns it as a String in a particular encoding.StringreadString(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.StringreadString(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[]readStringArray(InputStream in)Reads in an array of strings.shortreadUnsignedByte(InputStream in)Reads in a single byte, cast to a short to eliminate sign.longreadUnsignedInt(InputStream in)Reads in a integer, cast to a long, discarding sign.intreadUnsignedShort(InputStream in)Reads in a short, cast to an integer, discarding sign.intreadVariableLengthInt(InputStream in)Reads an integer from an input stream that is variable-length encoded.longreadVariableLengthLong(InputStream in)Reads a long from an input stream that is variable-length encoded.booleanseekToPattern(InputStream in, byte[] b)Keeps reading until it hits a specific byte pattern.voidsetEndianMode(boolean mode)Sets the byte endian mode for the byte conversion methods.
-
-
-
Field Detail
-
LITTLE_ENDIAN
public static final boolean LITTLE_ENDIAN
- See Also:
- Constant Field Values
-
BIG_ENDIAN
public static final boolean BIG_ENDIAN
- See Also:
- Constant Field Values
-
-
Method Detail
-
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
protected int byteRead(InputStream in) throws IOException
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
protected int byteRead(InputStream in, byte[] b) throws IOException
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
protected int byteRead(InputStream in, byte[] b, int maxlen) throws IOException
Reads a series of bytes from the bound stream into a byte array until end of stream is reached ormaxlenbytes 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
public boolean seekToPattern(InputStream in, byte[] b) throws IOException
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
public boolean readFor(InputStream in, byte[] b) throws IOException
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
public byte[] readByteArray(InputStream in) throws IOException
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
public String readString(InputStream in) throws IOException
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
public String readString(InputStream in, String encoding) throws IOException
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
public String readString(InputStream in, Charset charset) throws IOException
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
public String readString(InputStream in, int bytes, String encoding) throws IOException
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
public String readString(InputStream in, int bytes, Charset charset) throws IOException
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
public String[] readStringArray(InputStream in) throws IOException
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
public boolean readBoolean(InputStream in) throws IOException
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
public boolean[] readBooleanArray(InputStream in) throws IOException
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
public long readLong(InputStream in) throws IOException
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
public long[] readLongs(InputStream in, int n) throws IOException
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
public long[] readLongArray(InputStream in) throws IOException
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
public byte readByte(InputStream in) throws IOException
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
public short readUnsignedByte(InputStream in) throws IOException
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
public int readBytes(InputStream in, byte[] b) throws IOException
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
public int readBytes(InputStream in, byte[] b, int maxlen) throws IOException
Reads a series of bytes from the bound stream into a byte array until end of stream is reached ormaxlenbytes 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
public byte[] readBytes(InputStream in, int n) throws IOException
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
public long readUnsignedInt(InputStream in) throws IOException
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
public int readInt(InputStream in) throws IOException
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
public int read24BitInt(InputStream in) throws IOException
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
public int[] readInts(InputStream in, int n) throws IOException
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
public int[] readIntArray(InputStream in) throws IOException
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
public float readFloat(InputStream in) throws IOException
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
public float[] readFloats(InputStream in, int n) throws IOException
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
public float[] readFloatArray(InputStream in) throws IOException
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
public double readDouble(InputStream in) throws IOException
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
public double[] readDoubles(InputStream in, int n) throws IOException
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
public double[] readDoubleArray(InputStream in) throws IOException
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
public short readShort(InputStream in) throws IOException
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
public int readUnsignedShort(InputStream in) throws IOException
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
public short[] readShorts(InputStream in, int n) throws IOException
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
public short[] readShortArray(InputStream in) throws IOException
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
public char readChar(InputStream in) throws IOException
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
public char[] readChars(InputStream in, int n) throws IOException
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
public char[] readCharArray(InputStream in) throws IOException
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
public int readVariableLengthInt(InputStream in) throws IOException
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
public long readVariableLengthLong(InputStream in) throws IOException
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.
-
-