Package alluxio.util.io
Class BufferUtils
- java.lang.Object
-
- alluxio.util.io.BufferUtils
-
@ThreadSafe public final class BufferUtils extends java.lang.Object
Utilities related to buffers, not onlyByteBuffer
.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
byteToInt(byte b)
Converts a byte to an integer.static void
cleanDirectBuffer(java.nio.ByteBuffer buffer)
Forces to unmap a direct buffer if this buffer is no longer used.static java.nio.ByteBuffer
cloneByteBuffer(java.nio.ByteBuffer buf)
Clones aByteBuffer
.static java.util.List<java.nio.ByteBuffer>
cloneByteBufferList(java.util.List<java.nio.ByteBuffer> source)
Clones a list ofByteBuffer
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 givenByteBuf
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, java.nio.ByteBuffer buf)
Checks if the givenByteBuffer
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(java.nio.channels.ReadableByteChannel src, java.nio.channels.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 java.nio.ByteBuffer
getIncreasingByteBuffer(int len)
Gets aByteBuffer
containing an increasing sequence of bytes starting at zero.static java.nio.ByteBuffer
getIncreasingByteBuffer(int start, int len)
Gets aByteBuffer
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 startNum, int startIndex, int endIndex, java.nio.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 startIndex, int endIndex, java.nio.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(java.nio.ByteBuffer buf)
Creates a byte array from the given ByteBuffer, the position property of inputByteBuffer
remains unchanged.static void
putIntByteBuffer(java.nio.ByteBuffer buf, int b)
Puts a byte (the first byte of an integer) into aByteBuffer
.static java.nio.ByteBuffer
sliceByteBuffer(java.nio.ByteBuffer buffer, int position)
Convenience method forsliceByteBuffer(ByteBuffer, int, int)
where the last parameter is the number of remaining bytes in the new buffer.static java.nio.ByteBuffer
sliceByteBuffer(java.nio.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(java.nio.channels.ReadableByteChannel src, java.nio.channels.WritableByteChannel dest)
An efficient copy between two channels with a fixed-size buffer.static void
writeBufferToFile(java.lang.String path, byte[] buffer)
Writes buffer to the given file path.
-
-
-
Method Detail
-
byteToInt
public static int byteToInt(byte b)
Converts a byte to an integer.- Parameters:
b
- the byte to convert- Returns:
- the integer representation of the byte
-
cleanDirectBuffer
public static void cleanDirectBuffer(java.nio.ByteBuffer buffer)
Forces to unmap a direct buffer if this buffer is no longer used. After calling this method, this direct buffer should be discarded. This is unsafe operation and currently a work-around to avoid huge memory occupation caused by memory map.- Parameters:
buffer
- bytebuffer
-
cloneByteBuffer
public static java.nio.ByteBuffer cloneByteBuffer(java.nio.ByteBuffer buf)
Clones aByteBuffer
.The new bytebuffer will have the same content, but the type of the bytebuffer may not be the same.
- Parameters:
buf
- The ByteBuffer to copy- Returns:
- The new ByteBuffer
-
cloneByteBufferList
public static java.util.List<java.nio.ByteBuffer> cloneByteBufferList(java.util.List<java.nio.ByteBuffer> source)
Clones a list ofByteBuffer
s.- Parameters:
source
- the list of ByteBuffers to copy- Returns:
- the new list of ByteBuffers
-
putIntByteBuffer
public static void putIntByteBuffer(java.nio.ByteBuffer buf, int b)
Puts a byte (the first byte of an integer) into aByteBuffer
.- Parameters:
buf
- ByteBuffer to useb
- byte to put into the ByteBuffer
-
getIncreasingByteArray
public static byte[] getIncreasingByteArray(int len)
Gets an increasing sequence of bytes starting at zero.- Parameters:
len
- the target length of the sequence- Returns:
- an increasing sequence of bytes
-
getIncreasingByteArray
public static byte[] getIncreasingByteArray(int start, int len)
Gets an increasing sequence of bytes starting with the given value.- Parameters:
start
- the starting value to uselen
- the target length of the sequence- Returns:
- an increasing sequence of bytes
-
equalConstantByteArray
public 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.- Parameters:
value
- the value to check forlen
- the target length of the sequencearr
- the byte array to check- Returns:
- true if the byte array has a prefix of length
len
that is a constant sequence of bytes of the given value
-
equalIncreasingByteArray
public 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.- Parameters:
len
- the target length of the sequencearr
- the byte array to check- Returns:
- true if the byte array has a prefix of length
len
that is an increasing sequence of bytes starting at zero
-
equalIncreasingByteArray
public 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. The array length must be equal to the length checked.- Parameters:
start
- the starting value to uselen
- the target length of the sequencearr
- the byte array to check- Returns:
- true if the byte array has a prefix of length
len
that is an increasing sequence of bytes starting atstart
-
startsWithIncreasingByteArray
public 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.- Parameters:
start
- the starting value to uselen
- the target length of the sequencearr
- the byte array to check- Returns:
- true if the byte array has a subarray of length
len
that is an increasing sequence of bytes starting atstart
-
getIncreasingByteBuffer
public static java.nio.ByteBuffer getIncreasingByteBuffer(int len)
Gets aByteBuffer
containing an increasing sequence of bytes starting at zero.- Parameters:
len
- the target length of the sequence- Returns:
- ByteBuffer containing an increasing sequence of bytes
-
getIncreasingByteBuffer
public static java.nio.ByteBuffer getIncreasingByteBuffer(int start, int len)
Gets aByteBuffer
containing an increasing sequence of bytes starting at the given value.- Parameters:
len
- the target length of the sequencestart
- the starting value to use- Returns:
- ByteBuffer containing an increasing sequence of bytes
-
equalIncreasingByteBuffer
public static boolean equalIncreasingByteBuffer(int start, int len, java.nio.ByteBuffer buf)
Checks if the givenByteBuffer
starts with an increasing sequence of bytes starting at the given value of length equal to or greater than the given length.- Parameters:
start
- the starting value to uselen
- the target length of the sequencebuf
- the ByteBuffer to check- Returns:
- true if the ByteBuffer has a prefix of length
len
that is an increasing sequence of bytes starting atstart
-
equalIncreasingByteBuf
public static boolean equalIncreasingByteBuf(int start, int len, io.netty.buffer.ByteBuf buf)
Checks if the givenByteBuf
starts with an increasing sequence of bytes starting at the given value of length equal to or greater than the given length.- Parameters:
start
- the starting value to uselen
- the target length of the sequencebuf
- the ByteBuffer to check- Returns:
- true if the ByteBuf has a prefix of length
len
that is an increasing sequence of bytes starting atstart
-
matchIncreasingByteArray
public 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.- Parameters:
startIndex
- the start index in the array to check againstendIndex
- the end index in the array to check againstarr
- the byte array to check- Returns:
- true if match, false otherwise
-
matchIncreasingByteArray
public 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.- Parameters:
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 check- Returns:
- true if match, false otherwise
-
matchIncreasingByteBuffer
public static boolean matchIncreasingByteBuffer(int startIndex, int endIndex, java.nio.ByteBuffer buf)
Checks if the given byte buffer has a sub buffer equals an increasing sequence of bytes, starting from the given value.- Parameters:
startIndex
- the start index in the array to check againstendIndex
- the end index in the array to check againstbuf
- the byte buffer to check- Returns:
- true if match, false otherwise
-
matchIncreasingByteBuffer
public static boolean matchIncreasingByteBuffer(int startNum, int startIndex, int endIndex, java.nio.ByteBuffer buf)
Checks if the given byte buffer has a sub buffer equals an increasing sequence of bytes, starting from the given value.- Parameters:
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 check- Returns:
- true if match, false otherwise
-
writeBufferToFile
public static void writeBufferToFile(java.lang.String path, byte[] buffer) throws java.io.IOException
Writes buffer to the given file path.- Parameters:
path
- file path to write the databuffer
- raw data- Throws:
java.io.IOException
-
transfer
public static void transfer(java.nio.channels.ReadableByteChannel src, java.nio.channels.WritableByteChannel dest) throws java.io.IOException
An efficient copy between two channels with a fixed-size buffer.- Parameters:
src
- the source channeldest
- the destination channel- Throws:
java.io.IOException
-
intAsUnsignedByteValue
public static int intAsUnsignedByteValue(int i)
Gets the unsigned byte value of an integer. Useful for comparing to the result of reading from an input stream.- Parameters:
i
- the integer to convert- Returns:
- the integer value after casting as an unsigned byte
-
newByteArrayFromByteBuffer
public static byte[] newByteArrayFromByteBuffer(java.nio.ByteBuffer buf)
Creates a byte array from the given ByteBuffer, the position property of inputByteBuffer
remains unchanged.- Parameters:
buf
- source ByteBuffer- Returns:
- a newly created byte array
-
sliceByteBuffer
public static java.nio.ByteBuffer sliceByteBuffer(java.nio.ByteBuffer buffer, int position, int length)
Creates a new ByteBuffer sliced from a given ByteBuffer. The new ByteBuffer shares the content of the existing one, but with independent position/mark/limit. After slicing, the new ByteBuffer has position 0, and the input ByteBuffer is unmodified.- Parameters:
buffer
- source ByteBuffer to sliceposition
- position in the source ByteBuffer to slicelength
- length of the sliced ByteBuffer- Returns:
- the sliced ByteBuffer
-
sliceByteBuffer
public static java.nio.ByteBuffer sliceByteBuffer(java.nio.ByteBuffer buffer, int position)
Convenience method forsliceByteBuffer(ByteBuffer, int, int)
where the last parameter is the number of remaining bytes in the new buffer.- Parameters:
buffer
- sourceByteBuffer
to sliceposition
- position in the sourceByteBuffer
to slice- Returns:
- the sliced
ByteBuffer
-
fastCopy
public static void fastCopy(java.nio.channels.ReadableByteChannel src, java.nio.channels.WritableByteChannel dest) throws java.io.IOException
An efficient copy between two channels with a fixed-size buffer.- Parameters:
src
- the source channeldest
- the destination channel- Throws:
java.io.IOException
-
-