Package org.apache.cassandra.utils
Class ByteBufferUtil
- java.lang.Object
-
- org.apache.cassandra.utils.ByteBufferUtil
-
public class ByteBufferUtil extends java.lang.ObjectUtility 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
Fields Modifier and Type Field Description static java.nio.ByteBuffer[]EMPTY_ARRAYstatic java.nio.ByteBufferEMPTY_BYTE_BUFFERstatic java.nio.ByteBufferUNSET_BYTE_BUFFERRepresents an unset value in bound variables
-
Constructor Summary
Constructors Constructor Description ByteBufferUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.nio.ByteBufferbytes(byte b)static java.nio.ByteBufferbytes(double d)static java.nio.ByteBufferbytes(float f)static java.nio.ByteBufferbytes(int i)static java.nio.ByteBufferbytes(long n)static java.nio.ByteBufferbytes(short s)static java.nio.ByteBufferbytes(java.lang.String s)Encode a String in a ByteBuffer using UTF_8.static java.nio.ByteBufferbytes(java.lang.String s, java.nio.charset.Charset charset)Encode a String in a ByteBuffer using the provided charset.static java.nio.ByteBufferbytes(java.net.InetAddress address)static java.nio.ByteBufferbytes(java.util.UUID uuid)static java.nio.ByteBufferbytes(TimeUUID uuid)static java.lang.StringbytesToHex(java.nio.ByteBuffer bytes)static booleancanMinimize(java.nio.ByteBuffer buf)static booleancanMinimize(java.nio.ByteBuffer[] src)static java.nio.ByteBufferclone(java.nio.ByteBuffer buffer)static intcompare(byte[] o1, java.nio.ByteBuffer o2)static intcompare(byte[] o1, java.nio.ByteBuffer o2, int s2, int l2)static intcompare(java.nio.ByteBuffer o1, byte[] o2)static intcompare(java.nio.ByteBuffer o1, int s1, int l1, byte[] o2)static intcompareSubArrays(java.nio.ByteBuffer bytes1, int offset1, java.nio.ByteBuffer bytes2, int offset2, int length)Compare two ByteBuffer at specified offsets for length.static intcompareUnsigned(java.nio.ByteBuffer o1, java.nio.ByteBuffer o2)static booleancontains(java.nio.ByteBuffer buffer, java.nio.ByteBuffer subBuffer)Check is the given buffer contains a given sub-buffer.static voidcopyBytes(java.nio.ByteBuffer src, int srcPos, byte[] dst, int dstPos, int length)Transfer bytes from a ByteBuffer to byte array.static voidcopyBytes(java.nio.ByteBuffer src, int srcPos, java.nio.ByteBuffer dst, int dstPos, int length)Transfer bytes from one ByteBuffer to another.static booleanendsWith(java.nio.ByteBuffer src, java.nio.ByteBuffer suffix)static java.nio.ByteBufferensureCapacity(java.nio.ByteBuffer buf, int outputLength, boolean allowBufferResize)Ensurebufis large enough foroutputLength.static java.nio.ByteBufferensureCapacity(java.nio.ByteBuffer buf, int outputLength, boolean allowBufferResize, BufferType bufferType)Ensurebufis large enough foroutputLength.static booleanequalsWithShortLength(java.io.DataInput in, java.nio.ByteBuffer toMatch)Returns true if the buffer at the current position in the input matches given buffer.static byte[]getArray(java.nio.ByteBuffer buffer)You should almost never use this.static byte[]getArray(java.nio.ByteBuffer buffer, int position, int length)You should almost never use this.static floatgetFloat(java.nio.ByteBuffer bytes, int offset)static intgetShortLength(java.nio.ByteBuffer bb, int position)static intgetUnsignedShort(java.nio.ByteBuffer bb, int position)static java.nio.ByteBufferhexToBytes(java.lang.String str)static java.io.InputStreaminputStream(java.nio.ByteBuffer bytes)static booleanisPrefix(java.nio.ByteBuffer prefix, java.nio.ByteBuffer value)static intlastIndexOf(java.nio.ByteBuffer buffer, byte valueToFind, int startIndex)ByteBuffer adaptation of org.apache.commons.lang3.ArrayUtils.lastIndexOf methodstatic java.nio.ByteBufferminimalBufferFor(java.nio.ByteBuffer buf)trims size of bytebuffer to exactly number of bytes in it, to do not hold too much memorystatic java.nio.ByteBuffer[]minimizeBuffers(java.nio.ByteBuffer[] src)static java.nio.ByteBufferobjectToBytes(java.lang.Object obj)static intput(java.nio.ByteBuffer src, java.nio.ByteBuffer trg)static java.nio.ByteBufferread(java.io.DataInput in, int length)static byte[]readBytes(java.io.DataInput in, int length)static java.nio.ByteBufferreadBytes(java.nio.ByteBuffer bb, int length)static byte[]readBytesWithLength(java.io.DataInput in)static java.nio.ByteBufferreadBytesWithShortLength(java.nio.ByteBuffer bb)static voidreadFully(java.nio.channels.FileChannel channel, java.nio.ByteBuffer dst, long position)static intreadShortLength(java.io.DataInput in)static intreadShortLength(java.nio.ByteBuffer bb)static java.nio.ByteBufferreadWithLength(java.io.DataInput in)static java.nio.ByteBufferreadWithShortLength(java.io.DataInput in)static java.nio.ByteBufferreadWithVIntLength(DataInputPlus in)static intserializedSizeWithLength(java.nio.ByteBuffer buffer)static intserializedSizeWithShortLength(java.nio.ByteBuffer buffer)static intserializedSizeWithVIntLength(java.nio.ByteBuffer buffer)static voidskipShortLength(DataInputPlus in)static voidskipWithVIntLength(DataInputPlus in)static booleanstartsWith(java.nio.ByteBuffer src, java.nio.ByteBuffer prefix)static java.lang.Stringstring(java.nio.ByteBuffer buffer)Decode a String representation.static java.lang.Stringstring(java.nio.ByteBuffer buffer, int position, int length)Decode a String representation.static java.lang.Stringstring(java.nio.ByteBuffer buffer, int position, int length, java.nio.charset.Charset charset)Decode a String representation.static java.lang.Stringstring(java.nio.ByteBuffer buffer, java.nio.charset.Charset charset)Decode a String representation.static bytetoByte(java.nio.ByteBuffer bytes)Convert a byte buffer to a short.static doubletoDouble(java.nio.ByteBuffer bytes)static floattoFloat(java.nio.ByteBuffer bytes)static inttoInt(java.nio.ByteBuffer bytes)Convert a byte buffer to an integer.static longtoLong(java.nio.ByteBuffer bytes)static shorttoShort(java.nio.ByteBuffer bytes)Convert a byte buffer to a short.static voidwriteShortLength(java.nio.ByteBuffer bb, int length)static voidwriteWithLength(java.nio.ByteBuffer bytes, DataOutputPlus out)static voidwriteWithShortLength(java.nio.ByteBuffer buffer, DataOutputPlus out)static voidwriteWithVIntLength(java.nio.ByteBuffer bytes, DataOutputPlus out)static voidwriteZeroes(java.nio.ByteBuffer dest, int count)
-
-
-
Method Detail
-
compareUnsigned
@Inline public static int compareUnsigned(java.nio.ByteBuffer o1, java.nio.ByteBuffer o2)
-
compare
@Inline public static int compare(byte[] o1, java.nio.ByteBuffer o2)
-
compare
@Inline public static int compare(java.nio.ByteBuffer o1, byte[] o2)
-
compare
public static int compare(java.nio.ByteBuffer o1, int s1, int l1, byte[] o2)
-
compare
public static int compare(byte[] o1, java.nio.ByteBuffer o2, int s2, int l2)
-
string
public static java.lang.String string(java.nio.ByteBuffer buffer) throws java.nio.charset.CharacterCodingExceptionDecode 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.CharacterCodingExceptionDecode 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:
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.CharacterCodingExceptionDecode 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:
java.nio.charset.CharacterCodingException
-
string
public static java.lang.String string(java.nio.ByteBuffer buffer, java.nio.charset.Charset charset) throws java.nio.charset.CharacterCodingExceptionDecode a String representation.- Parameters:
buffer- a byte buffer holding the string representationcharset- 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.
-
getArray
public static byte[] getArray(java.nio.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(java.nio.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 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
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 encodecharset- 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.
-
copyBytes
public static void copyBytes(java.nio.ByteBuffer src, int srcPos, byte[] dst, int dstPos, int length)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
public static void copyBytes(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, 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
public static int put(java.nio.ByteBuffer src, java.nio.ByteBuffer trg)
-
writeZeroes
public static void writeZeroes(java.nio.ByteBuffer dest, int count)
-
writeWithLength
public static void writeWithLength(java.nio.ByteBuffer bytes, DataOutputPlus out) throws java.io.IOException- Throws:
java.io.IOException
-
writeWithVIntLength
public static void writeWithVIntLength(java.nio.ByteBuffer bytes, DataOutputPlus out) throws java.io.IOException- Throws:
java.io.IOException
-
writeWithShortLength
public static void writeWithShortLength(java.nio.ByteBuffer buffer, DataOutputPlus out) throws java.io.IOException- Throws:
java.io.IOException
-
readWithLength
public static java.nio.ByteBuffer readWithLength(java.io.DataInput in) throws java.io.IOException- Throws:
java.io.IOException
-
readWithVIntLength
public static java.nio.ByteBuffer readWithVIntLength(DataInputPlus in) throws java.io.IOException
- Throws:
java.io.IOException
-
serializedSizeWithLength
public static int serializedSizeWithLength(java.nio.ByteBuffer buffer)
-
serializedSizeWithVIntLength
public static int serializedSizeWithVIntLength(java.nio.ByteBuffer buffer)
-
skipWithVIntLength
public static void skipWithVIntLength(DataInputPlus 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.
-
serializedSizeWithShortLength
public static int serializedSizeWithShortLength(java.nio.ByteBuffer buffer)
-
skipShortLength
public static void skipShortLength(DataInputPlus in) throws java.io.IOException
- Parameters:
in- data input- 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
-
readBytes
public static byte[] readBytes(java.io.DataInput in, int length) throws java.io.IOException- Throws:
java.io.IOException
-
readBytesWithLength
public static byte[] readBytesWithLength(java.io.DataInput in) 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
-
toShort
public static short toShort(java.nio.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(java.nio.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(java.nio.ByteBuffer bytes)
-
toFloat
public static float toFloat(java.nio.ByteBuffer bytes)
-
getFloat
public static float getFloat(java.nio.ByteBuffer bytes, int offset)
-
toDouble
public static double toDouble(java.nio.ByteBuffer bytes)
-
objectToBytes
public static java.nio.ByteBuffer objectToBytes(java.lang.Object obj)
-
bytes
public static java.nio.ByteBuffer bytes(byte b)
-
bytes
public static java.nio.ByteBuffer bytes(short s)
-
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.
-
bytes
public static java.nio.ByteBuffer bytes(java.net.InetAddress address)
-
bytes
public static java.nio.ByteBuffer bytes(java.util.UUID uuid)
-
bytes
public static java.nio.ByteBuffer bytes(TimeUUID uuid)
-
isPrefix
public static boolean isPrefix(java.nio.ByteBuffer prefix, java.nio.ByteBuffer value)
-
canMinimize
public static boolean canMinimize(java.nio.ByteBuffer buf)
-
minimalBufferFor
public static java.nio.ByteBuffer minimalBufferFor(java.nio.ByteBuffer buf)
trims size of bytebuffer to exactly number of bytes in it, to do not hold too much memory
-
minimizeBuffers
public static java.nio.ByteBuffer[] minimizeBuffers(java.nio.ByteBuffer[] src)
-
canMinimize
public static boolean canMinimize(java.nio.ByteBuffer[] src)
-
getUnsignedShort
public static int getUnsignedShort(java.nio.ByteBuffer bb, int position)
-
getShortLength
public static int getShortLength(java.nio.ByteBuffer bb, int position)
-
readShortLength
public static int readShortLength(java.nio.ByteBuffer bb)
-
writeShortLength
public static void writeShortLength(java.nio.ByteBuffer bb, int length)
-
readBytes
public static java.nio.ByteBuffer readBytes(java.nio.ByteBuffer bb, int length)
-
readBytesWithShortLength
public static java.nio.ByteBuffer readBytesWithShortLength(java.nio.ByteBuffer bb)
-
ensureCapacity
public static java.nio.ByteBuffer ensureCapacity(java.nio.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 java.nio.ByteBuffer ensureCapacity(java.nio.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
public static boolean contains(java.nio.ByteBuffer buffer, java.nio.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(java.nio.ByteBuffer src, java.nio.ByteBuffer prefix)
-
endsWith
public static boolean endsWith(java.nio.ByteBuffer src, java.nio.ByteBuffer suffix)
-
equalsWithShortLength
public static boolean equalsWithShortLength(java.io.DataInput in, java.nio.ByteBuffer toMatch) throws java.io.IOExceptionReturns 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:
java.io.IOException
-
readFully
public static void readFully(java.nio.channels.FileChannel channel, java.nio.ByteBuffer dst, long position) throws java.io.IOException- Throws:
java.io.IOException
-
-