Package org.miaixz.bus.core.io
Class CircularByteBuffer
java.lang.Object
org.miaixz.bus.core.io.CircularByteBuffer
循环缓冲区
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(byte value) 增加byte到buffer中voidadd(byte[] targetBuffer, int offset, int length) Adds the given bytes to the buffer.voidclear()Removes all bytes from the buffer.intReturns the number of bytes, that are currently present in the buffer.intgetSpace()Returns the number of bytes, that can currently be added to the buffer.booleanhasBytes()Returns, whether the buffer is currently holding, at least, a single byte.booleanhasSpace()Returns, whether there is currently room for a single byte in the buffer.booleanhasSpace(int count) Returns, whether there is currently room for the given number of bytes in the buffer.booleanpeek(byte[] sourceBuffer, int offset, int length) Returns, whether the next bytes in the buffer are exactly those, given bysourceBuffer,offset, andlength.byteread()从buffer中读取下一个byte,同时移除这个bytes。voidread(byte[] targetBuffer, int targetOffset, int length) Returns the given number of bytes from the buffer by storing them in the given byte array at the given offset.
-
Constructor Details
-
CircularByteBuffer
public CircularByteBuffer()默认缓冲大小的构造(Normal.DEFAULT_BUFFER_SIZE) -
CircularByteBuffer
public CircularByteBuffer(int pSize) 构造- Parameters:
pSize- 缓冲大小
-
-
Method Details
-
read
public byte read()从buffer中读取下一个byte,同时移除这个bytes。- Returns:
- The byte
- Throws:
IllegalStateException- buffer为空抛出,使用hasBytes(),或getCurrentNumberOfBytes()判断
-
read
public void read(byte[] targetBuffer, int targetOffset, int length) Returns the given number of bytes from the buffer by storing them in the given byte array at the given offset. 从buffer中获取指定长度的bytes,从给定的targetBuffer的targetOffset位置写出- Parameters:
targetBuffer- 目标bytestargetOffset- 目标数组开始位置length- 读取长度- Throws:
NullPointerException- 提供的数组为nullIllegalArgumentException-targetOffset或length为负数或targetBuffer太小IllegalStateException- buffer中的byte不足,使用getCurrentNumberOfBytes()判断。
-
add
public void add(byte value) 增加byte到buffer中- Parameters:
value- The byte- Throws:
IllegalStateException- buffer已满. 用hasSpace()或getSpace()判断。
-
peek
public boolean peek(byte[] sourceBuffer, int offset, int length) Returns, whether the next bytes in the buffer are exactly those, given bysourceBuffer,offset, andlength. No bytes are being removed from the buffer. If the result is true, then the following invocations ofread()are guaranteed to return exactly those bytes.- Parameters:
sourceBuffer- the buffer to compare againstoffset- start offsetlength- length to compare- Returns:
- True, if the next invocations of
read()will return the bytes at offsetspOffset+0,pOffset+1, ...,pOffset+pLength-1 of byte arraypBuffer. - Throws:
IllegalArgumentException- Either ofpOffset, orpLengthis negative.NullPointerException- The byte arraypBufferis null.
-
add
public void add(byte[] targetBuffer, int offset, int length) Adds the given bytes to the buffer. This is the same as invokingadd(byte)for the bytes at offsetsoffset+0,offset+1, ...,offset+length-1of byte arraytargetBuffer.- Parameters:
targetBuffer- the buffer to copieroffset- start offsetlength- length to copier- Throws:
IllegalStateException- The buffer doesn't have sufficient space. UsegetSpace()to prevent this exception.IllegalArgumentException- Either ofpOffset, orpLengthis negative.NullPointerException- The byte arraypBufferis null.
-
hasSpace
public boolean hasSpace()Returns, whether there is currently room for a single byte in the buffer. Same ashasSpace(1).- Returns:
- true if there is space for a byte
- See Also:
-
hasSpace
public boolean hasSpace(int count) Returns, whether there is currently room for the given number of bytes in the buffer.- Parameters:
count- the byte count- Returns:
- true if there is space for the given number of bytes
- See Also:
-
hasBytes
public boolean hasBytes()Returns, whether the buffer is currently holding, at least, a single byte.- Returns:
- true if the buffer is not empty
-
getSpace
public int getSpace()Returns the number of bytes, that can currently be added to the buffer.- Returns:
- the number of bytes that can be added
-
getCurrentNumberOfBytes
public int getCurrentNumberOfBytes()Returns the number of bytes, that are currently present in the buffer.- Returns:
- the number of bytes
-
clear
public void clear()Removes all bytes from the buffer.
-