Module bus.socket

Class WriteBuffer

java.lang.Object
java.io.OutputStream
org.miaixz.bus.socket.buffer.WriteBuffer
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public final class WriteBuffer extends OutputStream
包装当前会话分配到的虚拟Buffer,提供流式操作方式
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

  • Method Details

    • write

      public void write(int b)
      按照OutputStream.write(int)规范:要写入的字节是参数 b 的八个低位。 b 的 24 个高位将被忽略。 而使用该接口时容易传入非byte范围内的数据,接口定义与实际使用出现歧义的可能性较大,故建议废弃该方法,选用writeByte(byte)
      Specified by:
      write in class OutputStream
      Parameters:
      b - 输出字节
    • writeShort

      public void writeShort(short v) throws IOException
      输出一个short类型的数据
      Parameters:
      v - short数值
      Throws:
      IOException - IO异常
    • writeByte

      public void writeByte(byte b)
      Parameters:
      b - 待输出数值
      See Also:
    • writeInt

      public void writeInt(int v) throws IOException
      输出int数值,占用4个字节
      Parameters:
      v - int数值
      Throws:
      IOException - IO异常
    • writeLong

      public void writeLong(long v) throws IOException
      输出long数值,占用8个字节
      Parameters:
      v - long数值
      Throws:
      IOException - IO异常
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] bytes, int offset, int len, Consumer<WriteBuffer> consumer) throws IOException
      执行异步输出操作 此方法会将指定的字节流异步写入,并在完成时通知提供的消费者。
      Parameters:
      bytes - 待输出的字节流。
      offset - 字节流中开始输出的偏移量。
      len - 要输出的字节数。
      consumer - 完成输出后调用的消费者接口,用于处理写入完成后的缓冲区。
      Throws:
      IOException - 如果在写入过程中发生I/O错误。
      WritePendingException - 如果已有写入操作未完成,此时再调用此方法会抛出此异常。
    • write

      public void write(byte[] bytes, Consumer<WriteBuffer> consumer) throws IOException
      Throws:
      IOException
    • transferFrom

      public void transferFrom(ByteBuffer byteBuffer, Consumer<WriteBuffer> consumer) throws IOException
      Throws:
      IOException
    • flush

      public void flush()
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
    • isEmpty

      public boolean isEmpty()
      是否存在待输出的数据
      Returns:
      true:有,false:无
    • finishWrite

      public void finishWrite()
    • poll

      public VirtualBuffer poll()
      获取并移除当前缓冲队列中头部的VirtualBuffer
      Returns:
      待输出的VirtualBuffer