Package eu.woolplatform.utils.io
Class BinaryWriter
- java.lang.Object
-
- eu.woolplatform.utils.io.BinaryWriter
-
public class BinaryWriter extends Object
A binary writer can write elementary data types to an output stream. The written data can be read by aBinaryReader.
-
-
Constructor Summary
Constructors Constructor Description BinaryWriter(OutputStream out)Constructs a new binary writer that will write to the specified output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidwriteBoolean(boolean b)Writes the value of a boolean in one byte.voidwriteByte(byte b)Writes a byte.voidwriteDouble(double d)Writes the value of a double in 64 bits in big-endian order.voidwriteFloat(float f)Writes the value of a float in 32 bits in big-endian order.voidwriteInt(int n)Writes the value of an int as a 32-bit signed integer in big-endian order.voidwriteLong(long n)Writes the value of a long as a 64-bit signed long in big-endian order.voidwriteShort(short n)Writes the value of a short as a 16-bit signed short in big-endian order.voidwriteShortString(String s)Writes a string.voidwriteString(String s)Writes a string.voidwriteUnsignedByte(short n)Writes the value of a short as an unsigned byte.voidwriteUnsignedInt(long n)Writes the value of a long as a 32-bit unsigned int in big-endian order.voidwriteUnsignedShort(int n)Writes the value of an int as a 16-bit unsigned short in big-endian order.
-
-
-
Constructor Detail
-
BinaryWriter
public BinaryWriter(OutputStream out)
Constructs a new binary writer that will write to the specified output stream.- Parameters:
out- the output stream
-
-
Method Detail
-
writeString
public void writeString(String s) throws IOException
Writes a string. The string is encoded with UTF-8. This method first writes an integer with the length of the encoded string. Then it writes the encoded string.- Parameters:
s- the string- Throws:
IOException- if a writing error occurs
-
writeShortString
public void writeShortString(String s) throws IOException
Writes a string. The string is encoded with UTF-8. This method first writes an unsigned short with the length of the encoded string. Then it writes the encoded string.- Parameters:
s- the string- Throws:
IOException- if a writing error occurs
-
writeByte
public void writeByte(byte b) throws IOExceptionWrites a byte.- Parameters:
b- the byte- Throws:
IOException- if a writing error occurs
-
writeUnsignedByte
public void writeUnsignedByte(short n) throws IOExceptionWrites the value of a short as an unsigned byte. If the value does not fit in an unsigned byte, then this method throws an IOException.- Parameters:
n- the unsigned short value- Throws:
IOException- if the value does not fit in an unsigned byte or if a writing error occurs
-
writeShort
public void writeShort(short n) throws IOExceptionWrites the value of a short as a 16-bit signed short in big-endian order.- Parameters:
n- the short- Throws:
IOException- if a writing error occurs
-
writeUnsignedShort
public void writeUnsignedShort(int n) throws IOExceptionWrites the value of an int as a 16-bit unsigned short in big-endian order. If the value does not fit in a 16-bit unsigned short, then this method throws an IOException.- Parameters:
n- the unsigned short value- Throws:
IOException- if the value does not fit in a 16-bit unsigned short or if a writing error occurs
-
writeInt
public void writeInt(int n) throws IOExceptionWrites the value of an int as a 32-bit signed integer in big-endian order.- Parameters:
n- the integer- Throws:
IOException- if a writing error occurs
-
writeUnsignedInt
public void writeUnsignedInt(long n) throws IOExceptionWrites the value of a long as a 32-bit unsigned int in big-endian order. If the value does not fit in a 32-bit unsigned int, then this method throws an IOException.- Parameters:
n- the unsigned int value- Throws:
IOException- if the value does not fit in a 32-bit unsigned int or if a writing error occurs
-
writeLong
public void writeLong(long n) throws IOExceptionWrites the value of a long as a 64-bit signed long in big-endian order.- Parameters:
n- the long- Throws:
IOException- if a writing error occurs
-
writeFloat
public void writeFloat(float f) throws IOExceptionWrites the value of a float in 32 bits in big-endian order.- Parameters:
f- the float- Throws:
IOException- if a writing error occurs
-
writeDouble
public void writeDouble(double d) throws IOExceptionWrites the value of a double in 64 bits in big-endian order.- Parameters:
d- the double- Throws:
IOException- if a writing error occurs
-
writeBoolean
public void writeBoolean(boolean b) throws IOExceptionWrites the value of a boolean in one byte. It writes 1 for true, 0 for false.- Parameters:
b- the boolean- Throws:
IOException- if a writing error occurs
-
-