Class BinaryWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    BigEndianBinaryWriter, LittleEndianBinaryWriter

    public abstract class BinaryWriter
    extends java.io.OutputStream
    IO-Optimized binary writer.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.io.OutputStream out  
    • Constructor Summary

      Constructors 
      Constructor Description
      BinaryWriter​(java.io.OutputStream out)  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void write​(byte[] bytes)  
      void write​(byte[] bytes, int off, int len)  
      void write​(int b)  
      int writeBinary​(Binary value)
      Write the content of binary to output stream.
      int writeByte​(byte integer)
      Write a signed byte to the output stream.
      int writeDouble​(double value)
      Write a double value to stream.
      abstract int writeInt​(int integer)
      Write a signed int to the output stream.
      abstract int writeLong​(long integer)
      Write a signed long to the output stream.
      abstract int writeShort​(short integer)
      Write a signed short to the output stream.
      int writeSigned​(int number, int bytes)  
      int writeSigned​(long number, int bytes)  
      abstract int writeUInt16​(int number)  
      abstract int writeUInt24​(int number)  
      abstract int writeUInt32​(int number)  
      int writeUInt8​(int number)  
      int writeUnsigned​(int number, int bytes)  
      int writeVarint​(int varint)
      Write a signed number as varint (integer with variable number of bytes, determined as part of the bytes themselves.
      int writeVarint​(long varint)
      Write a signed number as varint (integer with variable number of bytes, determined as part of the bytes themselves.
      int writeZigzag​(int number)
      Write a long number as zigzag encoded to the stream.
      int writeZigzag​(long number)
      Write a long number as zigzag encoded to the stream.
      • Methods inherited from class java.io.OutputStream

        flush, nullOutputStream
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • out

        protected final java.io.OutputStream out
    • Constructor Detail

      • BinaryWriter

        public BinaryWriter​(java.io.OutputStream out)
    • Method Detail

      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] bytes)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] bytes,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
      • writeByte

        public int writeByte​(byte integer)
                      throws java.io.IOException
        Write a signed byte to the output stream.
        Parameters:
        integer - The number to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeShort

        public abstract int writeShort​(short integer)
                                throws java.io.IOException
        Write a signed short to the output stream.
        Parameters:
        integer - The number to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeInt

        public abstract int writeInt​(int integer)
                              throws java.io.IOException
        Write a signed int to the output stream.
        Parameters:
        integer - The number to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeLong

        public abstract int writeLong​(long integer)
                               throws java.io.IOException
        Write a signed long to the output stream.
        Parameters:
        integer - The number to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeDouble

        public int writeDouble​(double value)
                        throws java.io.IOException
        Write a double value to stream.
        Parameters:
        value - The double value to writeBinary.
        Returns:
        The number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeBinary

        public int writeBinary​(Binary value)
                        throws java.io.IOException
        Write the content of binary to output stream.
        Parameters:
        value - Binary to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeUInt8

        public int writeUInt8​(int number)
                       throws java.io.IOException
        Parameters:
        number - Unsigned byte to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeUInt16

        public abstract int writeUInt16​(int number)
                                 throws java.io.IOException
        Parameters:
        number - Unsigned short to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeUInt24

        public abstract int writeUInt24​(int number)
                                 throws java.io.IOException
        Parameters:
        number - Unsigned short to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeUInt32

        public abstract int writeUInt32​(int number)
                                 throws java.io.IOException
        Parameters:
        number - Unsigned short to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeUnsigned

        public int writeUnsigned​(int number,
                                 int bytes)
                          throws java.io.IOException
        Parameters:
        number - Unsigned integer to writeBinary.
        bytes - Number of bytes to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeSigned

        public int writeSigned​(int number,
                               int bytes)
                        throws java.io.IOException
        Parameters:
        number - Signed integer to writeBinary.
        bytes - Number of bytes to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeSigned

        public int writeSigned​(long number,
                               int bytes)
                        throws java.io.IOException
        Parameters:
        number - Signed integer to writeBinary.
        bytes - Number of bytes to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeZigzag

        public int writeZigzag​(int number)
                        throws java.io.IOException
        Write a long number as zigzag encoded to the stream. The least significant bit becomes the sign, and the actual value is mad absolute and shifted one bit. This makes it maximum compressed both when positive and negative.
        Parameters:
        number - The number to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeZigzag

        public int writeZigzag​(long number)
                        throws java.io.IOException
        Write a long number as zigzag encoded to the stream. The least significant bit becomes the sign, and the actual value is mad absolute and shifted one bit. This makes it maximum compressed both when positive and negative.
        Parameters:
        number - The number to writeBinary.
        Returns:
        Number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeVarint

        public int writeVarint​(int varint)
                        throws java.io.IOException
        Write a signed number as varint (integer with variable number of bytes, determined as part of the bytes themselves.
        Parameters:
        varint - The number to writeBinary.
        Returns:
        The number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.
      • writeVarint

        public int writeVarint​(long varint)
                        throws java.io.IOException
        Write a signed number as varint (integer with variable number of bytes, determined as part of the bytes themselves.
        Parameters:
        varint - The number to writeBinary.
        Returns:
        The number of bytes written.
        Throws:
        java.io.IOException - if unable to write to stream.