Package org.apache.hadoop.hbase.nio
Class ByteBuff
java.lang.Object
org.apache.hadoop.hbase.nio.ByteBuff
- All Implemented Interfaces:
HBaseReferenceCounted
,org.apache.hbase.thirdparty.io.netty.util.ReferenceCounted
- Direct Known Subclasses:
MultiByteBuff
,SingleByteBuff
An abstract class that abstracts out as to how the byte buffers are used, either single or
multiple. We have this interface because the java's ByteBuffers cannot be sub-classed. This class
provides APIs similar to the ones provided in java's nio ByteBuffers and allows you to do
positional reads/writes and relative reads and writes on the underlying BB. In addition to it, we
have some additional APIs which helps us in the read path.
The ByteBuff implement
The ByteBuff implement
HBaseReferenceCounted
interface which mean need to maintains a
RefCnt
inside, if ensure that the ByteBuff won't be used any more, we must do a
release()
to recycle its NIO ByteBuffers. when considering the
duplicate()
or slice()
, releasing either the duplicated one or
the original one will free its memory, because they share the same NIO ByteBuffers. when you want
to retain the NIO ByteBuffers even if the origin one called release()
, you can
do like this:
ByteBuff original = ...; ByteBuff dup = original.duplicate(); dup.retain(); original.release(); // The NIO buffers can still be accessed unless you release the duplicated one dup.get(...); dup.release(); // Both the original and dup can not access the NIO buffers any more.
-
Nested Class Summary
Modifier and TypeClassDescription(package private) static interface
Functional interface for Channel read -
Field Summary
Modifier and TypeFieldDescription(package private) static final ByteBuff.ChannelReader
(package private) static final ByteBuff.ChannelReader
private static final int
protected RefCnt
private static final String
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract byte[]
array()
Returns the byte[] if the underlying BB has single BB and hasArray trueabstract int
Returns the arrayOffset of the byte[] incase of a single BB backed ByteBuffabstract ByteBuffer
asSubByteBuffer
(int length) Returns bytes from current position till length specified, as a single ByteBuffer.abstract void
asSubByteBuffer
(int offset, int length, ObjectIntPair<ByteBuffer> pair) Returns bytes from given offset till length specified, as a single ByteBuffer.abstract int
capacity()
Returns the total capacity of this ByteBuff.protected void
Checks that there are still references to the buffer.static int
Compares two ByteBuffsabstract ByteBuff
Returns an ByteBuff which is a duplicate version of this ByteBuff.abstract byte
get()
A relative method that returns byte at the current position.abstract void
get
(byte[] dst) Copies the content from this ByteBuff's current position to the byte array and fills it.abstract void
get
(byte[] dst, int offset, int length) Copies the specified number of bytes from this ByteBuff's current position to the byte[]'s offset.abstract byte
get
(int index) Fetches the byte at the given index.abstract void
get
(int sourceOffset, byte[] dst, int offset, int length) Copies the specified number of bytes from this ByteBuff's given position to the byte[]'s offset.abstract void
get
(ByteBuffer out, int sourceOffset, int length) Copies the content from this ByteBuff to a ByteBuffer Note : This will advance the position marker ofout
but not change the position maker for this ByteBuffabstract byte
getByteAfterPosition
(int offset) Fetches the byte at the given offset from current position.abstract int
getInt()
Returns the int value at the current position.abstract int
getInt
(int index) Fetches the int at the given index.abstract int
getIntAfterPosition
(int offset) Fetches the int value at the given offset from current position.abstract long
getLong()
Returns the long value at the current position.abstract long
getLong
(int index) Fetches the long at the given index.abstract long
getLongAfterPosition
(int offset) Fetches the long value at the given offset from current position.abstract short
getShort()
Returns the short value at the current position.abstract short
getShort
(int index) Fetches the short value at the given index.abstract short
getShortAfterPosition
(int offset) Fetches the short value at the given offset from current position.abstract boolean
hasArray()
Returns true or false if the underlying BB support hasArrayabstract boolean
Returns true if there are elements between the current position and the limit.abstract int
limit()
Returns the limit of this ByteBuffabstract ByteBuff
limit
(int limit) Marks the limit of this ByteBuffabstract ByteBuff
mark()
Marks the current position of the ByteBuffabstract ByteBuff
moveBack
(int len) Jumps back the current position of this ByteBuff by specified length.abstract ByteBuffer[]
abstract int
position()
Returns this ByteBuff's current positionabstract ByteBuff
position
(int position) Sets this ByteBuff's position to the given value.abstract ByteBuff
put
(byte b) Writes a byte to this ByteBuff at the current position and increments the positionabstract ByteBuff
put
(byte[] src) Copies from the given byte[] to this ByteBuffabstract ByteBuff
put
(byte[] src, int offset, int length) Copies from the given byte[] to this ByteBuffabstract ByteBuff
put
(int index, byte b) Writes a byte to this ByteBuff at the given indexabstract ByteBuff
Copies the contents from the src ByteBuff to this ByteBuff.abstract ByteBuff
putInt
(int value) Writes an int to this ByteBuff at its current position.abstract ByteBuff
putLong
(long value) Writes a long to this ByteBuff at its current position.abstract int
read
(FileChannel channel, long offset) Reads bytes from FileChannel into this ByteBuffabstract int
read
(ReadableByteChannel channel) Reads bytes from the given channel into this ByteBuf.static int
read
(ReadableByteChannel channel, ByteBuffer buf, long offset, ByteBuff.ChannelReader reader) static int
Read integer from ByteBuff coded in 7 bits and increment position.static long
Read long which was written to fitInBytes bytes and increment position.int
refCnt()
boolean
release()
abstract int
Returns the number of elements between the current position and the limit.abstract ByteBuff
reset()
Similar toByteBuffer
.reset(), ensures that this ByteBuff is reset back to last marked position.abstract ByteBuff
rewind()
Rewinds this ByteBuff and the position is set to 0abstract ByteBuff
skip
(int len) Jumps the current position of this ByteBuff by specified length.abstract ByteBuff
slice()
Returns an ByteBuff which is a sliced version of this ByteBuff.byte[]
toBytes()
Copy the content from this ByteBuff to a byte[].abstract byte[]
toBytes
(int offset, int length) Copy the content from this ByteBuff to a byte[] based on the given offset and length.toString()
touch()
Calling this method in strategic locations where ByteBuffs are referenced may help diagnose potential buffer leaks.static ByteBuff
wrap
(ByteBuffer buffer) static ByteBuff
wrap
(ByteBuffer[] buffers) static ByteBuff
wrap
(ByteBuffer[] buffers, ByteBuffAllocator.Recycler recycler) static ByteBuff
wrap
(ByteBuffer[] buffers, RefCnt refCnt) In theory, the upstream should never construct an ByteBuff by passing an given refCnt, so please don't use this public method in other place.private static ByteBuff
wrap
(ByteBuffer buffer, RefCnt refCnt) static ByteBuff
wrap
(List<ByteBuffer> buffers) static ByteBuff
wrap
(List<ByteBuffer> buffers, ByteBuffAllocator.Recycler recycler) private static ByteBuff
wrap
(List<ByteBuffer> buffers, RefCnt refCnt) abstract int
write
(FileChannel channel, long offset) Write this ByteBuff's data into target fileMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.hadoop.hbase.nio.HBaseReferenceCounted
release, retain
Methods inherited from interface org.apache.hbase.thirdparty.io.netty.util.ReferenceCounted
retain
-
Field Details
-
REFERENCE_COUNT_NAME
- See Also:
-
NIO_BUFFER_LIMIT
- See Also:
-
refCnt
-
CHANNEL_READER
-
FILE_READER
-
-
Constructor Details
-
ByteBuff
public ByteBuff()
-
-
Method Details
-
checkRefCount
Checks that there are still references to the buffer. This protects against the case where a ByteBuff method (i.e. slice, get, etc) could be called against a buffer whose backing data may have been released. We only need to do this check if the refCnt has a recycler. If there's no recycler, the backing data will be handled by normal java GC and won't get incorrectly released. So we can avoid the overhead of checking the refCnt on every call. See HBASE-27710. -
refCnt
- Specified by:
refCnt
in interfaceorg.apache.hbase.thirdparty.io.netty.util.ReferenceCounted
-
release
- Specified by:
release
in interfaceorg.apache.hbase.thirdparty.io.netty.util.ReferenceCounted
-
position
Returns this ByteBuff's current position -
position
Sets this ByteBuff's position to the given value.- Returns:
- this object
-
skip
Jumps the current position of this ByteBuff by specified length.- Parameters:
len
- the length to be skipped
-
moveBack
Jumps back the current position of this ByteBuff by specified length.- Parameters:
len
- the length to move back
-
capacity
Returns the total capacity of this ByteBuff. -
limit
Returns the limit of this ByteBuff -
limit
Marks the limit of this ByteBuff -
rewind
Rewinds this ByteBuff and the position is set to 0 -
mark
Marks the current position of the ByteBuff -
asSubByteBuffer
Returns bytes from current position till length specified, as a single ByteBuffer. When all these bytes happen to be in a single ByteBuffer, which this object wraps, that ByteBuffer item as such will be returned. So users are warned not to change the position or limit of this returned ByteBuffer. The position of the returned byte buffer is at the begin of the required bytes. When the required bytes happen to span across multiple ByteBuffers, this API will copy the bytes to a newly created ByteBuffer of required size and return that.- Parameters:
length
- number of bytes required.- Returns:
- bytes from current position till length specified, as a single ByteButter.
-
asSubByteBuffer
Returns bytes from given offset till length specified, as a single ByteBuffer. When all these bytes happen to be in a single ByteBuffer, which this object wraps, that ByteBuffer item as such will be returned (with offset in this ByteBuffer where the bytes starts). So users are warned not to change the position or limit of this returned ByteBuffer. When the required bytes happen to span across multiple ByteBuffers, this API will copy the bytes to a newly created ByteBuffer of required size and return that.- Parameters:
offset
- the offset in this ByteBuff from where the subBuffer should be createdlength
- the length of the subBufferpair
- a pair that will have the bytes from the current position till length specified, as a single ByteBuffer and offset in that Buffer where the bytes starts. Since this API gets called in a loop we are passing a pair to it which could be created outside the loop and the method would set the values on the pair that is passed in by the caller. Thus it avoids more object creations that would happen if the pair that is returned is created by this method every time.
-
remaining
Returns the number of elements between the current position and the limit. -
hasRemaining
Returns true if there are elements between the current position and the limit. -
reset
Similar toByteBuffer
.reset(), ensures that this ByteBuff is reset back to last marked position.- Returns:
- This ByteBuff
-
slice
Returns an ByteBuff which is a sliced version of this ByteBuff. The position, limit and mark of the new ByteBuff will be independent than that of the original ByteBuff. The content of the new ByteBuff will start at this ByteBuff's current position- Returns:
- a sliced ByteBuff
-
duplicate
Returns an ByteBuff which is a duplicate version of this ByteBuff. The position, limit and mark of the new ByteBuff will be independent than that of the original ByteBuff. The content of the new ByteBuff will start at this ByteBuff's current position The position, limit and mark of the new ByteBuff would be identical to this ByteBuff in terms of values.- Returns:
- a sliced ByteBuff
-
get
A relative method that returns byte at the current position. Increments the current position by the size of a byte.- Returns:
- the byte at the current position
-
get
Fetches the byte at the given index. Does not change position of the underlying ByteBuffers- Returns:
- the byte at the given index
-
getByteAfterPosition
Fetches the byte at the given offset from current position. Does not change position of the underlying ByteBuffers.- Returns:
- the byte value at the given index.
-
put
Writes a byte to this ByteBuff at the current position and increments the position- Returns:
- this object
-
put
Writes a byte to this ByteBuff at the given index- Returns:
- this object
-
get
Copies the specified number of bytes from this ByteBuff's current position to the byte[]'s offset. Also advances the position of the ByteBuff by the given length.- Parameters:
dst
- the byte[] to which the ByteBuff's content is to be copiedoffset
- within the current arraylength
- upto which the bytes to be copied
-
get
Copies the specified number of bytes from this ByteBuff's given position to the byte[]'s offset. The position of the ByteBuff remains in the current position only- Parameters:
sourceOffset
- the offset in this ByteBuff from where the copy should happendst
- the byte[] to which the ByteBuff's content is to be copiedoffset
- within the current arraylength
- upto which the bytes to be copied
-
get
Copies the content from this ByteBuff's current position to the byte array and fills it. Also advances the position of the ByteBuff by the length of the byte[].- Parameters:
dst
- the byte[] to which the ByteBuff's content is to be copied
-
put
Copies from the given byte[] to this ByteBuff- Parameters:
src
- source byte arrayoffset
- the position in the byte array from which the copy should be donelength
- the length upto which the copy should happen- Returns:
- this ByteBuff
-
put
Copies from the given byte[] to this ByteBuff- Parameters:
src
- source byte array- Returns:
- this ByteBuff
-
hasArray
Returns true or false if the underlying BB support hasArray -
array
Returns the byte[] if the underlying BB has single BB and hasArray true -
arrayOffset
Returns the arrayOffset of the byte[] incase of a single BB backed ByteBuff -
getShort
Returns the short value at the current position. Also advances the position by the size of short. -
getShort
Fetches the short value at the given index. Does not change position of the underlying ByteBuffers. The caller is sure that the index will be after the current position of this ByteBuff. So even if the current short does not fit in the current item we can safely move to the next item and fetch the remaining bytes forming the short- Returns:
- the short value at the given index
-
getShortAfterPosition
Fetches the short value at the given offset from current position. Does not change position of the underlying ByteBuffers.- Returns:
- the short value at the given index.
-
getInt
Returns the int value at the current position. Also advances the position by the size of int. -
putInt
Writes an int to this ByteBuff at its current position. Also advances the position by size of int. -
getInt
Fetches the int at the given index. Does not change position of the underlying ByteBuffers. Even if the current int does not fit in the current item we can safely move to the next item and fetch the remaining bytes forming the int. -
getIntAfterPosition
Fetches the int value at the given offset from current position. Does not change position of the underlying ByteBuffers. -
getLong
Returns the long value at the current position. Also advances the position by the size of long. -
putLong
Writes a long to this ByteBuff at its current position. Also advances the position by size of long. -
getLong
Fetches the long at the given index. Does not change position of the underlying ByteBuffers. The caller is sure that the index will be after the current position of this ByteBuff. So even if the current long does not fit in the current item we can safely move to the next item and fetch the remaining bytes forming the long- Returns:
- the long value at the given index
-
getLongAfterPosition
Fetches the long value at the given offset from current position. Does not change position of the underlying ByteBuffers.- Returns:
- the long value at the given index.
-
toBytes
Copy the content from this ByteBuff to a byte[]. -
toBytes
Copy the content from this ByteBuff to a byte[] based on the given offset and length. -
get
Copies the content from this ByteBuff to a ByteBuffer Note : This will advance the position marker ofout
but not change the position maker for this ByteBuff- Parameters:
out
- the ByteBuffer to which the copy has to happensourceOffset
- the offset in the ByteBuff from which the elements has to be copiedlength
- the length in this ByteBuff upto which the elements has to be copied
-
put
Copies the contents from the src ByteBuff to this ByteBuff. This will be absolute positional copying and won't affect the position of any of the buffers.- Parameters:
offset
- the position in this ByteBuff to which the copy should happensrc
- the src ByteBuffsrcOffset
- the offset in the src ByteBuff from where the elements should be readlength
- the length up to which the copy should happen
-
read
Reads bytes from the given channel into this ByteBuf.- Throws:
IOException
-
read
Reads bytes from FileChannel into this ByteBuff- Throws:
IOException
-
write
Write this ByteBuff's data into target file- Throws:
IOException
-
read
public static int read(ReadableByteChannel channel, ByteBuffer buf, long offset, ByteBuff.ChannelReader reader) throws IOException - Throws:
IOException
-
readCompressedInt
Read integer from ByteBuff coded in 7 bits and increment position. -
compareTo
Compares two ByteBuffs- Parameters:
buf1
- the first ByteBuffo1
- the offset in the first ByteBuff from where the compare has to happenlen1
- the length in the first ByteBuff upto which the compare has to happenbuf2
- the second ByteBuffo2
- the offset in the second ByteBuff from where the compare has to happenlen2
- the length in the second ByteBuff upto which the compare has to happen- Returns:
- Positive if buf1 is bigger than buf2, 0 if they are equal, and negative if buf1 is smaller than buf2.
-
readLong
Read long which was written to fitInBytes bytes and increment position.- Parameters:
fitInBytes
- In how many bytes given long is stored.- Returns:
- The value of parsed long.
-
nioByteBuffers
-
toString
-
wrap
In theory, the upstream should never construct an ByteBuff by passing an given refCnt, so please don't use this public method in other place. Make the method public here because the BucketEntry#wrapAsCacheable in hbase-server module will use its own refCnt and ByteBuffers from IOEngine to composite an HFileBlock's ByteBuff, we didn't find a better way so keep the public way here. -
wrap
-
wrap
-
wrap
-
wrap
-
wrap
-
touch
Calling this method in strategic locations where ByteBuffs are referenced may help diagnose potential buffer leaks. We pass the buffer itself as a default hint, but one can usetouch(Object)
to pass their own hint as well.- Specified by:
touch
in interfaceHBaseReferenceCounted
- Specified by:
touch
in interfaceorg.apache.hbase.thirdparty.io.netty.util.ReferenceCounted
-
touch
- Specified by:
touch
in interfaceHBaseReferenceCounted
- Specified by:
touch
in interfaceorg.apache.hbase.thirdparty.io.netty.util.ReferenceCounted
-
getRefCnt
-
wrap
-
wrap
-