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 boolean
BIG_ENDIAN
static boolean
LITTLE_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 void
setEndianMode(boolean mode)
Sets the byte endian mode for the byte conversion methods.void
writeBoolean(OutputStream out, boolean b)
Writes a boolean as a byte.void
writeBooleanArray(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.void
writeByte(OutputStream out, byte b)
Writes a byte.void
writeByteArray(OutputStream out, byte[] b)
Writes an array of bytes, which is the length of the array as an integer plus each byte.void
writeBytes(OutputStream out, byte[] b)
Writes a series of bytes.void
writeChar(OutputStream out, char c)
Writes a character.void
writeCharArray(OutputStream out, char[] c)
Writes a character array, which is the length of the array as an integer plus each character.void
writeDouble(OutputStream out, double d)
Writes a 64-bit float.void
writeDoubleArray(OutputStream out, double[] d)
Writes an array of 64-bit floats, which is the length of the array as an integer plus each double.void
writeFloat(OutputStream out, float f)
Writes a 32-bit float.void
writeFloatArray(OutputStream out, float[] f)
Writes an array of 32-bit floats, which is the length of the array as an integer plus each float.void
writeInt(OutputStream out, int i)
Writes an integer.void
writeIntArray(OutputStream out, int[] i)
Writes an integer array, which is the length of the array as an integer plus each integer.void
writeLong(OutputStream out, long l)
Writes a long.void
writeLongArray(OutputStream out, long[] l)
Writes an array of longs, which is the length of the array as an integer plus each long.void
writeShort(OutputStream out, short s)
Writes a short.void
writeShortArray(OutputStream out, short[] s)
Writes an array of shorts, which is the length of the array as an integer plus each short.void
writeString(OutputStream out, String s)
Writes a String.void
writeString(OutputStream out, String s, String encodingType)
Writes a String in a specific encoding.void
writeString(OutputStream out, String s, Charset charset)
Writes a String in a specific encoding.void
writeStringArray(OutputStream out, String[] s)
Writes an array of Strings, which is the length of the array as an integer plus each String.void
writeUnsignedByte(OutputStream out, int b)
Writes an int that is less than 256 to a byte.void
writeUnsignedByte(OutputStream out, short s)
Writes a short that is less than 256 to a byte.void
writeUnsignedInteger(OutputStream out, long l)
Writes a long that is less than 2^32 to an integer.void
writeUnsignedShort(OutputStream out, int s)
Writes an integer, less than 65536, as a short.void
writeVariableLengthInt(OutputStream out, int i)
Converts an integer from an int to a variable-length string of bytes.void
writeVariableLengthLong(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.
-
-