Class BufferUtils


  • @ThreadSafe
    public final class BufferUtils
    extends java.lang.Object
    Utilities related to buffers, not only ByteBuffer.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int byteToInt​(byte b)
      Converts a byte to an integer.
      static void cleanDirectBuffer​(java.nio.ByteBuffer buffer)
      Forces to unmap a direct buffer if this buffer is no longer used.
      static java.nio.ByteBuffer cloneByteBuffer​(java.nio.ByteBuffer buf)
      Clones a ByteBuffer.
      static java.util.List<java.nio.ByteBuffer> cloneByteBufferList​(java.util.List<java.nio.ByteBuffer> source)
      Clones a list of ByteBuffers.
      static boolean equalConstantByteArray​(byte value, int len, byte[] arr)
      Checks if the given byte array starts with a constant sequence of bytes of the given value and length.
      static boolean equalIncreasingByteArray​(int len, byte[] arr)
      Checks if the given byte array starts with an increasing sequence of bytes of the given length, starting from 0.
      static boolean equalIncreasingByteArray​(int start, int len, byte[] arr)
      Checks if the given byte array starts with an increasing sequence of bytes of the given length, starting from the given value.
      static boolean equalIncreasingByteBuf​(int start, int len, io.netty.buffer.ByteBuf buf)
      Checks if the given ByteBuf starts with an increasing sequence of bytes starting at the given value of length equal to or greater than the given length.
      static boolean equalIncreasingByteBuffer​(int start, int len, java.nio.ByteBuffer buf)
      Checks if the given ByteBuffer starts with an increasing sequence of bytes starting at the given value of length equal to or greater than the given length.
      static void fastCopy​(java.nio.channels.ReadableByteChannel src, java.nio.channels.WritableByteChannel dest)
      An efficient copy between two channels with a fixed-size buffer.
      static byte[] getIncreasingByteArray​(int len)
      Gets an increasing sequence of bytes starting at zero.
      static byte[] getIncreasingByteArray​(int start, int len)
      Gets an increasing sequence of bytes starting with the given value.
      static java.nio.ByteBuffer getIncreasingByteBuffer​(int len)
      Gets a ByteBuffer containing an increasing sequence of bytes starting at zero.
      static java.nio.ByteBuffer getIncreasingByteBuffer​(int start, int len)
      Gets a ByteBuffer containing an increasing sequence of bytes starting at the given value.
      static int intAsUnsignedByteValue​(int i)
      Gets the unsigned byte value of an integer.
      static boolean matchIncreasingByteArray​(int startIndex, int endIndex, byte[] arr)
      Checks if the given byte array has a sub array equals an increasing sequence of bytes, starting from 0.
      static boolean matchIncreasingByteArray​(int startNum, int startIndex, int endIndex, byte[] arr)
      Checks if the given byte array has a sub array equals an increasing sequence of bytes, starting from the given value.
      static boolean matchIncreasingByteBuffer​(int startNum, int startIndex, int endIndex, java.nio.ByteBuffer buf)
      Checks if the given byte buffer has a sub buffer equals an increasing sequence of bytes, starting from the given value.
      static boolean matchIncreasingByteBuffer​(int startIndex, int endIndex, java.nio.ByteBuffer buf)
      Checks if the given byte buffer has a sub buffer equals an increasing sequence of bytes, starting from the given value.
      static byte[] newByteArrayFromByteBuffer​(java.nio.ByteBuffer buf)
      Creates a byte array from the given ByteBuffer, the position property of input ByteBuffer remains unchanged.
      static void putIntByteBuffer​(java.nio.ByteBuffer buf, int b)
      Puts a byte (the first byte of an integer) into a ByteBuffer.
      static java.nio.ByteBuffer sliceByteBuffer​(java.nio.ByteBuffer buffer, int position)
      Convenience method for sliceByteBuffer(ByteBuffer, int, int) where the last parameter is the number of remaining bytes in the new buffer.
      static java.nio.ByteBuffer sliceByteBuffer​(java.nio.ByteBuffer buffer, int position, int length)
      Creates a new ByteBuffer sliced from a given ByteBuffer.
      static boolean startsWithIncreasingByteArray​(int start, int len, byte[] arr)
      Checks if the given byte array contains an increasing sequence of bytes of the given length, starting from the given value.
      static void transfer​(java.nio.channels.ReadableByteChannel src, java.nio.channels.WritableByteChannel dest)
      An efficient copy between two channels with a fixed-size buffer.
      static void writeBufferToFile​(java.lang.String path, byte[] buffer)
      Writes buffer to the given file path.
      • Methods inherited from class java.lang.Object

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

      • byteToInt

        public static int byteToInt​(byte b)
        Converts a byte to an integer.
        Parameters:
        b - the byte to convert
        Returns:
        the integer representation of the byte
      • cleanDirectBuffer

        public static void cleanDirectBuffer​(java.nio.ByteBuffer buffer)
        Forces to unmap a direct buffer if this buffer is no longer used. After calling this method, this direct buffer should be discarded. This is unsafe operation and currently a work-around to avoid huge memory occupation caused by memory map.
        Parameters:
        buffer - bytebuffer
      • cloneByteBuffer

        public static java.nio.ByteBuffer cloneByteBuffer​(java.nio.ByteBuffer buf)
        Clones a ByteBuffer.

        The new bytebuffer will have the same content, but the type of the bytebuffer may not be the same.

        Parameters:
        buf - The ByteBuffer to copy
        Returns:
        The new ByteBuffer
      • cloneByteBufferList

        public static java.util.List<java.nio.ByteBuffer> cloneByteBufferList​(java.util.List<java.nio.ByteBuffer> source)
        Clones a list of ByteBuffers.
        Parameters:
        source - the list of ByteBuffers to copy
        Returns:
        the new list of ByteBuffers
      • putIntByteBuffer

        public static void putIntByteBuffer​(java.nio.ByteBuffer buf,
                                            int b)
        Puts a byte (the first byte of an integer) into a ByteBuffer.
        Parameters:
        buf - ByteBuffer to use
        b - byte to put into the ByteBuffer
      • getIncreasingByteArray

        public static byte[] getIncreasingByteArray​(int len)
        Gets an increasing sequence of bytes starting at zero.
        Parameters:
        len - the target length of the sequence
        Returns:
        an increasing sequence of bytes
      • getIncreasingByteArray

        public static byte[] getIncreasingByteArray​(int start,
                                                    int len)
        Gets an increasing sequence of bytes starting with the given value.
        Parameters:
        start - the starting value to use
        len - the target length of the sequence
        Returns:
        an increasing sequence of bytes
      • equalConstantByteArray

        public static boolean equalConstantByteArray​(byte value,
                                                     int len,
                                                     byte[] arr)
        Checks if the given byte array starts with a constant sequence of bytes of the given value and length.
        Parameters:
        value - the value to check for
        len - the target length of the sequence
        arr - the byte array to check
        Returns:
        true if the byte array has a prefix of length len that is a constant sequence of bytes of the given value
      • equalIncreasingByteArray

        public static boolean equalIncreasingByteArray​(int len,
                                                       byte[] arr)
        Checks if the given byte array starts with an increasing sequence of bytes of the given length, starting from 0.
        Parameters:
        len - the target length of the sequence
        arr - the byte array to check
        Returns:
        true if the byte array has a prefix of length len that is an increasing sequence of bytes starting at zero
      • equalIncreasingByteArray

        public static boolean equalIncreasingByteArray​(int start,
                                                       int len,
                                                       byte[] arr)
        Checks if the given byte array starts with an increasing sequence of bytes of the given length, starting from the given value. The array length must be equal to the length checked.
        Parameters:
        start - the starting value to use
        len - the target length of the sequence
        arr - the byte array to check
        Returns:
        true if the byte array has a prefix of length len that is an increasing sequence of bytes starting at start
      • startsWithIncreasingByteArray

        public static boolean startsWithIncreasingByteArray​(int start,
                                                            int len,
                                                            byte[] arr)
        Checks if the given byte array contains an increasing sequence of bytes of the given length, starting from the given value.
        Parameters:
        start - the starting value to use
        len - the target length of the sequence
        arr - the byte array to check
        Returns:
        true if the byte array has a subarray of length len that is an increasing sequence of bytes starting at start
      • getIncreasingByteBuffer

        public static java.nio.ByteBuffer getIncreasingByteBuffer​(int len)
        Gets a ByteBuffer containing an increasing sequence of bytes starting at zero.
        Parameters:
        len - the target length of the sequence
        Returns:
        ByteBuffer containing an increasing sequence of bytes
      • getIncreasingByteBuffer

        public static java.nio.ByteBuffer getIncreasingByteBuffer​(int start,
                                                                  int len)
        Gets a ByteBuffer containing an increasing sequence of bytes starting at the given value.
        Parameters:
        len - the target length of the sequence
        start - the starting value to use
        Returns:
        ByteBuffer containing an increasing sequence of bytes
      • equalIncreasingByteBuffer

        public static boolean equalIncreasingByteBuffer​(int start,
                                                        int len,
                                                        java.nio.ByteBuffer buf)
        Checks if the given ByteBuffer starts with an increasing sequence of bytes starting at the given value of length equal to or greater than the given length.
        Parameters:
        start - the starting value to use
        len - the target length of the sequence
        buf - the ByteBuffer to check
        Returns:
        true if the ByteBuffer has a prefix of length len that is an increasing sequence of bytes starting at start
      • equalIncreasingByteBuf

        public static boolean equalIncreasingByteBuf​(int start,
                                                     int len,
                                                     io.netty.buffer.ByteBuf buf)
        Checks if the given ByteBuf starts with an increasing sequence of bytes starting at the given value of length equal to or greater than the given length.
        Parameters:
        start - the starting value to use
        len - the target length of the sequence
        buf - the ByteBuffer to check
        Returns:
        true if the ByteBuf has a prefix of length len that is an increasing sequence of bytes starting at start
      • matchIncreasingByteArray

        public static boolean matchIncreasingByteArray​(int startIndex,
                                                       int endIndex,
                                                       byte[] arr)
        Checks if the given byte array has a sub array equals an increasing sequence of bytes, starting from 0.
        Parameters:
        startIndex - the start index in the array to check against
        endIndex - the end index in the array to check against
        arr - the byte array to check
        Returns:
        true if match, false otherwise
      • matchIncreasingByteArray

        public static boolean matchIncreasingByteArray​(int startNum,
                                                       int startIndex,
                                                       int endIndex,
                                                       byte[] arr)
        Checks if the given byte array has a sub array equals an increasing sequence of bytes, starting from the given value.
        Parameters:
        startNum - the starting value to use
        startIndex - the start index in the array to check against
        endIndex - the end index in the array to check against
        arr - the byte array to check
        Returns:
        true if match, false otherwise
      • matchIncreasingByteBuffer

        public static boolean matchIncreasingByteBuffer​(int startIndex,
                                                        int endIndex,
                                                        java.nio.ByteBuffer buf)
        Checks if the given byte buffer has a sub buffer equals an increasing sequence of bytes, starting from the given value.
        Parameters:
        startIndex - the start index in the array to check against
        endIndex - the end index in the array to check against
        buf - the byte buffer to check
        Returns:
        true if match, false otherwise
      • matchIncreasingByteBuffer

        public static boolean matchIncreasingByteBuffer​(int startNum,
                                                        int startIndex,
                                                        int endIndex,
                                                        java.nio.ByteBuffer buf)
        Checks if the given byte buffer has a sub buffer equals an increasing sequence of bytes, starting from the given value.
        Parameters:
        startNum - the starting value to use
        startIndex - the start index in the array to check against
        endIndex - the end index in the array to check against
        buf - the byte buffer to check
        Returns:
        true if match, false otherwise
      • writeBufferToFile

        public static void writeBufferToFile​(java.lang.String path,
                                             byte[] buffer)
                                      throws java.io.IOException
        Writes buffer to the given file path.
        Parameters:
        path - file path to write the data
        buffer - raw data
        Throws:
        java.io.IOException
      • transfer

        public static void transfer​(java.nio.channels.ReadableByteChannel src,
                                    java.nio.channels.WritableByteChannel dest)
                             throws java.io.IOException
        An efficient copy between two channels with a fixed-size buffer.
        Parameters:
        src - the source channel
        dest - the destination channel
        Throws:
        java.io.IOException
      • intAsUnsignedByteValue

        public static int intAsUnsignedByteValue​(int i)
        Gets the unsigned byte value of an integer. Useful for comparing to the result of reading from an input stream.
        Parameters:
        i - the integer to convert
        Returns:
        the integer value after casting as an unsigned byte
      • newByteArrayFromByteBuffer

        public static byte[] newByteArrayFromByteBuffer​(java.nio.ByteBuffer buf)
        Creates a byte array from the given ByteBuffer, the position property of input ByteBuffer remains unchanged.
        Parameters:
        buf - source ByteBuffer
        Returns:
        a newly created byte array
      • sliceByteBuffer

        public static java.nio.ByteBuffer sliceByteBuffer​(java.nio.ByteBuffer buffer,
                                                          int position,
                                                          int length)
        Creates a new ByteBuffer sliced from a given ByteBuffer. The new ByteBuffer shares the content of the existing one, but with independent position/mark/limit. After slicing, the new ByteBuffer has position 0, and the input ByteBuffer is unmodified.
        Parameters:
        buffer - source ByteBuffer to slice
        position - position in the source ByteBuffer to slice
        length - length of the sliced ByteBuffer
        Returns:
        the sliced ByteBuffer
      • sliceByteBuffer

        public static java.nio.ByteBuffer sliceByteBuffer​(java.nio.ByteBuffer buffer,
                                                          int position)
        Convenience method for sliceByteBuffer(ByteBuffer, int, int) where the last parameter is the number of remaining bytes in the new buffer.
        Parameters:
        buffer - source ByteBuffer to slice
        position - position in the source ByteBuffer to slice
        Returns:
        the sliced ByteBuffer
      • fastCopy

        public static void fastCopy​(java.nio.channels.ReadableByteChannel src,
                                    java.nio.channels.WritableByteChannel dest)
                             throws java.io.IOException
        An efficient copy between two channels with a fixed-size buffer.
        Parameters:
        src - the source channel
        dest - the destination channel
        Throws:
        java.io.IOException