Class ByteBuf
- All Implemented Interfaces:
Recyclable
head and tail.
When you write data to ByteBuf, it's tail increases by the amount of bytes written.
When you read data from ByteBuf, it's head increases by the amount of bytes read.
You can read bytes from ByteBuf only when tail is bigger than head.
You can write bytes to ByteBuf until tail doesn't exceed length of the underlying array.
ByteBuf is similar to a FIFO byte queue except it has no wrap-around or growth.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRef()Increasesrefsvalue by 1.byte[]array()Returns byte arrayarray.byte[]asArray()Returns a byte array fromheadtotail.Returns aStringcreated from thisByteBufusing given charset.byteat(int index) Returns byte from thisByteBufwhich index is equal to the passed value.booleancanRead()Checks if there are bytes available for reading if thisByteBufis not recycled.booleancanWrite()Checks if there are bytes available for writing if thisByteBufis not recycled.intdrainTo(byte[] array, int offset, int length) Drains bytes from thisByteBufstarting from currentheadto a given byte array with specified offset and length.intDrains bytes to a givenByteBuf.static ByteBufempty()Creates an emptyByteBufwith array of size 0,tailandheadboth equal to 0.intfind(byte b) Finds the given value in thearrayand returns its position.intfind(byte[] bytes) Finds the given byte array in thearrayand returns its position.intfind(byte[] bytes, int off, int len) Finds the given byte array in thearrayand returns its position.byteget()Returns the byte from thisByteBufwhich index is equal tohead.byte[]getArray()Returns a byte array fromheadtotail.Returns aStringcreated from thisByteBufusing given charset.inthead()Returnsheadif thisByteBufis not recycled.voidhead(int pos) Setsheadif thisByteBufis not recycled.booleanisContentEqual(byte[] array) Checks if provided array is equal to the readable bytes of thearray.booleanisContentEqual(byte[] array, int offset, int length) Checks if provided array is equal to the readable bytes of thearray.booleanisContentEqual(@NotNull ByteBuf other) Checks if providedByteBufreadable bytes are equal to the readable bytes of thearray.protected booleanChecks if thisByteBufis recycled.protected booleanChecks if thisByteBufneeds recycling by checking the value ofrefs.intlimit()Returns length of thearrayof thisByteBuf.voidmoveHead(int delta) Sets new value ofheadby moving it by the given delta if thisByteBufis not recycled.voidmoveTail(int delta) Sets new value oftailby moving it by the given delta if thisByteBufis not recycled.voidofReadByteBuffer(ByteBuffer byteBuffer) Unwraps given Java'sByteBufferintoByteBuf.voidofWriteByteBuffer(ByteBuffer byteBuffer) Unwraps given Java'sByteBufferintoByteBuf.bytepeek()bytepeek(int offset) Returns abytefrom thisarraywhich is located atheadposition increased by the offset if thisByteBufis not recycled.voidput(byte b) voidput(byte[] bytes) Puts given byte array to thearrayat thetailand increases thetailby the length of the given array.voidput(byte[] bytes, int offset, int length) Puts given byte array to theByteBuffrom thetailwith given offset.voidPuts given ByteBuf to thisByteBuffrom thetailand increases thetailby the length of the given ByteBuf.intread(byte[] b) intread(byte[] b, int off, int len) booleanbytereadByte()charreadChar()doublefloatintreadInt()longreadLong()intReturns the amount of bytes which are available for reading if thisByteBufis not recycled.shortintlongvoidrecycle()Recycles thisByteBufby returning it toByteBufPool.voidrewind()Setstailandheadof thisByteBufto 0.voidset(int index, byte b) @NotNull ByteBufslice()Creates a slice of thisByteBufif it is not recycled.@NotNull ByteBufslice(int length) Creates a slice of thisByteBufwith the given length if it is not recycled.@NotNull ByteBufslice(int offset, int length) Creates a slice of thisByteBufwith the given offset and length.inttail()Returnstailif thisByteBufis not recycled.voidtail(int pos) Setstailif thisByteBufis not recycled.Wraps thisByteBufinto Java'sByteBufferready to read.toString()Wraps thisByteBufinto Java'sByteBufferready to write.static @NotNull ByteBufwrap(byte[] bytes, int head, int tail) Wraps provided byte array intoByteBufwith specifiedtailandhead.static @NotNull ByteBufwrapForReading(byte[] bytes) Wraps provided byte array intoByteBufwithtailequal to length of provided array.static @NotNull ByteBufwrapForWriting(byte[] bytes) Wraps provided byte array intoByteBufwithtailequal to 0.voidwrite(byte[] b) voidwrite(byte[] b, int off, int len) voidwriteBoolean(boolean v) voidwriteByte(byte v) voidwriteChar(char v) voidwriteDouble(double v) voidwriteFloat(float v) voidwriteInt(int v) voidwriteLong(long v) intReturns the amount of bytes which are available for writing if thisByteBufis not recycled.voidwriteShort(short v) voidwriteVarInt(int v) voidwriteVarLong(long v)
-
Field Details
-
array
protected final byte[] arrayStores bytes of thisByteBuf.
-
-
Method Details
-
empty
Creates an emptyByteBufwith array of size 0,tailandheadboth equal to 0.- Returns:
- an empty
ByteBuf
-
wrapForWriting
Wraps provided byte array intoByteBufwithtailequal to 0.- Parameters:
bytes- byte array to be wrapped intoByteBuf- Returns:
ByteBufover underlying byte array that is ready for writing
-
wrapForReading
Wraps provided byte array intoByteBufwithtailequal to length of provided array.- Parameters:
bytes- byte array to be wrapped intoByteBuf- Returns:
ByteBufover underlying byte array that is ready for reading
-
wrap
@Contract("_, _, _ -> new") @NotNull public static @NotNull ByteBuf wrap(byte[] bytes, int head, int tail) Wraps provided byte array intoByteBufwith specifiedtailandhead.- Parameters:
bytes- byte array to be wrapped intoByteBufhead-headofByteBuftail-tailofByteBuf- Returns:
ByteBufover underlying byte array with giventailandhead
-
slice
Creates a slice of thisByteBufif it is not recycled. Itsheadandtailwon't change.refsincreases by 1.- Returns:
- a
ByteBuf.ByteBufSliceof thisByteBuf
-
slice
Creates a slice of thisByteBufwith the given length if it is not recycled.- Parameters:
length- length of the new slice. Definestailof the newByteBuf.ByteBufSlice. It is added to the currenthead.- Returns:
- a
ByteBufSliceof thisByteBuf.
-
slice
Creates a slice of thisByteBufwith the given offset and length.- Parameters:
offset- offset from which to slice thisByteBuf.length- length of the slice.- Returns:
- a
ByteBufSliceof thisByteBufwith the given offset and length.
-
recycle
public void recycle()Recycles thisByteBufby returning it toByteBufPool.- Specified by:
recyclein interfaceRecyclable
-
addRef
public void addRef()Increasesrefsvalue by 1. -
isRecycled
@Contract(pure=true) protected boolean isRecycled()Checks if thisByteBufis recycled.- Returns:
trueorfalse
-
rewind
public void rewind()Setstailandheadof thisByteBufto 0. -
isRecycleNeeded
@Contract(pure=true) protected boolean isRecycleNeeded()Checks if thisByteBufneeds recycling by checking the value ofrefs. If the value is greater than 0, returnstrue.- Returns:
trueif thisByteBufneeds recycle, otherwisefalse
-
toReadByteBuffer
Wraps thisByteBufinto Java'sByteBufferready to read.- Returns:
ByteBufferready to read
-
toWriteByteBuffer
Wraps thisByteBufinto Java'sByteBufferready to write.- Returns:
ByteBufferready to write
-
ofReadByteBuffer
Unwraps given Java'sByteBufferintoByteBuf.- Parameters:
byteBuffer-ByteBufferto be unwrapped
-
ofWriteByteBuffer
Unwraps given Java'sByteBufferintoByteBuf.- Parameters:
byteBuffer-ByteBufferto be unwrapped
-
array
@Contract(value="->!null", pure=true) public byte[] array()Returns byte arrayarray.- Returns:
array
-
limit
@Contract(pure=true) public int limit()Returns length of thearrayof thisByteBuf.- Returns:
- length of this
ByteBuf
-
head
@Contract(pure=true) public int head()Returnsheadif thisByteBufis not recycled.- Returns:
head
-
head
public void head(int pos) Setsheadif thisByteBufis not recycled.- Parameters:
pos- the value which will be assigned to thehead. Must be smaller or equal totail
-
tail
@Contract(pure=true) public int tail()Returnstailif thisByteBufis not recycled.- Returns:
tail
-
tail
public void tail(int pos) Setstailif thisByteBufis not recycled.- Parameters:
pos- the value which will be assigned to thetail. Must be bigger or equal toheadand smaller than length of thearray
-
moveHead
public void moveHead(int delta) Sets new value ofheadby moving it by the given delta if thisByteBufis not recycled.- Parameters:
delta- the value by which currentheadwill be moved. Newheadmust be bigger or equal to 0 and smaller or equal totail
-
moveTail
public void moveTail(int delta) Sets new value oftailby moving it by the given delta if thisByteBufis not recycled.- Parameters:
delta- the value by which currenttailwill be moved. Newtailmust be bigger or equal toheadand smaller or equal to the length of thearray
-
readRemaining
@Contract(pure=true) public int readRemaining()Returns the amount of bytes which are available for reading if thisByteBufis not recycled.- Returns:
- amount of bytes available for reading
-
writeRemaining
@Contract(pure=true) public int writeRemaining()Returns the amount of bytes which are available for writing if thisByteBufis not recycled.- Returns:
- amount of bytes available for writing
-
canRead
@Contract(pure=true) public boolean canRead()Checks if there are bytes available for reading if thisByteBufis not recycled.- Returns:
trueifheaddoesn't equaltail, otherwisefalse
-
canWrite
@Contract(pure=true) public boolean canWrite()Checks if there are bytes available for writing if thisByteBufis not recycled.- Returns:
trueiftaildoesn't equal the length of thearray, otherwisefalse
-
get
public byte get()Returns the byte from thisByteBufwhich index is equal tohead. Then increasesheadby 1.- Returns:
bytevalue at thehead
-
at
@Contract(pure=true) public byte at(int index) Returns byte from thisByteBufwhich index is equal to the passed value. Then increasesheadby 1.- Parameters:
index- index of the byte to be returned.- Returns:
- the
byteat the specified position.
-
peek
@Contract(pure=true) public byte peek()- Returns:
- a
bytefrom thisarraywhich is located athead.
-
peek
@Contract(pure=true) public byte peek(int offset) Returns abytefrom thisarraywhich is located atheadposition increased by the offset if thisByteBufis not recycled.headdoesn't change.- Parameters:
offset- added to theheadvalue. Received value must be smaller than currenttail.- Returns:
- a
bytefrom thisarraywhich is located atheadwith provided offset
-
drainTo
public int drainTo(byte[] array, int offset, int length) Drains bytes from thisByteBufstarting from currentheadto a given byte array with specified offset and length.This
ByteBufmust be not recycled.- Parameters:
array- array to which bytes will be drained tooffset- starting position in the destination data. The sum of the value and thelengthparameter must be smaller or equal toarraylengthlength- number of bytes to be drained to given array. Must be greater or equal to 0. The sum of the value andheadmust be smaller or equal totail- Returns:
- number of bytes that were drained.
-
drainTo
Drains bytes to a givenByteBuf.- See Also:
-
set
public void set(int index, byte b) -
put
public void put(byte b) - Parameters:
b- the byte which will be put to thearray.
-
put
Puts given ByteBuf to thisByteBuffrom thetailand increases thetailby the length of the given ByteBuf. Then equates ByteBuf'sheadtotail.Only those bytes which are located between
headandtailare put to thearray.- Parameters:
buf- the ByteBuf which will be put to theByteBuf
-
put
public void put(byte[] bytes) Puts given byte array to thearrayat thetailand increases thetailby the length of the given array.- Parameters:
bytes- the byte array which will be put to thearray
-
put
public void put(byte[] bytes, int offset, int length) Puts given byte array to theByteBuffrom thetailwith given offset. Increases thetailby the length of the given array.This
ByteBufmust be not recycled. Its length must be greater or equal to the sum of itstailand the length of the byte array which will be put in it. Also, the sum of the provided offset and length of the byte array which will be put to thearraymust smaller or equal to the whole length of the byte array. -
find
public int find(byte b) Finds the given value in thearrayand returns its position.This
ByteBufmust be not recycled. -
find
public int find(byte[] bytes) Finds the given byte array in thearrayand returns its position. ThisByteBufmust be not recycled. -
find
public int find(byte[] bytes, int off, int len) -
isContentEqual
@Contract(pure=true) public boolean isContentEqual(byte[] array, int offset, int length) Checks if provided array is equal to the readable bytes of thearray.- Parameters:
array- byte array to be compared with thearrayoffset- offset value for the provided byte arraylength- amount of the bytes to be compared- Returns:
trueif the byte array is equal to the array, otherwisefalse
-
isContentEqual
Checks if providedByteBufreadable bytes are equal to the readable bytes of thearray.- Parameters:
other-ByteBufto be compared with thearray- Returns:
trueif theByteBufis equal to the array, otherwisefalse
-
isContentEqual
@Contract(pure=true) public boolean isContentEqual(byte[] array) Checks if provided array is equal to the readable bytes of thearray.- Parameters:
array- byte array to be compared with thearray- Returns:
trueif the byte array is equal to the array, otherwisefalse
-
getArray
@Contract(value="->!null", pure=true) public byte[] getArray()- Returns:
- byte array from
headtotail
-
asArray
@Contract(value="->!null", pure=false) public byte[] asArray()Returns a byte array fromheadtotail. DOES recycle thisByteBuf.- Returns:
- byte array created from this
ByteBuf
-
getString
Returns aStringcreated from thisByteBufusing given charset. Does not recycle thisByteBuf.- Parameters:
charset- charset which is used to createStringfrom thisByteBuf.- Returns:
Stringfrom thisByteBufin a given charset.
-
asString
Returns aStringcreated from thisByteBufusing given charset. DOES recycle thisByteBuf.- Parameters:
charset- charset which is used to create string fromByteBuf- Returns:
Stringfrom thisByteBufin a given charset.
-
read
public int read(byte[] b) -
read
public int read(byte[] b, int off, int len) -
readByte
public byte readByte() -
readBoolean
public boolean readBoolean() -
readChar
public char readChar() -
readDouble
public double readDouble() -
readFloat
public float readFloat() -
readInt
public int readInt() -
readVarInt
public int readVarInt() -
readLong
public long readLong() -
readShort
public short readShort() -
readVarLong
public long readVarLong() -
write
public void write(byte[] b) -
write
public void write(byte[] b, int off, int len) -
writeBoolean
public void writeBoolean(boolean v) -
writeByte
public void writeByte(byte v) -
writeChar
public void writeChar(char v) -
writeDouble
public void writeDouble(double v) -
writeFloat
public void writeFloat(float v) -
writeInt
public void writeInt(int v) -
writeLong
public void writeLong(long v) -
writeShort
public void writeShort(short v) -
writeVarInt
public void writeVarInt(int v) -
writeVarLong
public void writeVarLong(long v) -
toString
-