Class RealSink

java.lang.Object
org.miaixz.bus.core.io.sink.RealSink
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Channel, WritableByteChannel, BufferSink, Sink

public final class RealSink extends Object implements BufferSink
原始流信息
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • buffer

      public final Buffer buffer
    • sink

      public final Sink sink
  • Constructor Details

    • RealSink

      public RealSink(Sink sink)
  • Method Details

    • buffer

      public Buffer buffer()
      Description copied from interface: BufferSink
      Returns this sink's internal buffer.
      Specified by:
      buffer in interface BufferSink
    • write

      public void write(Buffer source, long byteCount) throws IOException
      Description copied from interface: Sink
      Removes byteCount bytes from source and appends them to this.
      Specified by:
      write in interface Sink
      Throws:
      IOException
    • write

      public BufferSink write(ByteString byteString) throws IOException
      Specified by:
      write in interface BufferSink
      Throws:
      IOException
    • writeUtf8

      public BufferSink writeUtf8(String string) throws IOException
      Description copied from interface: BufferSink
      Encodes string in UTF-8 and writes it to this sink.
      
      
         Buffer buffer = new Buffer();
         buffer.writeUtf8("Uh uh uh!");
         buffer.writeByte(' ');
         buffer.writeUtf8("You didn't say the magic word!");
      
         assertEquals("Uh uh uh! You didn't say the magic word!", buffer.readUtf8());
       
      Specified by:
      writeUtf8 in interface BufferSink
      Throws:
      IOException
    • writeUtf8

      public BufferSink writeUtf8(String string, int beginIndex, int endIndex) throws IOException
      Description copied from interface: BufferSink
      Encodes the characters at beginIndex up to endIndex from string in UTF-8 and writes it to this sink.
      
      
         Buffer buffer = new Buffer();
         buffer.writeUtf8("I'm a hacker!\n", 6, 12);
         buffer.writeByte(' ');
         buffer.writeUtf8("That's what I said: you're a nerd.\n", 29, 33);
         buffer.writeByte(' ');
         buffer.writeUtf8("I prefer to be called a hacker!\n", 24, 31);
      
         assertEquals("hacker nerd hacker!", buffer.readUtf8());
       
      Specified by:
      writeUtf8 in interface BufferSink
      Throws:
      IOException
    • writeUtf8CodePoint

      public BufferSink writeUtf8CodePoint(int codePoint) throws IOException
      Description copied from interface: BufferSink
      Encodes codePoint in UTF-8 and writes it to this sink.
      Specified by:
      writeUtf8CodePoint in interface BufferSink
      Throws:
      IOException
    • writeString

      public BufferSink writeString(String string, Charset charset) throws IOException
      Description copied from interface: BufferSink
      Encodes string in charset and writes it to this sink.
      Specified by:
      writeString in interface BufferSink
      Throws:
      IOException
    • writeString

      public BufferSink writeString(String string, int beginIndex, int endIndex, Charset charset) throws IOException
      Description copied from interface: BufferSink
      Encodes the characters at beginIndex up to endIndex from string in charset and writes it to this sink.
      Specified by:
      writeString in interface BufferSink
      Throws:
      IOException
    • write

      public BufferSink write(byte[] source) throws IOException
      Description copied from interface: BufferSink
      Like OutputStream.write(byte[]), this writes a complete byte array to this sink.
      Specified by:
      write in interface BufferSink
      Throws:
      IOException
    • write

      public BufferSink write(byte[] source, int offset, int byteCount) throws IOException
      Description copied from interface: BufferSink
      Like OutputStream.write(byte[], int, int), this writes byteCount bytes of source, starting at offset.
      Specified by:
      write in interface BufferSink
      Throws:
      IOException
    • write

      public int write(ByteBuffer source) throws IOException
      Specified by:
      write in interface WritableByteChannel
      Throws:
      IOException
    • writeAll

      public long writeAll(Source source) throws IOException
      Description copied from interface: BufferSink
      Removes all bytes from source and appends them to this sink. Returns the number of bytes read which will be 0 if source is exhausted.
      Specified by:
      writeAll in interface BufferSink
      Throws:
      IOException
    • write

      public BufferSink write(Source source, long byteCount) throws IOException
      Description copied from interface: BufferSink
      Removes byteCount bytes from source and appends them to this sink.
      Specified by:
      write in interface BufferSink
      Throws:
      IOException
    • writeByte

      public BufferSink writeByte(int b) throws IOException
      Description copied from interface: BufferSink
      Writes a byte to this sink.
      Specified by:
      writeByte in interface BufferSink
      Throws:
      IOException
    • writeShort

      public BufferSink writeShort(int s) throws IOException
      Description copied from interface: BufferSink
      Writes a big-endian short to this sink using two bytes.
      
      
         Buffer buffer = new Buffer();
         buffer.writeShort(32767);
         buffer.writeShort(15);
      
         assertEquals(4, buffer.size());
         assertEquals((byte) 0x7f, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x0f, buffer.readByte());
         assertEquals(0, buffer.size());
       
      Specified by:
      writeShort in interface BufferSink
      Throws:
      IOException
    • writeShortLe

      public BufferSink writeShortLe(int s) throws IOException
      Description copied from interface: BufferSink
      Writes a little-endian short to this sink using two bytes.
      
      
         Buffer buffer = new Buffer();
         buffer.writeShortLe(32767);
         buffer.writeShortLe(15);
      
         assertEquals(4, buffer.size());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0x7f, buffer.readByte());
         assertEquals((byte) 0x0f, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals(0, buffer.size());
       
      Specified by:
      writeShortLe in interface BufferSink
      Throws:
      IOException
    • writeInt

      public BufferSink writeInt(int i) throws IOException
      Description copied from interface: BufferSink
      Writes a big-endian int to this sink using four bytes.
      
      
         Buffer buffer = new Buffer();
         buffer.writeInt(2147483647);
         buffer.writeInt(15);
      
         assertEquals(8, buffer.size());
         assertEquals((byte) 0x7f, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x0f, buffer.readByte());
         assertEquals(0, buffer.size());
       
      Specified by:
      writeInt in interface BufferSink
      Throws:
      IOException
    • writeIntLe

      public BufferSink writeIntLe(int i) throws IOException
      Description copied from interface: BufferSink
      Writes a little-endian int to this sink using four bytes.
      
      
         Buffer buffer = new Buffer();
         buffer.writeIntLe(2147483647);
         buffer.writeIntLe(15);
      
         assertEquals(8, buffer.size());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0x7f, buffer.readByte());
         assertEquals((byte) 0x0f, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals(0, buffer.size());
       
      Specified by:
      writeIntLe in interface BufferSink
      Throws:
      IOException
    • writeLong

      public BufferSink writeLong(long v) throws IOException
      Description copied from interface: BufferSink
      Writes a big-endian long to this sink using eight bytes.
      
      
         Buffer buffer = new Buffer();
         buffer.writeLong(9223372036854775807L);
         buffer.writeLong(15);
      
         assertEquals(16, buffer.size());
         assertEquals((byte) 0x7f, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x0f, buffer.readByte());
         assertEquals(0, buffer.size());
       
      Specified by:
      writeLong in interface BufferSink
      Throws:
      IOException
    • writeLongLe

      public BufferSink writeLongLe(long v) throws IOException
      Description copied from interface: BufferSink
      Writes a little-endian long to this sink using eight bytes.
      
      
         Buffer buffer = new Buffer();
         buffer.writeLongLe(9223372036854775807L);
         buffer.writeLongLe(15);
      
         assertEquals(16, buffer.size());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0xff, buffer.readByte());
         assertEquals((byte) 0x7f, buffer.readByte());
         assertEquals((byte) 0x0f, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals((byte) 0x00, buffer.readByte());
         assertEquals(0, buffer.size());
       
      Specified by:
      writeLongLe in interface BufferSink
      Throws:
      IOException
    • writeDecimalLong

      public BufferSink writeDecimalLong(long v) throws IOException
      Description copied from interface: BufferSink
      Writes a long to this sink in signed decimal form (i.e., as a string in base 10).
      
      
         Buffer buffer = new Buffer();
         buffer.writeDecimalLong(8675309L);
         buffer.writeByte(' ');
         buffer.writeDecimalLong(-123L);
         buffer.writeByte(' ');
         buffer.writeDecimalLong(1L);
      
         assertEquals("8675309 -123 1", buffer.readUtf8());
       
      Specified by:
      writeDecimalLong in interface BufferSink
      Throws:
      IOException
    • writeHexadecimalUnsignedLong

      public BufferSink writeHexadecimalUnsignedLong(long v) throws IOException
      Description copied from interface: BufferSink
      Writes a long to this sink in hexadecimal form (i.e., as a string in base 16).
      
      
         Buffer buffer = new Buffer();
         buffer.writeHexadecimalUnsignedLong(65535L);
         buffer.writeByte(' ');
         buffer.writeHexadecimalUnsignedLong(0xcafebabeL);
         buffer.writeByte(' ');
         buffer.writeHexadecimalUnsignedLong(0x10L);
      
         assertEquals("ffff cafebabe 10", buffer.readUtf8());
       
      Specified by:
      writeHexadecimalUnsignedLong in interface BufferSink
      Throws:
      IOException
    • emitCompleteSegments

      public BufferSink emitCompleteSegments() throws IOException
      Description copied from interface: BufferSink
      Writes complete segments to the underlying sink, if one exists. Like BufferSink.flush(), but weaker. Use this to limit the memory held in the buffer to a single segment. Typically application code will not need to call this: it is only necessary when application code writes directly to this sink's buffer.
      
      
         BufferedSink b0 = new Buffer();
         BufferedSink b1 = Okio.buffer(b0);
         BufferedSink b2 = Okio.buffer(b1);
      
         b2.buffer().write(new byte[20_000]);
         assertEquals(20_000, b2.buffer().size());
         assertEquals(     0, b1.buffer().size());
         assertEquals(     0, b0.buffer().size());
      
         b2.emitCompleteSegments();
         assertEquals( 3_616, b2.buffer().size());
         assertEquals(     0, b1.buffer().size());
         assertEquals(16_384, b0.buffer().size()); // This example assumes 8192 byte segments.
       
      Specified by:
      emitCompleteSegments in interface BufferSink
      Throws:
      IOException
    • emit

      public BufferSink emit() throws IOException
      Description copied from interface: BufferSink
      Writes all buffered data to the underlying sink, if one exists. Like BufferSink.flush(), but weaker. Call this before this buffered sink goes out of scope so that its data can reach its destination.
      
      
         BufferedSink b0 = new Buffer();
         BufferedSink b1 = Okio.buffer(b0);
         BufferedSink b2 = Okio.buffer(b1);
      
         b2.writeUtf8("hello");
         assertEquals(5, b2.buffer().size());
         assertEquals(0, b1.buffer().size());
         assertEquals(0, b0.buffer().size());
      
         b2.emit();
         assertEquals(0, b2.buffer().size());
         assertEquals(5, b1.buffer().size());
         assertEquals(0, b0.buffer().size());
      
         b1.emit();
         assertEquals(0, b2.buffer().size());
         assertEquals(0, b1.buffer().size());
         assertEquals(5, b0.buffer().size());
       
      Specified by:
      emit in interface BufferSink
      Throws:
      IOException
    • outputStream

      public OutputStream outputStream()
      Description copied from interface: BufferSink
      Returns an output stream that writes to this sink.
      Specified by:
      outputStream in interface BufferSink
    • flush

      public void flush() throws IOException
      Description copied from interface: BufferSink
      Writes all buffered data to the underlying sink, if one exists. Then that sink is recursively flushed which pushes data as far as possible towards its ultimate destination. Typically that destination is a network socket or file.
      
      
         BufferedSink b0 = new Buffer();
         BufferedSink b1 = Okio.buffer(b0);
         BufferedSink b2 = Okio.buffer(b1);
      
         b2.writeUtf8("hello");
         assertEquals(5, b2.buffer().size());
         assertEquals(0, b1.buffer().size());
         assertEquals(0, b0.buffer().size());
      
         b2.flush();
         assertEquals(0, b2.buffer().size());
         assertEquals(0, b1.buffer().size());
         assertEquals(5, b0.buffer().size());
       
      Specified by:
      flush in interface BufferSink
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in interface Sink
      Throws:
      IOException
    • isOpen

      public boolean isOpen()
      Specified by:
      isOpen in interface Channel
    • close

      public void close()
      Description copied from interface: Sink
      Pushes all buffered bytes to their final destination and releases the resources held by this sink. It is an error to write a closed sink. It is safe to close a sink more than once.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface Sink
    • timeout

      public Timeout timeout()
      Description copied from interface: Sink
      Returns the timeout for this sink.
      Specified by:
      timeout in interface Sink
      Returns:
      the Timeout
    • toString

      public String toString()
      Overrides:
      toString in class Object