Class 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 a BinaryReader.
    • 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 IOException
        Writes a byte.
        Parameters:
        b - the byte
        Throws:
        IOException - if a writing error occurs
      • writeUnsignedByte

        public void writeUnsignedByte​(short n)
                               throws IOException
        Writes 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 IOException
        Writes 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 IOException
        Writes 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 IOException
        Writes 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 IOException
        Writes 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 IOException
        Writes 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 IOException
        Writes 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 IOException
        Writes 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 IOException
        Writes 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