Package com.blackrook.expression.struct
Class SerialWriter
- java.lang.Object
-
- com.blackrook.expression.struct.SerialWriter
-
public class SerialWriter extends Object
Assists in endian writing to an output stream.- Author:
- Matthew Tropiano
-
-
Field Summary
Fields Modifier and Type Field Description static booleanBIG_ENDIANstatic booleanLITTLE_ENDIAN
-
Constructor Summary
Constructors Constructor Description SerialWriter(boolean endianMode)Creates a new serial writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidsetEndianMode(boolean mode)Sets the byte endian mode for the byte conversion methods.voidwriteBoolean(OutputStream out, boolean b)Writes a boolean as a byte.voidwriteBooleanArray(OutputStream out, boolean... b)Writes a boolean array, which is the length of the array as an integer plus each boolean grouped into integer bits.voidwriteByte(OutputStream out, byte b)Writes a byte.voidwriteByteArray(OutputStream out, byte[] b)Writes an array of bytes, which is the length of the array as an integer plus each byte.voidwriteBytes(OutputStream out, byte[] b)Writes a series of bytes.voidwriteChar(OutputStream out, char c)Writes a character.voidwriteCharArray(OutputStream out, char[] c)Writes a character array, which is the length of the array as an integer plus each character.voidwriteDouble(OutputStream out, double d)Writes a 64-bit float.voidwriteDoubleArray(OutputStream out, double[] d)Writes an array of 64-bit floats, which is the length of the array as an integer plus each double.voidwriteFloat(OutputStream out, float f)Writes a 32-bit float.voidwriteFloatArray(OutputStream out, float[] f)Writes an array of 32-bit floats, which is the length of the array as an integer plus each float.voidwriteInt(OutputStream out, int i)Writes an integer.voidwriteIntArray(OutputStream out, int[] i)Writes an integer array, which is the length of the array as an integer plus each integer.voidwriteLong(OutputStream out, long l)Writes a long.voidwriteLongArray(OutputStream out, long[] l)Writes an array of longs, which is the length of the array as an integer plus each long.voidwriteShort(OutputStream out, short s)Writes a short.voidwriteShortArray(OutputStream out, short[] s)Writes an array of shorts, which is the length of the array as an integer plus each short.voidwriteString(OutputStream out, String s)Writes a String.voidwriteString(OutputStream out, String s, String encodingType)Writes a String in a specific encoding.voidwriteString(OutputStream out, String s, Charset charset)Writes a String in a specific encoding.voidwriteStringArray(OutputStream out, String[] s)Writes an array of Strings, which is the length of the array as an integer plus each String.voidwriteUnsignedByte(OutputStream out, int b)Writes an int that is less than 256 to a byte.voidwriteUnsignedByte(OutputStream out, short s)Writes a short that is less than 256 to a byte.voidwriteUnsignedInteger(OutputStream out, long l)Writes a long that is less than 2^32 to an integer.voidwriteUnsignedShort(OutputStream out, int s)Writes an integer, less than 65536, as a short.voidwriteVariableLengthInt(OutputStream out, int i)Converts an integer from an int to a variable-length string of bytes.voidwriteVariableLengthLong(OutputStream out, long i)Converts a long from a long to a variable-length string of bytes.
-
-
-
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) orients values from highest byte to lowest.- Parameters:
mode- an _ENDIAN mode.
-
writeString
public void writeString(OutputStream out, String s) throws IOException
Writes a String.- Parameters:
out- the output stream.s- the String to write.- Throws:
IOException- if an error occurred during the write.
-
writeString
public void writeString(OutputStream out, String s, String encodingType) throws IOException
Writes a String in a specific encoding.- Parameters:
out- the output stream.s- the String to write.encodingType- the encoding type name.- Throws:
IOException- if an error occurred during the write.
-
writeString
public void writeString(OutputStream out, String s, Charset charset) throws IOException
Writes a String in a specific encoding.- Parameters:
out- the output stream.s- the String to write.charset- the encoding charset.- Throws:
IOException- if an error occurred during the write.
-
writeStringArray
public void writeStringArray(OutputStream out, String[] s) throws IOException
Writes an array of Strings, which is the length of the array as an integer plus each String.- Parameters:
out- the output stream.s- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeByte
public void writeByte(OutputStream out, byte b) throws IOException
Writes a byte.- Parameters:
out- the output stream.b- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeUnsignedByte
public void writeUnsignedByte(OutputStream out, short s) throws IOException
Writes a short that is less than 256 to a byte.- Parameters:
out- the output stream.s- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeUnsignedByte
public void writeUnsignedByte(OutputStream out, int b) throws IOException
Writes an int that is less than 256 to a byte.- Parameters:
out- the output stream.b- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeBytes
public void writeBytes(OutputStream out, byte[] b) throws IOException
Writes a series of bytes.- Parameters:
out- the output stream.b- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeByteArray
public void writeByteArray(OutputStream out, byte[] b) throws IOException
Writes an array of bytes, which is the length of the array as an integer plus each byte.- Parameters:
out- the output stream.b- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeBoolean
public void writeBoolean(OutputStream out, boolean b) throws IOException
Writes a boolean as a byte.- Parameters:
out- the output stream.b- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeUnsignedInteger
public void writeUnsignedInteger(OutputStream out, long l) throws IOException
Writes a long that is less than 2^32 to an integer.- Parameters:
out- the output stream.l- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeInt
public void writeInt(OutputStream out, int i) throws IOException
Writes an integer.- Parameters:
out- the output stream.i- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeVariableLengthInt
public void writeVariableLengthInt(OutputStream out, int i) throws IOException
Converts an integer from an int to a variable-length string of bytes. Makes up to four bytes. Due to the nature of this algorithm, it is always written out in a Big-Endian fashion.- Parameters:
out- the output stream.i- the int to convert.- Throws:
IllegalArgumentException- if the int value to convert is above 0x0fffffff.IOException- if an error occurred during the write.
-
writeVariableLengthLong
public void writeVariableLengthLong(OutputStream out, long i) throws IOException
Converts a long from a long to a variable-length string of bytes. Makes up to eight bytes. Due to the nature of this algorithm, it is always written out in a Big-Endian fashion.- Parameters:
out- the output stream.i- the long to convert.- Throws:
IllegalArgumentException- if the long value to convert is above 0x7fffffffffffffffL.IOException- if an error occurred during the write.
-
writeIntArray
public void writeIntArray(OutputStream out, int[] i) throws IOException
Writes an integer array, which is the length of the array as an integer plus each integer.- Parameters:
out- the output stream.i- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeLong
public void writeLong(OutputStream out, long l) throws IOException
Writes a long.- Parameters:
out- the output stream.l- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeLongArray
public void writeLongArray(OutputStream out, long[] l) throws IOException
Writes an array of longs, which is the length of the array as an integer plus each long.- Parameters:
out- the output stream.l- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeFloatArray
public void writeFloatArray(OutputStream out, float[] f) throws IOException
Writes an array of 32-bit floats, which is the length of the array as an integer plus each float.- Parameters:
out- the output stream.f- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeFloat
public void writeFloat(OutputStream out, float f) throws IOException
Writes a 32-bit float.- Parameters:
out- the output stream.f- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeDouble
public void writeDouble(OutputStream out, double d) throws IOException
Writes a 64-bit float.- Parameters:
out- the output stream.d- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeDoubleArray
public void writeDoubleArray(OutputStream out, double[] d) throws IOException
Writes an array of 64-bit floats, which is the length of the array as an integer plus each double.- Parameters:
out- the output stream.d- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeShort
public void writeShort(OutputStream out, short s) throws IOException
Writes a short.- Parameters:
out- the output stream.s- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeUnsignedShort
public void writeUnsignedShort(OutputStream out, int s) throws IOException
Writes an integer, less than 65536, as a short.- Parameters:
out- the output stream.s- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeShortArray
public void writeShortArray(OutputStream out, short[] s) throws IOException
Writes an array of shorts, which is the length of the array as an integer plus each short.- Parameters:
out- the output stream.s- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeChar
public void writeChar(OutputStream out, char c) throws IOException
Writes a character.- Parameters:
out- the output stream.c- the value to write.- Throws:
IOException- if an error occurred during the write.
-
writeCharArray
public void writeCharArray(OutputStream out, char[] c) throws IOException
Writes a character array, which is the length of the array as an integer plus each character.- Parameters:
out- the output stream.c- the array to write.- Throws:
IOException- if an error occurred during the write.
-
writeBooleanArray
public void writeBooleanArray(OutputStream out, boolean... b) throws IOException
Writes a boolean array, which is the length of the array as an integer plus each boolean grouped into integer bits.- Parameters:
out- the output stream.b- the array to write.- Throws:
IOException- if an error occurred during the write.
-
-