final class RealBufferedSink extends java.lang.Object implements BufferedSink
| Modifier and Type | Field and Description |
|---|---|
Buffer |
buffer |
(package private) boolean |
closed |
Sink |
sink |
| Constructor and Description |
|---|
RealBufferedSink(Sink sink) |
| Modifier and Type | Method and Description |
|---|---|
Buffer |
buffer()
Returns this sink's internal buffer.
|
void |
close()
Pushes all buffered bytes to their final destination and releases the
resources held by this sink.
|
BufferedSink |
emit()
Writes all buffered data to the underlying sink, if one exists.
|
BufferedSink |
emitCompleteSegments()
Writes complete segments to the underlying sink, if one exists.
|
void |
flush()
Writes all buffered data to the underlying sink, if one exists.
|
boolean |
isOpen() |
java.io.OutputStream |
outputStream()
Returns an output stream that writes to this sink.
|
Timeout |
timeout()
Returns the timeout for this sink.
|
java.lang.String |
toString() |
void |
write(Buffer source,
long byteCount)
Removes
byteCount bytes from source and appends them to this. |
BufferedSink |
write(byte[] source)
Like
OutputStream.write(byte[]), this writes a complete byte array to
this sink. |
BufferedSink |
write(byte[] source,
int offset,
int byteCount)
Like
OutputStream.write(byte[], int, int), this writes byteCount
bytes of source, starting at offset. |
int |
write(java.nio.ByteBuffer source) |
BufferedSink |
write(ByteString byteString) |
BufferedSink |
write(Source source,
long byteCount)
Removes
byteCount bytes from source and appends them to this sink. |
long |
writeAll(Source source)
Removes all bytes from
source and appends them to this sink. |
BufferedSink |
writeByte(int b)
Writes a byte to this sink.
|
BufferedSink |
writeDecimalLong(long v)
Writes a long to this sink in signed decimal form (i.e., as a string in base 10).
|
BufferedSink |
writeHexadecimalUnsignedLong(long v)
Writes a long to this sink in hexadecimal form (i.e., as a string in base 16).
|
BufferedSink |
writeInt(int i)
Writes a big-endian int to this sink using four bytes.
|
BufferedSink |
writeIntLe(int i)
Writes a little-endian int to this sink using four bytes.
|
BufferedSink |
writeLong(long v)
Writes a big-endian long to this sink using eight bytes.
|
BufferedSink |
writeLongLe(long v)
Writes a little-endian long to this sink using eight bytes.
|
BufferedSink |
writeShort(int s)
Writes a big-endian short to this sink using two bytes.
|
BufferedSink |
writeShortLe(int s)
Writes a little-endian short to this sink using two bytes.
|
BufferedSink |
writeString(java.lang.String string,
java.nio.charset.Charset charset)
Encodes
string in charset and writes it to this sink. |
BufferedSink |
writeString(java.lang.String string,
int beginIndex,
int endIndex,
java.nio.charset.Charset charset)
Encodes the characters at
beginIndex up to endIndex from string in
charset and writes it to this sink. |
BufferedSink |
writeUtf8(java.lang.String string)
Encodes
string in UTF-8 and writes it to this sink. |
BufferedSink |
writeUtf8(java.lang.String string,
int beginIndex,
int endIndex)
Encodes the characters at
beginIndex up to endIndex from string in
UTF-8 and writes it to this sink. |
BufferedSink |
writeUtf8CodePoint(int codePoint)
Encodes
codePoint in UTF-8 and writes it to this sink. |
RealBufferedSink(Sink sink)
public Buffer buffer()
BufferedSinkbuffer in interface BufferedSinkpublic void write(Buffer source, long byteCount) throws java.io.IOException
SinkbyteCount bytes from source and appends them to this.public BufferedSink write(ByteString byteString) throws java.io.IOException
write in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeUtf8(java.lang.String string) throws java.io.IOException
BufferedSinkstring 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());
writeUtf8 in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeUtf8(java.lang.String string, int beginIndex, int endIndex) throws java.io.IOException
BufferedSinkbeginIndex 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());
writeUtf8 in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeUtf8CodePoint(int codePoint) throws java.io.IOException
BufferedSinkcodePoint in UTF-8 and writes it to this sink.writeUtf8CodePoint in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeString(java.lang.String string, java.nio.charset.Charset charset) throws java.io.IOException
BufferedSinkstring in charset and writes it to this sink.writeString in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeString(java.lang.String string, int beginIndex, int endIndex, java.nio.charset.Charset charset) throws java.io.IOException
BufferedSinkbeginIndex up to endIndex from string in
charset and writes it to this sink.writeString in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink write(byte[] source) throws java.io.IOException
BufferedSinkOutputStream.write(byte[]), this writes a complete byte array to
this sink.write in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink write(byte[] source, int offset, int byteCount) throws java.io.IOException
BufferedSinkOutputStream.write(byte[], int, int), this writes byteCount
bytes of source, starting at offset.write in interface BufferedSinkjava.io.IOExceptionpublic int write(java.nio.ByteBuffer source)
throws java.io.IOException
write in interface java.nio.channels.WritableByteChanneljava.io.IOExceptionpublic long writeAll(Source source) throws java.io.IOException
BufferedSinksource and appends them to this sink. Returns the
number of bytes read which will be 0 if source is exhausted.writeAll in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink write(Source source, long byteCount) throws java.io.IOException
BufferedSinkbyteCount bytes from source and appends them to this sink.write in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeByte(int b) throws java.io.IOException
BufferedSinkwriteByte in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeShort(int s) throws java.io.IOException
BufferedSink
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());
writeShort in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeShortLe(int s) throws java.io.IOException
BufferedSink
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());
writeShortLe in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeInt(int i) throws java.io.IOException
BufferedSink
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());
writeInt in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeIntLe(int i) throws java.io.IOException
BufferedSink
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());
writeIntLe in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeLong(long v) throws java.io.IOException
BufferedSink
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());
writeLong in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeLongLe(long v) throws java.io.IOException
BufferedSink
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());
writeLongLe in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeDecimalLong(long v) throws java.io.IOException
BufferedSink
Buffer buffer = new Buffer();
buffer.writeDecimalLong(8675309L);
buffer.writeByte(' ');
buffer.writeDecimalLong(-123L);
buffer.writeByte(' ');
buffer.writeDecimalLong(1L);
assertEquals("8675309 -123 1", buffer.readUtf8());
writeDecimalLong in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink writeHexadecimalUnsignedLong(long v) throws java.io.IOException
BufferedSink
Buffer buffer = new Buffer();
buffer.writeHexadecimalUnsignedLong(65535L);
buffer.writeByte(' ');
buffer.writeHexadecimalUnsignedLong(0xcafebabeL);
buffer.writeByte(' ');
buffer.writeHexadecimalUnsignedLong(0x10L);
assertEquals("ffff cafebabe 10", buffer.readUtf8());
writeHexadecimalUnsignedLong in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink emitCompleteSegments() throws java.io.IOException
BufferedSinkBufferedSink.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.
emitCompleteSegments in interface BufferedSinkjava.io.IOExceptionpublic BufferedSink emit() throws java.io.IOException
BufferedSinkBufferedSink.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());
emit in interface BufferedSinkjava.io.IOExceptionpublic java.io.OutputStream outputStream()
BufferedSinkoutputStream in interface BufferedSinkpublic void flush()
throws java.io.IOException
BufferedSink
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());
flush in interface java.io.Flushableflush in interface BufferedSinkflush in interface Sinkjava.io.IOExceptionpublic boolean isOpen()
isOpen in interface java.nio.channels.Channelpublic void close()
throws java.io.IOException
Sinkpublic Timeout timeout()
Sinkpublic java.lang.String toString()
toString in class java.lang.Object