Class 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 a BinaryWriter. The read methods throw an EOFException if 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 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 reached
        IOException - 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 reached
        IOException - if a reading error occurs
      • readUnsignedByte

        public short readUnsignedByte()
                               throws EOFException,
                                      IOException
        Reads an unsigned byte and returns its value as a short.
        Returns:
        the unsigned byte
        Throws:
        EOFException - if the end of the stream is reached
        IOException - if a reading error occurs
      • readShort

        public short readShort()
                        throws EOFException,
                               IOException
        Reads 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 reached
        IOException - if a reading error occurs
      • readUnsignedShort

        public int readUnsignedShort()
                              throws EOFException,
                                     IOException
        Reads 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 reached
        IOException - if a reading error occurs
      • readInt

        public int readInt()
                    throws EOFException,
                           IOException
        Reads 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 reached
        IOException - if a reading error occurs
      • readUnsignedInt

        public long readUnsignedInt()
                             throws EOFException,
                                    IOException
        Reads 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 reached
        IOException - if a reading error occurs
      • readLong

        public long readLong()
                      throws EOFException,
                             IOException
        Reads 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 reached
        IOException - if a reading error occurs
      • readFloat

        public float readFloat()
                        throws EOFException,
                               IOException
        Reads a float from 32 bits in big-endian order.
        Returns:
        the float
        Throws:
        EOFException - if the end of the stream is reached
        IOException - if a reading error occurs
      • readDouble

        public double readDouble()
                          throws EOFException,
                                 IOException
        Reads a double from 64 bits in big-endian order.
        Returns:
        the double
        Throws:
        EOFException - if the end of the stream is reached
        IOException - if a reading error occurs
      • readBoolean

        public boolean readBoolean()
                            throws EOFException,
                                   IOException
        Reads 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 reached
        IOException - if a reading error occurs or the byte value is invalid for a boolean