@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 |
equalIncreasingByteBuf(int start,
int len,
io.netty.buffer.ByteBuf buf)
Checks if the given
ByteBuf starts with an increasing sequence of bytes starting at
the given value of length equal to or greater than the given length. |
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 boolean |
matchIncreasingByteArray(int startIndex,
int endIndex,
byte[] arr)
Checks if the given byte array has a sub array equals an increasing sequence of bytes,
starting from 0.
|
static boolean |
matchIncreasingByteArray(int startNum,
int startIndex,
int endIndex,
byte[] arr)
Checks if the given byte array has a sub array equals an increasing sequence of bytes,
starting from the given value.
|
static boolean |
matchIncreasingByteBuffer(int startIndex,
int endIndex,
ByteBuffer buf)
Checks if the given byte buffer has a sub buffer equals an increasing sequence of bytes,
starting from the given value.
|
static boolean |
matchIncreasingByteBuffer(int startNum,
int startIndex,
int endIndex,
ByteBuffer buf)
Checks if the given byte buffer has a sub buffer equals an increasing sequence of bytes,
starting from the given value.
|
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 boolean |
startsWithIncreasingByteArray(int start,
int len,
byte[] arr)
Checks if the given byte array contains an increasing sequence of bytes of the given
length, starting from the given value.
|
static void |
transfer(ReadableByteChannel src,
WritableByteChannel dest)
An efficient copy between two channels with a fixed-size buffer.
|
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)
buffer
- bytebufferpublic 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 boolean startsWithIncreasingByteArray(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 boolean equalIncreasingByteBuf(int start, int len, io.netty.buffer.ByteBuf buf)
ByteBuf
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 boolean matchIncreasingByteArray(int startIndex, int endIndex, byte[] arr)
startIndex
- the start index in the array to check againstendIndex
- the end index in the array to check againstarr
- the byte array to checkpublic static boolean matchIncreasingByteArray(int startNum, int startIndex, int endIndex, byte[] arr)
startNum
- the starting value to usestartIndex
- the start index in the array to check againstendIndex
- the end index in the array to check againstarr
- the byte array to checkpublic static boolean matchIncreasingByteBuffer(int startIndex, int endIndex, ByteBuffer buf)
startIndex
- the start index in the array to check againstendIndex
- the end index in the array to check againstbuf
- the byte buffer to checkpublic static boolean matchIncreasingByteBuffer(int startNum, int startIndex, int endIndex, ByteBuffer buf)
startNum
- the starting value to usestartIndex
- the start index in the array to check againstendIndex
- the end index in the array to check againstbuf
- the byte buffer to checkpublic static void writeBufferToFile(String path, byte[] buffer) throws IOException
path
- file path to write the databuffer
- raw dataIOException
public static void transfer(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
public static void fastCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException
src
- the source channeldest
- the destination channelIOException
Copyright © 2023. All Rights Reserved.