Class ByteBufferUtil

java.lang.Object
org.apache.cassandra.utils.ByteBufferUtil

public class ByteBufferUtil extends Object
Utility methods to make ByteBuffers less painful The following should illustrate the different ways byte buffers can be used public void testArrayOffet() { byte[] b = "test_slice_array".getBytes(); ByteBuffer bb = ByteBuffer.allocate(1024); assert bb.position() == 0; assert bb.limit() == 1024; assert bb.capacity() == 1024; bb.put(b); assert bb.position() == b.length; assert bb.remaining() == bb.limit() - bb.position(); ByteBuffer bb2 = bb.slice(); assert bb2.position() == 0; //slice should begin at other buffers current position assert bb2.arrayOffset() == bb.position(); //to match the position in the underlying array one needs to //track arrayOffset assert bb2.limit()+bb2.arrayOffset() == bb.limit(); assert bb2.remaining() == bb.remaining(); } }
  • Field Details

    • EMPTY_BYTE_BUFFER

      public static final ByteBuffer EMPTY_BYTE_BUFFER
    • UNSET_BYTE_BUFFER

      public static final ByteBuffer UNSET_BYTE_BUFFER
      Represents an unset value in bound variables
    • EMPTY_ARRAY

      public static final ByteBuffer[] EMPTY_ARRAY
  • Constructor Details

    • ByteBufferUtil

      public ByteBufferUtil()
  • Method Details

    • compareUnsigned

      @Inline public static int compareUnsigned(ByteBuffer o1, ByteBuffer o2)
    • compare

      @Inline public static int compare(byte[] o1, ByteBuffer o2)
    • compare

      @Inline public static int compare(ByteBuffer o1, byte[] o2)
    • compare

      public static int compare(ByteBuffer o1, int s1, int l1, byte[] o2)
    • compare

      public static int compare(byte[] o1, ByteBuffer o2, int s2, int l2)
    • string

      public static String string(ByteBuffer buffer) throws CharacterCodingException
      Decode a String representation. This method assumes that the encoding charset is UTF_8.
      Parameters:
      buffer - a byte buffer holding the string representation
      Returns:
      the decoded string
      Throws:
      CharacterCodingException
    • string

      public static String string(ByteBuffer buffer, int position, int length) throws CharacterCodingException
      Decode a String representation. This method assumes that the encoding charset is UTF_8.
      Parameters:
      buffer - a byte buffer holding the string representation
      position - the starting position in buffer to start decoding from
      length - the number of bytes from buffer to use
      Returns:
      the decoded string
      Throws:
      CharacterCodingException
    • string

      public static String string(ByteBuffer buffer, int position, int length, Charset charset) throws CharacterCodingException
      Decode a String representation.
      Parameters:
      buffer - a byte buffer holding the string representation
      position - the starting position in buffer to start decoding from
      length - the number of bytes from buffer to use
      charset - the String encoding charset
      Returns:
      the decoded string
      Throws:
      CharacterCodingException
    • string

      public static String string(ByteBuffer buffer, Charset charset) throws CharacterCodingException
      Decode a String representation.
      Parameters:
      buffer - a byte buffer holding the string representation
      charset - the String encoding charset
      Returns:
      the decoded string
      Throws:
      CharacterCodingException
    • getArray

      public static byte[] getArray(ByteBuffer buffer)
      You should almost never use this. Instead, use the write* methods to avoid copies.
    • getArray

      public static byte[] getArray(ByteBuffer buffer, int position, int length)
      You should almost never use this. Instead, use the write* methods to avoid copies.
    • lastIndexOf

      public static int lastIndexOf(ByteBuffer buffer, byte valueToFind, int startIndex)
      ByteBuffer adaptation of org.apache.commons.lang3.ArrayUtils.lastIndexOf method
      Parameters:
      buffer - the array to traverse for looking for the object, may be null
      valueToFind - the value to find
      startIndex - the start index (i.e. BB position) to travers backwards from
      Returns:
      the last index (i.e. BB position) of the value within the array [between buffer.position() and buffer.limit()]; -1 if not found.
    • bytes

      public static ByteBuffer bytes(String s)
      Encode a String in a ByteBuffer using UTF_8.
      Parameters:
      s - the string to encode
      Returns:
      the encoded string
    • bytes

      public static ByteBuffer bytes(String s, Charset charset)
      Encode a String in a ByteBuffer using the provided charset.
      Parameters:
      s - the string to encode
      charset - the String encoding charset to use
      Returns:
      the encoded string
    • clone

      public static ByteBuffer clone(ByteBuffer buffer)
      Returns:
      a new copy of the data in @param buffer USUALLY YOU SHOULD USE ByteBuffer.duplicate() INSTEAD, which creates a new Buffer (so you can mutate its position without affecting the original) without copying the underlying array.
    • copyBytes

      public static void copyBytes(ByteBuffer src, int srcPos, byte[] dst, int dstPos, int length)
      Transfer bytes from a ByteBuffer to byte array.
      Parameters:
      src - the source ByteBuffer
      srcPos - starting position in the source ByteBuffer
      dst - the destination byte array
      dstPos - starting position in the destination byte array
      length - the number of bytes to copy
    • copyBytes

      public static void copyBytes(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length)
      Transfer bytes from one ByteBuffer to another. This function acts as System.arrayCopy() but for ByteBuffers, and operates safely on direct memory.
      Parameters:
      src - the source ByteBuffer
      srcPos - starting position in the source ByteBuffer
      dst - the destination ByteBuffer
      dstPos - starting position in the destination ByteBuffer
      length - the number of bytes to copy
    • put

      public static int put(ByteBuffer src, ByteBuffer trg)
    • writeZeroes

      public static void writeZeroes(ByteBuffer dest, int count)
    • writeWithLength

      public static void writeWithLength(ByteBuffer bytes, DataOutputPlus out) throws IOException
      Throws:
      IOException
    • writeWithVIntLength

      public static void writeWithVIntLength(ByteBuffer bytes, DataOutputPlus out) throws IOException
      Throws:
      IOException
    • writeWithShortLength

      public static void writeWithShortLength(ByteBuffer buffer, DataOutputPlus out) throws IOException
      Throws:
      IOException
    • readWithLength

      public static ByteBuffer readWithLength(DataInput in) throws IOException
      Throws:
      IOException
    • readWithVIntLength

      public static ByteBuffer readWithVIntLength(DataInputPlus in) throws IOException
      Throws:
      IOException
    • serializedSizeWithLength

      public static int serializedSizeWithLength(ByteBuffer buffer)
    • serializedSizeWithVIntLength

      public static int serializedSizeWithVIntLength(ByteBuffer buffer)
    • skipWithVIntLength

      public static void skipWithVIntLength(DataInputPlus in) throws IOException
      Throws:
      IOException
    • readShortLength

      public static int readShortLength(DataInput in) throws IOException
      Throws:
      IOException
    • readWithShortLength

      public static ByteBuffer readWithShortLength(DataInput in) throws IOException
      Parameters:
      in - data input
      Returns:
      An unsigned short in an integer.
      Throws:
      IOException - if an I/O error occurs.
    • serializedSizeWithShortLength

      public static int serializedSizeWithShortLength(ByteBuffer buffer)
    • skipShortLength

      public static void skipShortLength(DataInputPlus in) throws IOException
      Parameters:
      in - data input
      Throws:
      IOException - if an I/O error occurs.
    • read

      public static ByteBuffer read(DataInput in, int length) throws IOException
      Throws:
      IOException
    • readBytes

      public static byte[] readBytes(DataInput in, int length) throws IOException
      Throws:
      IOException
    • readBytesWithLength

      public static byte[] readBytesWithLength(DataInput in) throws IOException
      Throws:
      IOException
    • toInt

      public static int toInt(ByteBuffer bytes)
      Convert a byte buffer to an integer. Does not change the byte buffer position.
      Parameters:
      bytes - byte buffer to convert to integer
      Returns:
      int representation of the byte buffer
    • toShort

      public static short toShort(ByteBuffer bytes)
      Convert a byte buffer to a short. Does not change the byte buffer position.
      Parameters:
      bytes - byte buffer to convert to short
      Returns:
      short representation of the byte buffer
    • toByte

      public static byte toByte(ByteBuffer bytes)
      Convert a byte buffer to a short. Does not change the byte buffer position.
      Parameters:
      bytes - byte buffer to convert to byte
      Returns:
      byte representation of the byte buffer
    • toLong

      public static long toLong(ByteBuffer bytes)
    • toFloat

      public static float toFloat(ByteBuffer bytes)
    • getFloat

      public static float getFloat(ByteBuffer bytes, int offset)
    • toDouble

      public static double toDouble(ByteBuffer bytes)
    • objectToBytes

      public static ByteBuffer objectToBytes(Object obj)
    • bytes

      public static ByteBuffer bytes(byte b)
    • bytes

      public static ByteBuffer bytes(short s)
    • bytes

      public static ByteBuffer bytes(int i)
    • bytes

      public static ByteBuffer bytes(long n)
    • bytes

      public static ByteBuffer bytes(float f)
    • bytes

      public static ByteBuffer bytes(double d)
    • inputStream

      public static InputStream inputStream(ByteBuffer bytes)
    • bytesToHex

      public static String bytesToHex(ByteBuffer bytes)
    • hexToBytes

      public static ByteBuffer hexToBytes(String str)
    • compareSubArrays

      public static int compareSubArrays(ByteBuffer bytes1, int offset1, ByteBuffer bytes2, int offset2, int length)
      Compare two ByteBuffer at specified offsets for length. Compares the non equal bytes as unsigned.
      Parameters:
      bytes1 - First byte buffer to compare.
      offset1 - Position to start the comparison at in the first array.
      bytes2 - Second byte buffer to compare.
      offset2 - Position to start the comparison at in the second array.
      length - How many bytes to compare?
      Returns:
      -1 if byte1 is less than byte2, 1 if byte2 is less than byte1 or 0 if equal.
    • bytes

      public static ByteBuffer bytes(InetAddress address)
    • bytes

      public static ByteBuffer bytes(UUID uuid)
    • bytes

      public static ByteBuffer bytes(TimeUUID uuid)
    • isPrefix

      public static boolean isPrefix(ByteBuffer prefix, ByteBuffer value)
    • canMinimize

      public static boolean canMinimize(ByteBuffer buf)
    • minimalBufferFor

      public static ByteBuffer minimalBufferFor(ByteBuffer buf)
      trims size of bytebuffer to exactly number of bytes in it, to do not hold too much memory
    • minimizeBuffers

      public static ByteBuffer[] minimizeBuffers(ByteBuffer[] src)
    • canMinimize

      public static boolean canMinimize(ByteBuffer[] src)
    • getUnsignedShort

      public static int getUnsignedShort(ByteBuffer bb, int position)
    • getShortLength

      public static int getShortLength(ByteBuffer bb, int position)
    • readShortLength

      public static int readShortLength(ByteBuffer bb)
    • writeShortLength

      public static void writeShortLength(ByteBuffer bb, int length)
    • readBytes

      public static ByteBuffer readBytes(ByteBuffer bb, int length)
    • readBytesWithShortLength

      public static ByteBuffer readBytesWithShortLength(ByteBuffer bb)
    • ensureCapacity

      public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize)
      Ensure buf is large enough for outputLength. If not, it is cleaned up and a new buffer is allocated; else; buffer has it's position/limit set appropriately.
      Parameters:
      buf - buffer to test the size of; may be null, in which case, a new buffer is allocated.
      outputLength - the minimum target size of the buffer
      allowBufferResize - true if resizing (reallocating) the buffer is allowed
      Returns:
      buf if it was large enough, else a newly allocated buffer.
    • ensureCapacity

      public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize, BufferType bufferType)
      Ensure buf is large enough for outputLength. If not, it is cleaned up and a new buffer is allocated; else; buffer has it's position/limit set appropriately.
      Parameters:
      buf - buffer to test the size of; may be null, in which case, a new buffer is allocated.
      outputLength - the minimum target size of the buffer
      allowBufferResize - true if resizing (reallocating) the buffer is allowed
      bufferType - on- or off- heap byte buffer
      Returns:
      buf if it was large enough, else a newly allocated buffer.
    • contains

      public static boolean contains(ByteBuffer buffer, ByteBuffer subBuffer)
      Check is the given buffer contains a given sub-buffer.
      Parameters:
      buffer - The buffer to search for sequence of bytes in.
      subBuffer - The buffer to match.
      Returns:
      true if buffer contains sub-buffer, false otherwise.
    • startsWith

      public static boolean startsWith(ByteBuffer src, ByteBuffer prefix)
    • endsWith

      public static boolean endsWith(ByteBuffer src, ByteBuffer suffix)
    • equalsWithShortLength

      public static boolean equalsWithShortLength(DataInput in, ByteBuffer toMatch) throws IOException
      Returns true if the buffer at the current position in the input matches given buffer. If true, the input is positioned at the end of the consumed buffer. If false, the position of the input is undefined.

      The matched buffer is unchanged

      Throws:
      IOException
    • readFully

      public static void readFully(FileChannel channel, ByteBuffer dst, long position) throws IOException
      Throws:
      IOException