Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.cassandra.utils
Class ByteBufferUtil

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

    public class ByteBufferUtil

extends java.lang.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 Summary
`static java.nio.ByteBuffer` | `EMPTY_BYTE_BUFFER`

Constructor Summary
`ByteBufferUtil()`

Method Summary
`static void` | `arrayCopy(java.nio.ByteBuffer buffer, int position, byte[] bytes, int offset, int length)`
`static void` | `arrayCopy(java.nio.ByteBuffer src, int srcPos, java.nio.ByteBuffer dst, int dstPos, int length)` Transfer bytes from one ByteBuffer to another.
`static java.nio.ByteBuffer` | `bytes(double d)`
`static java.nio.ByteBuffer` | `bytes(float f)`
`static java.nio.ByteBuffer` | `bytes(int i)`
`static java.nio.ByteBuffer` | `bytes(long n)`
`static java.nio.ByteBuffer` | `bytes(java.lang.String s)` Encode a String in a ByteBuffer using UTF_8.
`static java.nio.ByteBuffer` | `bytes(java.lang.String s, java.nio.charset.Charset charset)` Encode a String in a ByteBuffer using the provided charset.
`static java.lang.String` | `bytesToHex(java.nio.ByteBuffer bytes)`
`static java.nio.ByteBuffer` | `clone(java.nio.ByteBuffer buffer)`
`static int` | `compare(byte[] o1, java.nio.ByteBuffer o2)`
`static int` | `compare(java.nio.ByteBuffer o1, byte[] o2)`
`static int` | `compareSubArrays(java.nio.ByteBuffer bytes1, int offset1, java.nio.ByteBuffer bytes2, int offset2, int length)` Compare two ByteBuffer at specified offsets for length.
`static int` | `compareUnsigned(java.nio.ByteBuffer o1, java.nio.ByteBuffer o2)`
`static byte[]` | `getArray(java.nio.ByteBuffer buffer)` You should almost never use this.
`static java.nio.ByteBuffer` | `hexToBytes(java.lang.String str)`
`static java.io.InputStream` | `inputStream(java.nio.ByteBuffer bytes)`
`static int` | `lastIndexOf(java.nio.ByteBuffer buffer, byte valueToFind, int startIndex)` ByteBuffer adaptation of org.apache.commons.lang.ArrayUtils.lastIndexOf method
`static java.nio.ByteBuffer` | `read(java.io.DataInput in, int length)`
`static int` | `readShortLength(java.io.DataInput in)`
`static java.nio.ByteBuffer` | `readWithLength(java.io.DataInput in)`
`static java.nio.ByteBuffer` | `readWithShortLength(java.io.DataInput in)`
`static java.nio.ByteBuffer` | `skipShortLength(java.io.DataInput in)`
`static java.lang.String` | `string(java.nio.ByteBuffer buffer)` Decode a String representation.
`static java.lang.String` | `string(java.nio.ByteBuffer buffer, java.nio.charset.Charset charset)` Decode a String representation.
`static java.lang.String` | `string(java.nio.ByteBuffer buffer, int position, int length)` Decode a String representation.
`static java.lang.String` | `string(java.nio.ByteBuffer buffer, int position, int length, java.nio.charset.Charset charset)` Decode a String representation.
`static double` | `toDouble(java.nio.ByteBuffer bytes)`
`static float` | `toFloat(java.nio.ByteBuffer bytes)`
`static int` | `toInt(java.nio.ByteBuffer bytes)` Convert a byte buffer to an integer.
`static long` | `toLong(java.nio.ByteBuffer bytes)`
`static void` | `write(java.nio.ByteBuffer buffer, java.io.DataOutput out)`
`static void` | `writeWithLength(byte[] bytes, java.io.DataOutput out)`
`static void` | `writeWithLength(java.nio.ByteBuffer bytes, java.io.DataOutput out)`
`static void` | `writeWithShortLength(java.nio.ByteBuffer buffer, java.io.DataOutput out)`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### EMPTY_BYTE_BUFFER

    public static final java.nio.ByteBuffer EMPTY_BYTE_BUFFER

Constructor Detail

### ByteBufferUtil

    public ByteBufferUtil()

Method Detail

### compareUnsigned

    public static int compareUnsigned(java.nio.ByteBuffer o1,
                                      java.nio.ByteBuffer o2)

### compare

    public static int compare(byte[] o1,
                              java.nio.ByteBuffer o2)

### compare

    public static int compare(java.nio.ByteBuffer o1,
                              byte[] o2)

### string

    public static java.lang.String string(java.nio.ByteBuffer buffer)
                                   throws java.nio.charset.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:
    `java.nio.charset.CharacterCodingException`

### string

    public static java.lang.String string(java.nio.ByteBuffer buffer,
                                          int position,
                                          int length)
                                   throws java.nio.charset.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:
    `java.nio.charset.CharacterCodingException`

