Package org.aoju.bus.core.io.sink
Class RealSink
java.lang.Object
org.aoju.bus.core.io.sink.RealSink
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,Channel,WritableByteChannel,BufferSink,Sink
原始流信息
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuffer()Returns this sink's internal buffer.voidclose()Pushes all buffered bytes to their final destination and releases the resources held by this sink.emit()Writes all buffered data to the underlying sink, if one exists.Writes complete segments to the underlying sink, if one exists.voidflush()Writes all buffered data to the underlying sink, if one exists.booleanisOpen()Returns an output stream that writes to this sink.timeout()Returns the timeout for this sink.toString()write(byte[] source) LikeOutputStream.write(byte[]), this writes a complete byte array to this sink.write(byte[] source, int offset, int byteCount) LikeOutputStream.write(byte[], int, int), this writesbyteCountbytes ofsource, starting atoffset.intwrite(ByteBuffer source) voidRemovesbyteCountbytes fromsourceand appends them to this.write(ByteString byteString) RemovesbyteCountbytes fromsourceand appends them to this sink.longRemoves all bytes fromsourceand appends them to this sink.writeByte(int b) Writes a byte to this sink.writeDecimalLong(long v) Writes a long to this sink in signed decimal form (i.e., as a string in base 10).writeHexadecimalUnsignedLong(long v) Writes a long to this sink in hexadecimal form (i.e., as a string in base 16).writeInt(int i) Writes a big-endian int to this sink using four bytes.writeIntLe(int i) Writes a little-endian int to this sink using four bytes.writeLong(long v) Writes a big-endian long to this sink using eight bytes.writeLongLe(long v) Writes a little-endian long to this sink using eight bytes.writeShort(int s) Writes a big-endian short to this sink using two bytes.writeShortLe(int s) Writes a little-endian short to this sink using two bytes.writeString(String string, int beginIndex, int endIndex, Charset charset) Encodes the characters atbeginIndexup toendIndexfromstringincharsetand writes it to this sink.writeString(String string, Charset charset) Encodesstringincharsetand writes it to this sink.Encodesstringin UTF-8 and writes it to this sink.Encodes the characters atbeginIndexup toendIndexfromstringin UTF-8 and writes it to this sink.writeUtf8CodePoint(int codePoint) EncodescodePointin UTF-8 and writes it to this sink.
-
Field Details
-
buffer
-
sink
-
-
Constructor Details
-
RealSink
-
-
Method Details
-
buffer
Description copied from interface:BufferSinkReturns this sink's internal buffer.- Specified by:
bufferin interfaceBufferSink
-
write
Description copied from interface:SinkRemovesbyteCountbytes fromsourceand appends them to this.- Specified by:
writein interfaceSink- Throws:
IOException
-
write
- Specified by:
writein interfaceBufferSink- Throws:
IOException
-
writeUtf8
Description copied from interface:BufferSinkEncodesstringin 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:
writeUtf8in interfaceBufferSink- Throws:
IOException
-
writeUtf8
Description copied from interface:BufferSinkEncodes the characters atbeginIndexup toendIndexfromstringin 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:
writeUtf8in interfaceBufferSink- Throws:
IOException
-
writeUtf8CodePoint
Description copied from interface:BufferSinkEncodescodePointin UTF-8 and writes it to this sink.- Specified by:
writeUtf8CodePointin interfaceBufferSink- Throws:
IOException
-
writeString
Description copied from interface:BufferSinkEncodesstringincharsetand writes it to this sink.- Specified by:
writeStringin interfaceBufferSink- Throws:
IOException
-
writeString
public BufferSink writeString(String string, int beginIndex, int endIndex, Charset charset) throws IOException Description copied from interface:BufferSinkEncodes the characters atbeginIndexup toendIndexfromstringincharsetand writes it to this sink.- Specified by:
writeStringin interfaceBufferSink- Throws:
IOException
-
write
Description copied from interface:BufferSinkLikeOutputStream.write(byte[]), this writes a complete byte array to this sink.- Specified by:
writein interfaceBufferSink- Throws:
IOException
-
write
Description copied from interface:BufferSinkLikeOutputStream.write(byte[], int, int), this writesbyteCountbytes ofsource, starting atoffset.- Specified by:
writein interfaceBufferSink- Throws:
IOException
-
write
- Specified by:
writein interfaceWritableByteChannel- Throws:
IOException
-
writeAll
Description copied from interface:BufferSinkRemoves all bytes fromsourceand appends them to this sink. Returns the number of bytes read which will be 0 ifsourceis exhausted.- Specified by:
writeAllin interfaceBufferSink- Throws:
IOException
-
write
Description copied from interface:BufferSinkRemovesbyteCountbytes fromsourceand appends them to this sink.- Specified by:
writein interfaceBufferSink- Throws:
IOException
-
writeByte
Description copied from interface:BufferSinkWrites a byte to this sink.- Specified by:
writeBytein interfaceBufferSink- Throws:
IOException
-
writeShort
Description copied from interface:BufferSinkWrites 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:
writeShortin interfaceBufferSink- Throws:
IOException
-
writeShortLe
Description copied from interface:BufferSinkWrites 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:
writeShortLein interfaceBufferSink- Throws:
IOException
-
writeInt
Description copied from interface:BufferSinkWrites 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:
writeIntin interfaceBufferSink- Throws:
IOException
-
writeIntLe
Description copied from interface:BufferSinkWrites 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:
writeIntLein interfaceBufferSink- Throws:
IOException
-
writeLong
Description copied from interface:BufferSinkWrites 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:
writeLongin interfaceBufferSink- Throws:
IOException
-
writeLongLe
Description copied from interface:BufferSinkWrites 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:
writeLongLein interfaceBufferSink- Throws:
IOException
-
writeDecimalLong
Description copied from interface:BufferSinkWrites 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:
writeDecimalLongin interfaceBufferSink- Throws:
IOException
-
writeHexadecimalUnsignedLong
Description copied from interface:BufferSinkWrites 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:
writeHexadecimalUnsignedLongin interfaceBufferSink- Throws:
IOException
-
emitCompleteSegments
Description copied from interface:BufferSinkWrites complete segments to the underlying sink, if one exists. LikeBufferSink.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:
emitCompleteSegmentsin interfaceBufferSink- Throws:
IOException
-
emit
Description copied from interface:BufferSinkWrites all buffered data to the underlying sink, if one exists. LikeBufferSink.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:
emitin interfaceBufferSink- Throws:
IOException
-
outputStream
Description copied from interface:BufferSinkReturns an output stream that writes to this sink.- Specified by:
outputStreamin interfaceBufferSink
-
flush
Description copied from interface:BufferSinkWrites 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:
flushin interfaceBufferSink- Specified by:
flushin interfaceFlushable- Specified by:
flushin interfaceSink- Throws:
IOException
-
isOpen
-
close
public void close()Description copied from interface:SinkPushes 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. -
timeout
-
toString
-