Package 

Interface BufferedSink

  • All Implemented Interfaces:
    com.mobwith.httpmodule.Sink , java.io.Closeable , java.io.Flushable , java.lang.AutoCloseable , java.nio.channels.Channel , java.nio.channels.WritableByteChannel

    
    public interface BufferedSink
     implements Sink, WritableByteChannel
                        

    A sink that keeps a buffer internally so that callers can do small writes without a performance penalty.

    • Method Summary

      Modifier and Type Method Description
      abstract Buffer buffer() Returns this sink's internal buffer.
      abstract BufferedSink write(ByteString byteString)
      abstract BufferedSink write(Array<byte> source) Like write, this writes a complete byte array tothis sink.
      abstract BufferedSink write(Array<byte> source, int offset, int byteCount) Like write, this writes {@code byteCount} bytes of {@code source}, starting at {@code offset}.
      abstract long writeAll(Source source) Removes all bytes from {@code source} and appends them to this sink.
      abstract BufferedSink write(Source source, long byteCount) Removes {@code byteCount} bytes from {@code source} and appends them to this sink.
      abstract BufferedSink writeUtf8(String string) Encodes {@code string} in UTF-8 and writes it to this sink.
      abstract BufferedSink writeUtf8(String string, int beginIndex, int endIndex) Encodes the characters at {@code beginIndex} up to {@code endIndex} from {@code string} inUTF-8 and writes it to this sink.
      abstract BufferedSink writeUtf8CodePoint(int codePoint) Encodes {@code codePoint} in UTF-8 and writes it to this sink.
      abstract BufferedSink writeString(String string, Charset charset) Encodes {@code string} in {@code charset} and writes it to this sink.
      abstract BufferedSink writeString(String string, int beginIndex, int endIndex, Charset charset) Encodes the characters at {@code beginIndex} up to {@code endIndex} from {@code string} in {@code charset} and writes it to this sink.
      abstract BufferedSink writeByte(int b) Writes a byte to this sink.
      abstract BufferedSink writeShort(int s) Writes a big-endian short to this sink using two bytes.
      abstract BufferedSink writeShortLe(int s) Writes a little-endian short to this sink using two bytes.
      abstract BufferedSink writeInt(int i) Writes a big-endian int to this sink using four bytes.
      abstract BufferedSink writeIntLe(int i) Writes a little-endian int to this sink using four bytes.
      abstract BufferedSink writeLong(long v) Writes a big-endian long to this sink using eight bytes.
      abstract BufferedSink writeLongLe(long v) Writes a little-endian long to this sink using eight bytes.
      abstract BufferedSink writeDecimalLong(long v) Writes a long to this sink in signed decimal form (i.e., as a string in base 10).
      abstract BufferedSink writeHexadecimalUnsignedLong(long v) Writes a long to this sink in hexadecimal form (i.e., as a string in base 16).
      abstract void flush() Writes all buffered data to the underlying sink, if one exists.
      abstract BufferedSink emit() Writes all buffered data to the underlying sink, if one exists.
      abstract BufferedSink emitCompleteSegments() Writes complete segments to the underlying sink, if one exists.
      abstract OutputStream outputStream() Returns an output stream that writes to this sink.
      • Methods inherited from class com.mobwith.httpmodule.Sink

        close, timeout, write
      • Methods inherited from class java.io.Closeable

        close
      • Methods inherited from class java.io.Flushable

        flush
      • Methods inherited from class java.nio.channels.WritableByteChannel

        write
      • Methods inherited from class java.nio.channels.Channel

        isOpen
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • buffer

         abstract Buffer buffer()

        Returns this sink's internal buffer.

      • write

         abstract BufferedSink write(Array<byte> source, int offset, int byteCount)

        Like write, this writes {@code byteCount} bytes of {@code source}, starting at {@code offset}.

      • writeAll

         abstract long writeAll(Source source)

        Removes all bytes from {@code source} and appends them to this sink. Returns thenumber of bytes read which will be 0 if {@code source} is exhausted.

      • write

         abstract BufferedSink write(Source source, long byteCount)

        Removes {@code byteCount} bytes from {@code source} and appends them to this sink.

      • writeUtf8

         abstract BufferedSink writeUtf8(String string)

        Encodes {@code string} in UTF-8 and writes it to this sink.

        {@code * * 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()); * }
      • writeUtf8

         abstract BufferedSink writeUtf8(String string, int beginIndex, int endIndex)

        Encodes the characters at {@code beginIndex} up to {@code endIndex} from {@code string} inUTF-8 and writes it to this sink.

        {@code * * 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()); * }
      • writeString

         abstract BufferedSink writeString(String string, int beginIndex, int endIndex, Charset charset)

        Encodes the characters at {@code beginIndex} up to {@code endIndex} from {@code string} in {@code charset} and writes it to this sink.

      • writeShort

         abstract BufferedSink writeShort(int s)

        Writes a big-endian short to this sink using two bytes.

        {@code * * 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()); * }
      • writeShortLe

         abstract BufferedSink writeShortLe(int s)

        Writes a little-endian short to this sink using two bytes.

        {@code * * 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()); * }
      • writeInt

         abstract BufferedSink writeInt(int i)

        Writes a big-endian int to this sink using four bytes.

        {@code * * 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()); * }
      • writeIntLe

         abstract BufferedSink writeIntLe(int i)

        Writes a little-endian int to this sink using four bytes.

        {@code * * 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()); * }
      • writeLong

         abstract BufferedSink writeLong(long v)

        Writes a big-endian long to this sink using eight bytes.

        {@code * * 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()); * }
      • writeLongLe

         abstract BufferedSink writeLongLe(long v)

        Writes a little-endian long to this sink using eight bytes.

        {@code * * 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()); * }
      • writeDecimalLong

         abstract BufferedSink writeDecimalLong(long v)

        Writes a long to this sink in signed decimal form (i.e., as a string in base 10).

        {@code * * Buffer buffer = new Buffer(); * buffer.writeDecimalLong(8675309L); * buffer.writeByte(' '); * buffer.writeDecimalLong(-123L); * buffer.writeByte(' '); * buffer.writeDecimalLong(1L); * * assertEquals("8675309 -123 1", buffer.readUtf8()); * }
      • writeHexadecimalUnsignedLong

         abstract BufferedSink writeHexadecimalUnsignedLong(long v)

        Writes a long to this sink in hexadecimal form (i.e., as a string in base 16).

        {@code * * Buffer buffer = new Buffer(); * buffer.writeHexadecimalUnsignedLong(65535L); * buffer.writeByte(' '); * buffer.writeHexadecimalUnsignedLong(0xcafebabeL); * buffer.writeByte(' '); * buffer.writeHexadecimalUnsignedLong(0x10L); * * assertEquals("ffff cafebabe 10", buffer.readUtf8()); * }
      • flush

         abstract void flush()

        Writes all buffered data to the underlying sink, if one exists. Then that sink is recursivelyflushed which pushes data as far as possible towards its ultimate destination. Typically thatdestination is a network socket or file.

        {@code * * BufferedSink b0 = new Buffer(); * BufferedSink b1 = MobonOkio.buffer(b0); * BufferedSink b2 = MobonOkio.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()); * }
      • emit

         abstract BufferedSink emit()

        Writes all buffered data to the underlying sink, if one exists. Like flush, butweaker. Call this before this buffered sink goes out of scope so that its data can reach itsdestination.

        {@code * * BufferedSink b0 = new Buffer(); * BufferedSink b1 = MobonOkio.buffer(b0); * BufferedSink b2 = MobonOkio.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()); * }
      • emitCompleteSegments

         abstract BufferedSink emitCompleteSegments()

        Writes complete segments to the underlying sink, if one exists. Like flush, butweaker. Use this to limit the memory held in the buffer to a single segment. Typicallyapplication code will not need to call this: it is only necessary when application code writesdirectly to this sink's buffer.

        {@code * * BufferedSink b0 = new Buffer(); * BufferedSink b1 = MobonOkio.buffer(b0); * BufferedSink b2 = MobonOkio.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. * }