Package eu.woolplatform.utils.io
Class BinaryReader
- java.lang.Object
-
- eu.woolplatform.utils.io.BinaryReader
-
public class BinaryReader extends Object
A binary reader can read elementary data types from an input stream. It can read data that has been written with aBinaryWriter. The read methods throw anEOFExceptionif the end of the stream is reached. If the stream comes from a file, this is probably the end of the file. Otherwise it probably means that the connection to the stream was lost.After performing a read() method, you can get the number of bytes that have been read with
getReadSize().
-
-
Constructor Summary
Constructors Constructor Description BinaryReader(InputStream in)Constructs a new binary reader that will read from the specified input stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetReadSize()Returns the number of bytes that were read during the last read() method, if the method was successful.booleanreadBoolean()Reads a boolean from one byte.bytereadByte()Reads a byte.doublereadDouble()Reads a double from 64 bits in big-endian order.floatreadFloat()Reads a float from 32 bits in big-endian order.intreadInt()Reads a 32-bit signed integer in big-endian order and returns its value as an int.longreadLong()Reads a 64-bit signed long in big-endian order and returns its value as a long.shortreadShort()Reads a 16-bit signed short in big-endian order and returns its value as a short.StringreadShortString()Reads a string.StringreadString()Reads a string.shortreadUnsignedByte()Reads an unsigned byte and returns its value as a short.longreadUnsignedInt()Reads a 32-bit unsigned int in big-endian order and returns its value as a long.intreadUnsignedShort()Reads a 16-bit unsigned short in big-endian order and returns its value as an int.
-
-
-
Constructor Detail
-
BinaryReader
public BinaryReader(InputStream in)
Constructs a new binary reader that will read from the specified input stream.- Parameters:
in- the input stream
-
-
Method Detail
-
getReadSize
public int getReadSize()
Returns the number of bytes that were read during the last read() method, if the method was successful.- Returns:
- the number of bytes that were read
-
readString
public String readString() throws EOFException, IOException
Reads a string. It will first read an integer with the length of the encoded string. Then it reads the encoded string and decodes it with UTF-8.- Returns:
- the string
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readShortString
public String readShortString() throws EOFException, IOException
Reads a string. It will first read an unsigned short with the length of the encoded string. Then it reads the encoded string and decodes it with UTF-8.- Returns:
- the string
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readByte
public byte readByte() throws EOFException, IOExceptionReads a byte.- Returns:
- the byte
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readUnsignedByte
public short readUnsignedByte() throws EOFException, IOExceptionReads an unsigned byte and returns its value as a short.- Returns:
- the unsigned byte
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readShort
public short readShort() throws EOFException, IOExceptionReads a 16-bit signed short in big-endian order and returns its value as a short.- Returns:
- the signed short
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readUnsignedShort
public int readUnsignedShort() throws EOFException, IOExceptionReads a 16-bit unsigned short in big-endian order and returns its value as an int.- Returns:
- the unsigned short
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readInt
public int readInt() throws EOFException, IOExceptionReads a 32-bit signed integer in big-endian order and returns its value as an int.- Returns:
- the signed integer
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readUnsignedInt
public long readUnsignedInt() throws EOFException, IOExceptionReads a 32-bit unsigned int in big-endian order and returns its value as a long.- Returns:
- the unsigned int
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readLong
public long readLong() throws EOFException, IOExceptionReads a 64-bit signed long in big-endian order and returns its value as a long.- Returns:
- the signed long
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readFloat
public float readFloat() throws EOFException, IOExceptionReads a float from 32 bits in big-endian order.- Returns:
- the float
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readDouble
public double readDouble() throws EOFException, IOExceptionReads a double from 64 bits in big-endian order.- Returns:
- the double
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs
-
readBoolean
public boolean readBoolean() throws EOFException, IOExceptionReads a boolean from one byte. It returns true for byte 0x01 and false for byte 0x00. Otherwise it throws an exception.- Returns:
- the boolean
- Throws:
EOFException- if the end of the stream is reachedIOException- if a reading error occurs or the byte value is invalid for a boolean
-
-