Class CircularBuffer

java.lang.Object
org.miaixz.bus.core.io.buffer.CircularBuffer

public class CircularBuffer extends Object
循环缓冲区
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
    默认缓冲大小的构造
    CircularBuffer(int pSize)
    构造
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(byte value)
    增加byte到buffer中
    void
    add(byte[] targetBuffer, int offset, int length)
    Adds the given bytes to the buffer.
    void
    Removes all bytes from the buffer.
    int
    Returns the number of bytes, that are currently present in the buffer.
    int
    Returns the number of bytes, that can currently be added to the buffer.
    boolean
    Returns, whether the buffer is currently holding, at least, a single byte.
    boolean
    Returns, whether there is currently room for a single byte in the buffer.
    boolean
    hasSpace(int count)
    Returns, whether there is currently room for the given number of bytes in the buffer.
    boolean
    peek(byte[] sourceBuffer, int offset, int length)
    Returns, whether the next bytes in the buffer are exactly those, given by sourceBuffer, offset, and length.
    byte
    从buffer中读取下一个byte,同时移除这个bytes。
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CircularBuffer

      public CircularBuffer()
      默认缓冲大小的构造
    • CircularBuffer

      public CircularBuffer(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 - 目标bytes
      targetOffset - 目标数组开始位置
      length - 读取长度
      Throws:
      NullPointerException - 提供的数组为null
      IllegalArgumentException - targetOffsetlength 为负数或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 by sourceBuffer, offset, and length. No bytes are being removed from the buffer. If the result is true, then the following invocations of read() are guaranteed to return exactly those bytes.
      Parameters:
      sourceBuffer - the buffer to compare against
      offset - start offset
      length - length to compare
      Returns:
      True, if the next invocations of read() will return the bytes at offsets pOffset+0, pOffset+1, ..., pOffset+pLength-1 of byte array pBuffer.
      Throws:
      IllegalArgumentException - Either of pOffset, or pLength is negative.
      NullPointerException - The byte array pBuffer is null.
    • add

      public void add(byte[] targetBuffer, int offset, int length)
      Adds the given bytes to the buffer. This is the same as invoking add(byte) for the bytes at offsets offset+0, offset+1, ..., offset+length-1 of byte array targetBuffer.
      Parameters:
      targetBuffer - the buffer to copy
      offset - start offset
      length - length to copy
      Throws:
      IllegalStateException - The buffer doesn't have sufficient space. Use getSpace() to prevent this exception.
      IllegalArgumentException - Either of pOffset, or pLength is negative.
      NullPointerException - The byte array pBuffer is null.
    • hasSpace

      public boolean hasSpace()
      Returns, whether there is currently room for a single byte in the buffer. Same as hasSpace(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.