Class Coding


  • public final class Coding
    extends Object
    General encoding/decoding utility functions
    Author:
    Roman Gershman, Eyal Schneider
    • Constructor Detail

      • Coding

        public Coding()
    • Method Detail

      • read

        public static byte[] read​(InputStream is,
                                  int bytesCount)
                           throws IOException
        Reads a given number of bytes from the stream into a new buffer
        Parameters:
        is - The input stream
        bytesCount - The number of bytes to read from the stream
        Returns:
        The byte array that has been read
        Throws:
        IOException - In case of a read error
        EOFException - if the stream doesn't contain the requested number of bytes
      • read

        public static void read​(InputStream is,
                                byte[] buffer,
                                int offset,
                                int bytesCount)
                         throws IOException
        Reads a given number of bytes from the stream into a given buffer
        Parameters:
        is - The input stream
        buffer - The buffer to read into
        offset - The offset in the given buffer to write from
        bytesCount - The number of bytes to read from the stream
        Throws:
        IOException - In case of a read error
        EOFException - if the stream doesn't contain the requested number of bytes
      • tryRead

        public static boolean tryRead​(InputStream is,
                                      byte[] buffer,
                                      int offset,
                                      int bytesCount)
                               throws IOException
        Tries to read a given number of bytes from the stream into a given buffer. If EOF is encountered, returns false to indicate it. However, if only part of the expected bytes are available, throws EOFException.
        Parameters:
        is - The input stream
        buffer - The buffer to read into
        offset - The offset in the given buffer to write from
        bytesCount - The number of bytes to try to read from the stream
        Returns:
        true if the buffer has been filled, and false if the input stream has no more bytes to read.
        Throws:
        IOException - In case of a read error
        EOFException - if the stream isn't in EOF state prior to the call, and doesn't contain requested number of bytes either.
      • read

        public static byte read​(InputStream is)
                         throws IOException
        Reads a single mandatory byte from a given stream
        Parameters:
        is - The input stream to read from
        Returns:
        The read byte
        Throws:
        IOException - in case of IO error
        EOFException - if the stream doesn't contain any more bytes
      • readLittleEndian16

        public static int readLittleEndian16​(InputStream is)
                                      throws IOException
        Reads a 16 bit int from the input stream. LSB are assumed to comes first.
        Parameters:
        is - The input stream to read from
        Returns:
        The 16 bit int read from the input stream
        Throws:
        IOException
      • readLittleEndian16

        public static int readLittleEndian16​(byte[] src,
                                             int offset)
      • getLittleEndian16AsBytes

        public static byte[] getLittleEndian16AsBytes​(int value)
      • writeLittleEndian16

        public static void writeLittleEndian16​(int v,
                                               OutputStream os)
                                        throws IOException
        Writes a 16 bit int into the output stream. LSB comes first.
        Parameters:
        v - The value to write
        os - the output stream to write to
        Throws:
        IOException
      • writeLittleEndian16

        public static void writeLittleEndian16​(int value,
                                               byte[] dest,
                                               int offset)
      • readBigEndian16

        public static int readBigEndian16​(InputStream is)
                                   throws IOException
        Reads a 16 bit int from the input stream. MSB are assumed to comes first.
        Parameters:
        is - The input stream to read from
        Returns:
        The 16 bit int read from the input stream
        Throws:
        IOException
      • readBigEndian16

        public static int readBigEndian16​(byte[] src,
                                          int offset)
      • getBigEndian16AsBytes

        public static byte[] getBigEndian16AsBytes​(int value)
      • writeBigEndian16

        public static void writeBigEndian16​(int v,
                                            OutputStream os)
                                     throws IOException
        Writes a 16 bit int into the output stream. MSB comes first.
        Parameters:
        v - The value to write
        os - the output stream to write to
        Throws:
        IOException
      • writeBigEndian16

        public static void writeBigEndian16​(int value,
                                            byte[] dest,
                                            int offset)
      • readLittleEndian32

        public static int readLittleEndian32​(InputStream is)
                                      throws IOException
        Reads a 32 bit int from the input stream. LSB are assumed to comes first.
        Parameters:
        is - The input stream to read from
        Returns:
        The 32 bit int read from the input stream
        Throws:
        IOException
      • readLittleEndian32

        public static int readLittleEndian32​(byte[] src,
                                             int offset)
      • getLittleEndian32AsBytes

        public static byte[] getLittleEndian32AsBytes​(int value)
      • writeLittleEndian32

        public static void writeLittleEndian32​(int v,
                                               OutputStream os)
                                        throws IOException
        Writes a 32 bit int into the output stream. LSB comes first.
        Parameters:
        v - The value to write
        os - the output stream to write to
        Throws:
        IOException
      • writeLittleEndian32

        public static void writeLittleEndian32​(int value,
                                               byte[] dest,
                                               int offset)
      • readBigEndian32

        public static int readBigEndian32​(InputStream is)
                                   throws IOException
        Reads a 32 bit int from the input stream. MSB are assumed to comes first.
        Parameters:
        is - The input stream to read from
        Returns:
        The 32 bit int read from the input stream
        Throws:
        IOException
      • readBigEndian32

        public static int readBigEndian32​(byte[] src,
                                          int offset)
      • getBigEndian32AsBytes

        public static byte[] getBigEndian32AsBytes​(int value)
      • writeBigEndian32

        public static void writeBigEndian32​(int v,
                                            OutputStream os)
                                     throws IOException
        Writes a 32 bit int into the output stream. MSB comes first.
        Parameters:
        v - The value to write
        os - the output stream to write to
        Throws:
        IOException
      • writeBigEndian32

        public static void writeBigEndian32​(int value,
                                            byte[] dest,
                                            int offset)
      • readLittleEndian64

        public static long readLittleEndian64​(InputStream is)
                                       throws IOException
        Reads a 64 bit int from the input stream. LSB are assumed to comes first.
        Parameters:
        is - The input stream to read from
        Returns:
        The 64 bit int read from the input stream
        Throws:
        IOException
      • readLittleEndian64

        public static long readLittleEndian64​(byte[] src,
                                              int offset)
      • getLittleEndian64AsBytes

        public static byte[] getLittleEndian64AsBytes​(long value)
      • writeLittleEndian64

        public static void writeLittleEndian64​(long v,
                                               OutputStream os)
                                        throws IOException
        Writes a 64 bit int into the output stream. LSB comes first.
        Parameters:
        v - The value to write
        os - the output stream to write to
        Throws:
        IOException
      • writeLittleEndian64

        public static void writeLittleEndian64​(long value,
                                               byte[] dest,
                                               int offset)
      • readBigEndian64

        public static long readBigEndian64​(InputStream is)
                                    throws IOException
        Reads a 64 bit int from the input stream. MSB are assumed to comes first.
        Parameters:
        is - The input stream to read from
        Returns:
        The 64 bit long read from the input stream
        Throws:
        IOException - In case of IO error or if no 8 bytes were found
      • readBigEndian64

        public static long readBigEndian64​(byte[] src,
                                           int offset)
      • getBigEndian64AsBytes

        public static byte[] getBigEndian64AsBytes​(long value)
      • writeBigEndian64

        public static void writeBigEndian64​(long v,
                                            OutputStream os)
                                     throws IOException
        Writes a 64 bit int into the output stream. MSB comes first.
        Parameters:
        v - The value to write
        os - the output stream to write to
        Throws:
        IOException
      • writeBigEndian64

        public static void writeBigEndian64​(long value,
                                            byte[] dest,
                                            int offset)
      • computeVarint32Size

        public static byte computeVarint32Size​(int value)
        Compute the number of bytes that would be needed to encode a varint. value is treated as unsigned, so it won't be sign-extended if negative.
      • writeVarint32

        public static int writeVarint32​(int value,
                                        byte[] dest,
                                        int offset)
        Parameters:
        value -
        dest -
        offset -
        Returns:
        new offset to the dest after the value was written.
        Throws:
        IOException
      • readVarint32

        public static int readVarint32​(InputStream is,
                                       org.apache.commons.lang3.mutable.MutableInt length)
                                throws IOException
        Reads a varint from a given input stream.
        Parameters:
        is - The input stream to read from
        length - Output parameter - The number of bytes composing the varint
        Returns:
        the varint value
        Throws:
        IOException - in case of an illegal varint encoding or IO error while reading from stream
      • readVarint32

        public static int readVarint32​(byte[] src,
                                       org.apache.commons.lang3.mutable.MutableInt offset)
                                throws IOException
        Reads a varint from a given byte array
        Parameters:
        src - The array to read from
        offset - The offset to read from. This value is updated according to the actual number of bytes read.
        Returns:
        the varint value
        Throws:
        IOException - in case of an illegal varint encoding
      • fromHex

        public static byte[] fromHex​(String hex)
                              throws org.apache.commons.codec.DecoderException
        convert hex to byte array
        Parameters:
        hex -
        Returns:
        hex as byte array
        Throws:
        org.apache.commons.codec.DecoderException
      • toHex

        public static String toHex​(byte[] raw)
        convert byte array to hex
        Parameters:
        raw -
        Returns:
        String