Package org.apache.cassandra.utils
Class ByteBufferUtil
java.lang.Object
org.apache.cassandra.utils.ByteBufferUtil
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
FieldsModifier and TypeFieldDescriptionstatic final ByteBuffer[]static final ByteBufferstatic final ByteBufferRepresents an unset value in bound variables -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBufferbytes(byte b) static ByteBufferbytes(double d) static ByteBufferbytes(float f) static ByteBufferbytes(int i) static ByteBufferbytes(long n) static ByteBufferbytes(short s) static ByteBufferEncode a String in a ByteBuffer using UTF_8.static ByteBufferEncode a String in a ByteBuffer using the provided charset.static ByteBufferbytes(InetAddress address) static ByteBufferstatic ByteBufferstatic StringbytesToHex(ByteBuffer bytes) static booleancanMinimize(ByteBuffer buf) static booleancanMinimize(ByteBuffer[] src) static ByteBufferclone(ByteBuffer buffer) static intcompare(byte[] o1, ByteBuffer o2) static intcompare(byte[] o1, ByteBuffer o2, int s2, int l2) static intcompare(ByteBuffer o1, byte[] o2) static intcompare(ByteBuffer o1, int s1, int l1, byte[] o2) static intcompareSubArrays(ByteBuffer bytes1, int offset1, ByteBuffer bytes2, int offset2, int length) Compare two ByteBuffer at specified offsets for length.static intcompareUnsigned(ByteBuffer o1, ByteBuffer o2) static booleancontains(ByteBuffer buffer, ByteBuffer subBuffer) Check is the given buffer contains a given sub-buffer.static voidcopyBytes(ByteBuffer src, int srcPos, byte[] dst, int dstPos, int length) Transfer bytes from a ByteBuffer to byte array.static voidcopyBytes(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length) Transfer bytes from one ByteBuffer to another.static booleanendsWith(ByteBuffer src, ByteBuffer suffix) static ByteBufferensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize) Ensurebufis large enough foroutputLength.static ByteBufferensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize, BufferType bufferType) Ensurebufis large enough foroutputLength.static booleanequalsWithShortLength(DataInput in, ByteBuffer toMatch) Returns true if the buffer at the current position in the input matches given buffer.static byte[]getArray(ByteBuffer buffer) You should almost never use this.static byte[]getArray(ByteBuffer buffer, int position, int length) You should almost never use this.static floatgetFloat(ByteBuffer bytes, int offset) static intgetShortLength(ByteBuffer bb, int position) static intgetUnsignedShort(ByteBuffer bb, int position) static ByteBufferhexToBytes(String str) static InputStreaminputStream(ByteBuffer bytes) static booleanisPrefix(ByteBuffer prefix, ByteBuffer value) static intlastIndexOf(ByteBuffer buffer, byte valueToFind, int startIndex) ByteBuffer adaptation of org.apache.commons.lang3.ArrayUtils.lastIndexOf methodstatic ByteBuffertrims size of bytebuffer to exactly number of bytes in it, to do not hold too much memorystatic ByteBuffer[]minimizeBuffers(ByteBuffer[] src) static ByteBufferobjectToBytes(Object obj) static intput(ByteBuffer src, ByteBuffer trg) static ByteBufferstatic byte[]static ByteBufferreadBytes(ByteBuffer bb, int length) static byte[]static ByteBufferstatic voidreadFully(FileChannel channel, ByteBuffer dst, long position) static intstatic intstatic ByteBufferstatic ByteBufferstatic ByteBufferstatic intserializedSizeWithLength(ByteBuffer buffer) static intstatic intstatic voidstatic voidstatic booleanstartsWith(ByteBuffer src, ByteBuffer prefix) static Stringstring(ByteBuffer buffer) Decode a String representation.static Stringstring(ByteBuffer buffer, int position, int length) Decode a String representation.static Stringstring(ByteBuffer buffer, int position, int length, Charset charset) Decode a String representation.static Stringstring(ByteBuffer buffer, Charset charset) Decode a String representation.static bytetoByte(ByteBuffer bytes) Convert a byte buffer to a short.static doubletoDouble(ByteBuffer bytes) static floattoFloat(ByteBuffer bytes) static inttoInt(ByteBuffer bytes) Convert a byte buffer to an integer.static longtoLong(ByteBuffer bytes) static shorttoShort(ByteBuffer bytes) Convert a byte buffer to a short.static voidwriteShortLength(ByteBuffer bb, int length) static voidwriteWithLength(ByteBuffer bytes, DataOutputPlus out) static voidwriteWithShortLength(ByteBuffer buffer, DataOutputPlus out) static voidwriteWithVIntLength(ByteBuffer bytes, DataOutputPlus out) static voidwriteZeroes(ByteBuffer dest, int count)
-
Field Details
-
EMPTY_BYTE_BUFFER
-
UNSET_BYTE_BUFFER
Represents an unset value in bound variables -
EMPTY_ARRAY
-
-
Constructor Details
-
ByteBufferUtil
public ByteBufferUtil()
-
-
Method Details
-
compareUnsigned
-
compare
-
compare
-
compare
-
compare
-
string
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 representationposition- the starting position inbufferto start decoding fromlength- the number of bytes frombufferto 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 representationposition- the starting position inbufferto start decoding fromlength- the number of bytes frombufferto usecharset- the String encoding charset- Returns:
- the decoded string
- Throws:
CharacterCodingException
-
string
Decode a String representation.- Parameters:
buffer- a byte buffer holding the string representationcharset- the String encoding charset- Returns:
- the decoded string
- Throws:
CharacterCodingException
-
getArray
You should almost never use this. Instead, use the write* methods to avoid copies. -
getArray
You should almost never use this. Instead, use the write* methods to avoid copies. -
lastIndexOf
ByteBuffer adaptation of org.apache.commons.lang3.ArrayUtils.lastIndexOf method- Parameters:
buffer- the array to traverse for looking for the object, may benullvalueToFind- the value to findstartIndex- 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()];
-1if not found.
-
bytes
Encode a String in a ByteBuffer using UTF_8.- Parameters:
s- the string to encode- Returns:
- the encoded string
-
bytes
Encode a String in a ByteBuffer using the provided charset.- Parameters:
s- the string to encodecharset- the String encoding charset to use- Returns:
- the encoded string
-
clone
- 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
Transfer bytes from a ByteBuffer to byte array.- Parameters:
src- the source ByteBuffersrcPos- starting position in the source ByteBufferdst- the destination byte arraydstPos- starting position in the destination byte arraylength- the number of bytes to copy
-
copyBytes
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 ByteBuffersrcPos- starting position in the source ByteBufferdst- the destination ByteBufferdstPos- starting position in the destination ByteBufferlength- the number of bytes to copy
-
put
-
writeZeroes
-
writeWithLength
- Throws:
IOException
-
writeWithVIntLength
- Throws:
IOException
-
writeWithShortLength
- Throws:
IOException
-
readWithLength
- Throws:
IOException
-
readWithVIntLength
- Throws:
IOException
-
serializedSizeWithLength
-
serializedSizeWithVIntLength
-
skipWithVIntLength
- Throws:
IOException
-
readShortLength
- Throws:
IOException
-
readWithShortLength
- Parameters:
in- data input- Returns:
- An unsigned short in an integer.
- Throws:
IOException- if an I/O error occurs.
-
serializedSizeWithShortLength
-
skipShortLength
- Parameters:
in- data input- Throws:
IOException- if an I/O error occurs.
-
read
- Throws:
IOException
-
readBytes
- Throws:
IOException
-
readBytesWithLength
- Throws:
IOException
-
toInt
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
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
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
-
toFloat
-
getFloat
-
toDouble
-
objectToBytes
-
bytes
-
bytes
-
bytes
-
bytes
-
bytes
-
bytes
-
inputStream
-
bytesToHex
-
hexToBytes
-
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
-
bytes
-
bytes
-
isPrefix
-
canMinimize
-
minimalBufferFor
trims size of bytebuffer to exactly number of bytes in it, to do not hold too much memory -
minimizeBuffers
-
canMinimize
-
getUnsignedShort
-
getShortLength
-
readShortLength
-
writeShortLength
-
readBytes
-
readBytesWithShortLength
-
ensureCapacity
public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize) Ensurebufis large enough foroutputLength. 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 bufferallowBufferResize- true if resizing (reallocating) the buffer is allowed- Returns:
bufif it was large enough, else a newly allocated buffer.
-
ensureCapacity
public static ByteBuffer ensureCapacity(ByteBuffer buf, int outputLength, boolean allowBufferResize, BufferType bufferType) Ensurebufis large enough foroutputLength. 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 bufferallowBufferResize- true if resizing (reallocating) the buffer is allowedbufferType- on- or off- heap byte buffer- Returns:
bufif it was large enough, else a newly allocated buffer.
-
contains
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
-
endsWith
-
equalsWithShortLength
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
- Throws:
IOException
-