@ThreadSafe public final class BufferUtils extends Object
ByteBuffer
.Modifier and Type | Method and Description |
---|---|
static int |
byteToInt(byte b)
Converts a byte to an integer.
|
static void |
cleanDirectBuffer(ByteBuffer buffer)
Forces to unmap a direct buffer if this buffer is no longer used.
|
static ByteBuffer |
cloneByteBuffer(ByteBuffer buf)
Clones a
ByteBuffer . |
static List<ByteBuffer> |
cloneByteBufferList(List<ByteBuffer> source)
Clones a list of
ByteBuffer s. |
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 |
equalIncreasingByteBuffer(int start,
int len,
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(ReadableByteChannel src,
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 ByteBuffer |
getIncreasingByteBuffer(int len)
Gets a
ByteBuffer containing an increasing sequence of bytes starting at zero. |
static 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 byte[] |
newByteArrayFromByteBuffer(ByteBuffer buf)
Creates a byte array from the given ByteBuffer, the position property of input
ByteBuffer remains unchanged. |
static void |
putIntByteBuffer(ByteBuffer buf,
int b)
Puts a byte (the first byte of an integer) into a
ByteBuffer . |
static ByteBuffer |
sliceByteBuffer(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 ByteBuffer |
sliceByteBuffer(ByteBuffer buffer,
int position,
int length)
Creates a new ByteBuffer sliced from a given ByteBuffer.
|
static void |
writeBufferToFile(String path,
byte[] buffer)
Writes buffer to the given file path.
|
public static int byteToInt(byte b)
b
- the byte to convertpublic static void cleanDirectBuffer(ByteBuffer buffer)
NOTE: DirectByteBuffers are not guaranteed to be garbage-collected immediately after their references are released and may lead to OutOfMemoryError. This function helps by calling the Cleaner method of a DirectByteBuffer explicitly. See more discussion.
buffer
- the byte buffer to be unmapped, this must be a direct bufferpublic static ByteBuffer cloneByteBuffer(ByteBuffer buf)
ByteBuffer
.
The new bytebuffer will have the same content, but the type of the bytebuffer may not be the same.
buf
- The ByteBuffer to copypublic static List<ByteBuffer> cloneByteBufferList(List<ByteBuffer> source)
ByteBuffer
s.source
- the list of ByteBuffers to copypublic static void putIntByteBuffer(ByteBuffer buf, int b)
ByteBuffer
.buf
- ByteBuffer to useb
- byte to put into the ByteBufferpublic static byte[] getIncreasingByteArray(int len)
len
- the target length of the sequencepublic static byte[] getIncreasingByteArray(int start, int len)
start
- the starting value to uselen
- the target length of the sequencepublic static boolean equalConstantByteArray(byte value, int len, byte[] arr)
value
- the value to check forlen
- the target length of the sequencearr
- the byte array to checklen
that is a constant
sequence of bytes of the given valuepublic static boolean equalIncreasingByteArray(int len, byte[] arr)
len
- the target length of the sequencearr
- the byte array to checklen
that is an increasing
sequence of bytes starting at zeropublic static boolean equalIncreasingByteArray(int start, int len, byte[] arr)
start
- the starting value to uselen
- the target length of the sequencearr
- the byte array to checklen
that is an increasing
sequence of bytes starting at start
public static ByteBuffer getIncreasingByteBuffer(int len)
ByteBuffer
containing an increasing sequence of bytes starting at zero.len
- the target length of the sequencepublic static ByteBuffer getIncreasingByteBuffer(int start, int len)
ByteBuffer
containing an increasing sequence of bytes starting at the given
value.len
- the target length of the sequencestart
- the starting value to usepublic static boolean equalIncreasingByteBuffer(int start, int len, ByteBuffer buf)
ByteBuffer
starts with an increasing sequence of bytes starting at
the given value of length equal to or greater than the given length.start
- the starting value to uselen
- the target length of the sequencebuf
- the ByteBuffer to checklen
that is an increasing
sequence of bytes starting at start
public static void writeBufferToFile(String path, byte[] buffer) throws IOException
path
- file path to write the databuffer
- raw dataIOException
public static void fastCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException
src
- the source channeldest
- the destination channelIOException
public static int intAsUnsignedByteValue(int i)
i
- the integer to convertpublic static byte[] newByteArrayFromByteBuffer(ByteBuffer buf)
ByteBuffer
remains unchanged.buf
- source ByteBufferpublic static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length)
buffer
- source ByteBuffer to sliceposition
- position in the source ByteBuffer to slicelength
- length of the sliced ByteBufferpublic static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position)
sliceByteBuffer(ByteBuffer, int, int)
where the last parameter
is the number of remaining bytes in the new buffer.buffer
- source ByteBuffer
to sliceposition
- position in the source ByteBuffer
to sliceByteBuffer
Copyright © 2023. All Rights Reserved.