### string

    public static java.lang.String string(java.nio.ByteBuffer buffer,
                                          int position,
                                          int length,
                                          java.nio.charset.Charset charset)
                                   throws java.nio.charset.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:
    `java.nio.charset.CharacterCodingException`

### string

    public static java.lang.String string(java.nio.ByteBuffer buffer,
                                          java.nio.charset.Charset charset)
                                   throws java.nio.charset.CharacterCodingException

  Decode a String representation.

  Parameters:
    `buffer` - a byte buffer holding the string representation
    `charset` - the String encoding charset
  Returns:
    the decoded string
  Throws:
    `java.nio.charset.CharacterCodingException`

### getArray

    public static byte[] getArray(java.nio.ByteBuffer buffer)

  You should almost never use this. Instead, use the write* methods to avoid copies.

### lastIndexOf

    public static int lastIndexOf(java.nio.ByteBuffer buffer,
                                  byte valueToFind,
                                  int startIndex)

  ByteBuffer adaptation of org.apache.commons.lang.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 java.nio.ByteBuffer bytes(java.lang.String s)

  Encode a String in a ByteBuffer using UTF_8.

  Parameters:
    `s` - the string to encode
  Returns:
    the encoded string

### bytes

    public static java.nio.ByteBuffer bytes(java.lang.String s,
                                            java.nio.charset.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 java.nio.ByteBuffer clone(java.nio.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.

### arrayCopy

    public static void arrayCopy(java.nio.ByteBuffer buffer,
                                 int position,
                                 byte[] bytes,
                                 int offset,
                                 int length)

### arrayCopy

    public static void arrayCopy(java.nio.ByteBuffer src,
                                 int srcPos,
                                 java.nio.ByteBuffer dst,
                                 int dstPos,
                                 int length)

  Transfer bytes from one ByteBuffer to another. This function acts as System.arrayCopy() but for ByteBuffers.

  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

### writeWithLength

    public static void writeWithLength(java.nio.ByteBuffer bytes,
                                       java.io.DataOutput out)
                                throws java.io.IOException

  Throws:
    `java.io.IOException`

### writeWithLength

    public static void writeWithLength(byte[] bytes,
                                       java.io.DataOutput out)
                                throws java.io.IOException

  Throws:
    `java.io.IOException`

### write

    public static void write(java.nio.ByteBuffer buffer,
                             java.io.DataOutput out)
                      throws java.io.IOException

  Throws:
    `java.io.IOException`

### writeWithShortLength

    public static void writeWithShortLength(java.nio.ByteBuffer buffer,
                                            java.io.DataOutput out)

### readWithLength

    public static java.nio.ByteBuffer readWithLength(java.io.DataInput in)
                                              throws java.io.IOException

  Throws:
    `java.io.IOException`

### readShortLength

    public static int readShortLength(java.io.DataInput in)
                               throws java.io.IOException

  Throws:
    `java.io.IOException`

### readWithShortLength

    public static java.nio.ByteBuffer readWithShortLength(java.io.DataInput in)
                                                   throws java.io.IOException

  Parameters:
    `in` - data input
  Returns:
    An unsigned short in an integer.
  Throws:
    `java.io.IOException` - if an I/O error occurs.

### skipShortLength

    public static java.nio.ByteBuffer skipShortLength(java.io.DataInput in)
                                               throws java.io.IOException

  Parameters:
    `in` - data input
  Returns:
    null
  Throws:
    `java.io.IOException` - if an I/O error occurs.

### read

    public static java.nio.ByteBuffer read(java.io.DataInput in,
                                           int length)
                                    throws java.io.IOException

  Throws:
    `java.io.IOException`

### toInt

    public static int toInt(java.nio.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

### toLong

    public static long toLong(java.nio.ByteBuffer bytes)

### toFloat

    public static float toFloat(java.nio.ByteBuffer bytes)

### toDouble

    public static double toDouble(java.nio.ByteBuffer bytes)

### bytes

    public static java.nio.ByteBuffer bytes(int i)

### bytes

    public static java.nio.ByteBuffer bytes(long n)

### bytes

    public static java.nio.ByteBuffer bytes(float f)

### bytes

    public static java.nio.ByteBuffer bytes(double d)

### inputStream

    public static java.io.InputStream inputStream(java.nio.ByteBuffer bytes)

### bytesToHex

    public static java.lang.String bytesToHex(java.nio.ByteBuffer bytes)

### hexToBytes

    public static java.nio.ByteBuffer hexToBytes(java.lang.String str)

### compareSubArrays

    public static int compareSubArrays(java.nio.ByteBuffer bytes1,
                                       int offset1,
                                       java.nio.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.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2013 The Apache Software Foundation