Class BitConverter


  • public final class BitConverter
    extends java.lang.Object
    Helper methods for various Number to Bit conversions. IMPORTANT: This class is closed to new additions. While not deprecated (it's good to have similar methods in one place), consider using one of the following instead: - StructuredWritableBuffer or StructuredReadableBuffer implementations, such as ByteArraySegment). These provide the most efficient serialization implementations available. - DirectDataOutput implementations, such as ByteBufferOutputStream). This provides an efficient serialization implementation as well. - DataOutputStream if none of the above are useful (this is the classic Java serializer).
    • Constructor Summary

      Constructors 
      Constructor Description
      BitConverter()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int makeInt​(int b1, int b2, int b3, int b4)
      Composes a 32-bit integer from the given byte components (Big-Endian order).
      static long makeLong​(int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8)
      Composes 64-bit long from the given byte components (in Big Endian order).
      static int readInt​(byte[] source, int position)
      Reads a 32-bit integer from the given byte array starting at the given position.
      static int readInt​(java.io.InputStream source)
      Reads a 32-bit integer from the given InputStream that was encoded using BitConverter.writeInt.
      static long readLong​(byte[] source, int position)
      Reads a 64-bit long from the given byte array starting at the given position.
      static java.util.UUID readUUID​(byte[] source, int position)
      Reads a 128-bit UUID from the given byte array starting at the given position.
      static int writeInt​(byte[] target, int offset, int value)
      Writes the given 32-bit Integer to the given byte array at the given offset.
      static int writeInt​(java.io.OutputStream target, int value)
      Writes the given 32-bit Integer to the given OutputStream.
      static int writeLong​(byte[] target, int offset, long value)
      Writes the given 64-bit Long to the given byte array at the given offset.
      static int writeLong​(java.io.OutputStream target, long value)
      Writes the given 64-bit Long to the given OutputStream.
      static int writeShort​(java.io.OutputStream target, short value)
      Writes the given 16-bit Short to the given OutputStream.
      static void writeUUID​(StructuredWritableBuffer b, java.util.UUID value)
      Writes the given 128-bit UUID to the given StructuredWritableBuffer.
      • Methods inherited from class java.lang.Object

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

      • BitConverter

        public BitConverter()
    • Method Detail

      • writeShort

        public static int writeShort​(java.io.OutputStream target,
                                     short value)
                              throws java.io.IOException
        Writes the given 16-bit Short to the given OutputStream.
        Parameters:
        target - The OutputStream to write to.
        value - The value to write.
        Returns:
        The number of bytes written.
        Throws:
        java.io.IOException - If an error occurred.
      • writeInt

        public static int writeInt​(byte[] target,
                                   int offset,
                                   int value)
        Writes the given 32-bit Integer to the given byte array at the given offset.
        Parameters:
        target - The byte array to write to.
        offset - The offset within the byte array to write at.
        value - The value to write.
        Returns:
        The number of bytes written.
      • writeInt

        public static int writeInt​(java.io.OutputStream target,
                                   int value)
                            throws java.io.IOException
        Writes the given 32-bit Integer to the given OutputStream.
        Parameters:
        target - The OutputStream to write to.
        value - The value to write.
        Returns:
        The number of bytes written.
        Throws:
        java.io.IOException - If an exception got thrown.
      • readInt

        public static int readInt​(byte[] source,
                                  int position)
        Reads a 32-bit integer from the given byte array starting at the given position.
        Parameters:
        source - The byte array to read from.
        position - The position in the byte array to start reading at.
        Returns:
        The read number.
      • readInt

        public static int readInt​(java.io.InputStream source)
                           throws java.io.IOException
        Reads a 32-bit integer from the given InputStream that was encoded using BitConverter.writeInt.
        Parameters:
        source - The InputStream to read from.
        Returns:
        The read number.
        Throws:
        java.io.IOException - If an exception got thrown.
      • makeInt

        public static int makeInt​(int b1,
                                  int b2,
                                  int b3,
                                  int b4)
        Composes a 32-bit integer from the given byte components (Big-Endian order).
        Parameters:
        b1 - Byte #1.
        b2 - Byte #2.
        b3 - Byte #3.
        b4 - Byte #4.
        Returns:
        The composed number.
      • writeLong

        public static int writeLong​(byte[] target,
                                    int offset,
                                    long value)
        Writes the given 64-bit Long to the given byte array at the given offset.
        Parameters:
        target - The byte array to write to.
        offset - The offset within the byte array to write at.
        value - The value to write.
        Returns:
        The number of bytes written.
      • writeLong

        public static int writeLong​(java.io.OutputStream target,
                                    long value)
                             throws java.io.IOException
        Writes the given 64-bit Long to the given OutputStream.
        Parameters:
        target - The OutputStream to write to.
        value - The value to write.
        Returns:
        The number of bytes written.
        Throws:
        java.io.IOException - If an exception got thrown.
      • readUUID

        public static java.util.UUID readUUID​(byte[] source,
                                              int position)
        Reads a 128-bit UUID from the given byte array starting at the given position.
        Parameters:
        source - The byte array to read from.
        position - The position in the byte array to start reading at.
        Returns:
        The read UUID.
      • readLong

        public static long readLong​(byte[] source,
                                    int position)
        Reads a 64-bit long from the given byte array starting at the given position.
        Parameters:
        source - The byte array to read from.
        position - The position in the byte array to start reading at.
        Returns:
        The read number.
      • makeLong

        public static long makeLong​(int b1,
                                    int b2,
                                    int b3,
                                    int b4,
                                    int b5,
                                    int b6,
                                    int b7,
                                    int b8)
        Composes 64-bit long from the given byte components (in Big Endian order).
        Parameters:
        b1 - Byte #1.
        b2 - Byte #2.
        b3 - Byte #3.
        b4 - Byte #4.
        b5 - Byte #5.
        b6 - Byte #6.
        b7 - Byte #7.
        b8 - Byte #8.
        Returns:
        The composed number.