Class ByteIOUtils


  • @ThreadSafe
    public final class ByteIOUtils
    extends java.lang.Object
    A collection of utility functions to read/write primitive values from/to a byte array.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte readByte​(byte[] buf, int pos)
      Reads a specific byte value from the input byte array at the given offset.
      static byte readByte​(java.nio.ByteBuffer buf, int pos)
      Reads a specific byte value from the input byte buffer at the given offset.
      static int readInt​(byte[] buf, int pos)
      Reads a specific integer byte value (4 bytes) from the input byte array at the given offset.
      static int readInt​(java.nio.ByteBuffer buf, int pos)
      Reads a specific integer byte value (4 bytes) from the input byte buffer at the given offset.
      static long readLong​(byte[] buf, int pos)
      Reads a specific long byte value (8 bytes) from the input byte array at the given offset.
      static short readShort​(byte[] buf, int pos)
      Reads a specific short byte value (2 bytes) from the input byte array at the given offset.
      static void writeByte​(byte[] buf, int pos, byte v)
      Writes a single byte value (1 byte) to the output byte array at the given offset.
      static void writeByte​(java.io.OutputStream out, byte v)
      Writes a single byte value (1 byte) to the output stream.
      static void writeByte​(java.nio.ByteBuffer buf, int pos, byte v)
      Writes a single byte value (1 byte) to the output byte buffer at the given offset.
      static void writeInt​(byte[] buf, int pos, int v)
      Writes a specific integer value (4 bytes) to the output byte array at the given offset.
      static void writeInt​(java.io.OutputStream out, int v)
      Writes a specific integer value (4 bytes) to the output stream.
      static void writeInt​(java.nio.ByteBuffer buf, int pos, int v)
      Writes a specific integer value (4 bytes) to the output byte buffer at the given offset.
      static void writeLong​(byte[] buf, int pos, long v)
      Writes a specific long value (8 bytes) to the output byte array at the given offset.
      static void writeLong​(java.io.OutputStream out, long v)
      Writes a specific long value (8 bytes) to the output stream.
      static void writeShort​(byte[] buf, int pos, short v)
      Writes a specific short value (2 bytes) to the output byte buffer at the given offset.
      static void writeShort​(java.io.OutputStream out, short v)
      Writes a specific short value (2 bytes) to the output stream.
      • Methods inherited from class java.lang.Object

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

      • readByte

        public static byte readByte​(byte[] buf,
                                    int pos)
        Reads a specific byte value from the input byte array at the given offset.
        Parameters:
        buf - input byte array
        pos - offset into the byte buffer to read
        Returns:
        the byte value read
      • readByte

        public static byte readByte​(java.nio.ByteBuffer buf,
                                    int pos)
        Reads a specific byte value from the input byte buffer at the given offset.
        Parameters:
        buf - input byte buffer
        pos - offset into the byte buffer to read
        Returns:
        the byte value read
      • readShort

        public static short readShort​(byte[] buf,
                                      int pos)
        Reads a specific short byte value (2 bytes) from the input byte array at the given offset.
        Parameters:
        buf - input byte buffer
        pos - offset into the byte buffer to read
        Returns:
        the short value read
      • readInt

        public static int readInt​(byte[] buf,
                                  int pos)
        Reads a specific integer byte value (4 bytes) from the input byte array at the given offset.
        Parameters:
        buf - input byte array
        pos - offset into the byte buffer to read
        Returns:
        the int value read
      • readInt

        public static int readInt​(java.nio.ByteBuffer buf,
                                  int pos)
        Reads a specific integer byte value (4 bytes) from the input byte buffer at the given offset.
        Parameters:
        buf - input byte buffer
        pos - offset into the byte buffer to read
        Returns:
        the int value read
      • readLong

        public static long readLong​(byte[] buf,
                                    int pos)
        Reads a specific long byte value (8 bytes) from the input byte array at the given offset.
        Parameters:
        buf - input byte buffer
        pos - offset into the byte buffer to read
        Returns:
        the long value read
      • writeByte

        public static void writeByte​(byte[] buf,
                                     int pos,
                                     byte v)
        Writes a single byte value (1 byte) to the output byte array at the given offset.
        Parameters:
        buf - output byte buffer
        pos - offset into the byte buffer to write
        v - byte value to write
      • writeByte

        public static void writeByte​(java.nio.ByteBuffer buf,
                                     int pos,
                                     byte v)
        Writes a single byte value (1 byte) to the output byte buffer at the given offset.
        Parameters:
        buf - output byte buffer
        pos - offset into the byte buffer to write
        v - byte value to write
      • writeByte

        public static void writeByte​(java.io.OutputStream out,
                                     byte v)
                              throws java.io.IOException
        Writes a single byte value (1 byte) to the output stream. This is equivalent of OutputStream.write(int).
        Parameters:
        out - output stream
        v - byte value to write
        Throws:
        java.io.IOException
      • writeShort

        public static void writeShort​(byte[] buf,
                                      int pos,
                                      short v)
        Writes a specific short value (2 bytes) to the output byte buffer at the given offset.
        Parameters:
        buf - output byte buffer
        pos - offset into the byte buffer to write
        v - short value to write
      • writeShort

        public static void writeShort​(java.io.OutputStream out,
                                      short v)
                               throws java.io.IOException
        Writes a specific short value (2 bytes) to the output stream.
        Parameters:
        out - output stream
        v - short value to write
        Throws:
        java.io.IOException
      • writeInt

        public static void writeInt​(byte[] buf,
                                    int pos,
                                    int v)
        Writes a specific integer value (4 bytes) to the output byte array at the given offset.
        Parameters:
        buf - output byte array
        pos - offset into the byte buffer to write
        v - int value to write
      • writeInt

        public static void writeInt​(java.nio.ByteBuffer buf,
                                    int pos,
                                    int v)
        Writes a specific integer value (4 bytes) to the output byte buffer at the given offset.
        Parameters:
        buf - output byte buffer
        pos - offset into the byte buffer to write
        v - int value to write
      • writeInt

        public static void writeInt​(java.io.OutputStream out,
                                    int v)
                             throws java.io.IOException
        Writes a specific integer value (4 bytes) to the output stream.
        Parameters:
        out - output stream
        v - integer value to write
        Throws:
        java.io.IOException
      • writeLong

        public static void writeLong​(byte[] buf,
                                     int pos,
                                     long v)
        Writes a specific long value (8 bytes) to the output byte array at the given offset.
        Parameters:
        buf - output byte array
        pos - offset into the byte buffer to write
        v - long value to write
      • writeLong

        public static void writeLong​(java.io.OutputStream out,
                                     long v)
                              throws java.io.IOException
        Writes a specific long value (8 bytes) to the output stream.
        Parameters:
        out - output stream
        v - long value to write
        Throws:
        java.io.IOException