Class RealSource
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,ReadableByteChannel,BufferSource,Source
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuffer()voidclose()关闭此源并释放此源持有的资源.booleanlongindexOf(byte b) Equivalent toindexOf(b, 0).longindexOf(byte b, long fromIndex) Returns the index of the firstbin the buffer at or afterfromIndex.longindexOf(byte b, long fromIndex, long toIndex) Returns the index ofbif it is found in the range offromIndexinclusive totoIndexexclusive.longindexOf(ByteString bytes) Equivalent toindexOf(bytes, 0).longindexOf(ByteString bytes, long fromIndex) Returns the index of the first match forbytesin the buffer at or afterfromIndex.longindexOfElement(ByteString targetBytes) longindexOfElement(ByteString targetBytes, long fromIndex) an input stream that reads from this source.booleanisOpen()peek()a newBufferSourcethat can read data from thisBufferSourcewithout consuming it.booleanrangeEquals(long offset, ByteString bytes) true if the bytes atoffsetin this source equalbytes.booleanrangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount) ifbyteCountbytes atoffsetin this source equalbytes* atbytesOffset.intread(byte[] sink) Removes up tosink.lengthbytes from this and copies them intosink.intread(byte[] sink, int offset, int byteCount) Removes up tobyteCountbytes from this and copies them intosinkatoffset.intread(ByteBuffer sink) long从中删除至少1个字节,最多为byteCount字节, 并将它们 附加到sink。返回读取的字节数,如果该源已耗尽,则返回-1longRemoves all bytes from this and appends them tosink.bytereadByte()byte[]Removes all bytes from this and returns them as a byte array.byte[]readByteArray(long byteCount) RemovesbyteCountbytes from this and returns them as a byte array.Removes all bytes bytes from this and returns them as a byte string.readByteString(long byteCount) RemovesbyteCountbytes from this and returns them as a byte string.longReads a long from this source in signed decimal form (i.e., as a string in base 10 with optional leading '-').voidreadFully(byte[] sink) Removes exactlysink.lengthbytes from this and copies them intosink.voidRemoves exactlybyteCountbytes from this and appends them tosink.longReads a long form this source in hexadecimal form (i.e., as a string in base 16).intreadInt()intlongreadLong()longRemoves eight bytes from this source and returns a little-endian long.shortshortRemoves two bytes from this source and returns a little-endian short.readString(long byteCount, Charset charset) RemovesbyteCountbytes from this, decodes them ascharset,readString(Charset charset) readUtf8()Removes all bytes from this, decodes them as UTF-8, and returns the string.readUtf8(long byteCount) RemovesbyteCountbytes from this, decodes them as UTF-8, and returns the string.intRemoves and returns characters up to but not including the next line break.Removes and returns characters up to but not including the next line break.readUtf8LineStrict(long limit) LikeBufferSource.readUtf8LineStrict(), except this allows the caller to specify the longest allowed match.booleanrequest(long byteCount) voidrequire(long byteCount) when the buffer contains at leastbyteCountbytes.intFinds the first string inoptionsthat is a prefix of this buffer, consumes it from this buffer, and returns its index.voidskip(long byteCount) Reads and discardsbyteCountbytes from this source.timeout()返回此源的超时时间.toString()
-
Field Details
-
buffer
-
source
-
-
Constructor Details
-
RealSource
-
-
Method Details
-
buffer
- Specified by:
bufferin interfaceBufferSource- Returns:
- this source's internal buffer. use getBuffer() instead.
-
getBuffer
- Specified by:
getBufferin interfaceBufferSource- Returns:
- This source's internal buffer.
-
read
Description copied from interface:Source从中删除至少1个字节,最多为byteCount字节, 并将它们 附加到sink。返回读取的字节数,如果该源已耗尽,则返回-1- Specified by:
readin interfaceSource- Parameters:
sink- 缓冲byteCount- 长度大小- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
exhausted
- Specified by:
exhaustedin interfaceBufferSource- Returns:
- true if there are no more bytes in this source. This will block until there are bytes to read or the source is definitely exhausted.
- Throws:
IOException-IOExceptionIOException.
-
require
Description copied from interface:BufferSourcewhen the buffer contains at leastbyteCountbytes.- Specified by:
requirein interfaceBufferSource- Parameters:
byteCount- long- Throws:
IOException-IOExceptionIOException.
-
request
- Specified by:
requestin interfaceBufferSource- Parameters:
byteCount- long- Returns:
- true when the buffer contains at least
byteCountbytes, expanding it as necessary. Returns false if the source is exhausted before the requested bytes can be read. - Throws:
IOException-IOExceptionIOException.
-
readByte
- Specified by:
readBytein interfaceBufferSource- Returns:
- Removes a byte from this source and returns it.
- Throws:
IOException-IOExceptionIOException.
-
readByteString
Description copied from interface:BufferSourceRemoves all bytes bytes from this and returns them as a byte string.- Specified by:
readByteStringin interfaceBufferSource- Returns:
- the ByteString
- Throws:
IOException-IOExceptionIOException.
-
readByteString
Description copied from interface:BufferSourceRemovesbyteCountbytes from this and returns them as a byte string.- Specified by:
readByteStringin interfaceBufferSource- Parameters:
byteCount- long- Returns:
- the ByteString
- Throws:
IOException-IOExceptionIOException.
-
select
Description copied from interface:BufferSourceFinds the first string inoptionsthat is a prefix of this buffer, consumes it from this buffer, and returns its index. If no byte string inoptionsis a prefix of this buffer this returns -1 and no bytes are consumed.This can be used as an alternative to
BufferSource.readByteString()or evenBufferSource.readUtf8()if the set of expected values is known in advance.Options FIELDS = Options.of( ByteString.encodeUtf8("depth="), ByteString.encodeUtf8("height="), ByteString.encodeUtf8("width=")); Buffer buffer = new Buffer() .writeUtf8("width=640\n") .writeUtf8("height=480\n"); assertEquals(2, buffer.select(FIELDS)); assertEquals(640, buffer.readDecimalLong()); assertEquals('\n', buffer.readByte()); assertEquals(1, buffer.select(FIELDS)); assertEquals(480, buffer.readDecimalLong()); assertEquals('\n', buffer.readByte());- Specified by:
selectin interfaceBufferSource- Parameters:
options- Options- Returns:
- the int
- Throws:
IOException-IOExceptionIOException.
-
readByteArray
Description copied from interface:BufferSourceRemoves all bytes from this and returns them as a byte array.- Specified by:
readByteArrayin interfaceBufferSource- Returns:
- the byte[]
- Throws:
IOException-IOExceptionIOException.
-
readByteArray
Description copied from interface:BufferSourceRemovesbyteCountbytes from this and returns them as a byte array.- Specified by:
readByteArrayin interfaceBufferSource- Parameters:
byteCount- long- Returns:
- the byte[]
- Throws:
IOException-IOExceptionIOException.
-
read
Description copied from interface:BufferSourceRemoves up tosink.lengthbytes from this and copies them intosink. Returns the number of bytes read, or -1 if this source is exhausted.- Specified by:
readin interfaceBufferSource- Parameters:
sink- byte[]- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
readFully
Description copied from interface:BufferSourceRemoves exactlysink.lengthbytes from this and copies them intosink. Throws anIOExceptionif the requested number of bytes cannot be read.- Specified by:
readFullyin interfaceBufferSource- Parameters:
sink- byte[]- Throws:
IOException-IOExceptionIOException.
-
read
Description copied from interface:BufferSourceRemoves up tobyteCountbytes from this and copies them intosinkatoffset. Returns the number of bytes read, or -1 if this source is exhausted.- Specified by:
readin interfaceBufferSource- Parameters:
sink- byte[]offset- intbyteCount- int- Returns:
- the int
- Throws:
IOException-IOExceptionIOException.
-
read
- Specified by:
readin interfaceReadableByteChannel- Throws:
IOException
-
readFully
Description copied from interface:BufferSourceRemoves exactlybyteCountbytes from this and appends them tosink. Throws anIOExceptionif the requested number of bytes cannot be read.- Specified by:
readFullyin interfaceBufferSource- Parameters:
sink- BufferbyteCount- long- Throws:
IOException-IOExceptionIOException.
-
readAll
Description copied from interface:BufferSourceRemoves all bytes from this and appends them tosink. Returns the total number of bytes written tosinkwhich will be 0 if this is exhausted.- Specified by:
readAllin interfaceBufferSource- Parameters:
sink- Sink- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
readUtf8
Description copied from interface:BufferSourceRemoves all bytes from this, decodes them as UTF-8, and returns the string. Returns the empty string if this source is empty.Buffer buffer = new Buffer() .writeUtf8("Uh uh uh!") .writeByte(' ') .writeUtf8("You didn't say the magic word!"); assertEquals("Uh uh uh! You didn't say the magic word!", buffer.readUtf8()); assertEquals(0, buffer.size()); assertEquals("", buffer.readUtf8()); assertEquals(0, buffer.size());- Specified by:
readUtf8in interfaceBufferSource- Returns:
- the String
- Throws:
IOException-IOExceptionIOException.
-
readUtf8
Description copied from interface:BufferSourceRemovesbyteCountbytes from this, decodes them as UTF-8, and returns the string.Buffer buffer = new Buffer() .writeUtf8("Uh uh uh!") .writeByte(' ') .writeUtf8("You didn't say the magic word!"); assertEquals(40, buffer.size()); assertEquals("Uh uh uh! You ", buffer.readUtf8(14)); assertEquals(26, buffer.size()); assertEquals("didn't say the", buffer.readUtf8(14)); assertEquals(12, buffer.size()); assertEquals(" magic word!", buffer.readUtf8(12)); assertEquals(0, buffer.size());- Specified by:
readUtf8in interfaceBufferSource- Parameters:
byteCount- long- Returns:
- the String
- Throws:
IOException-IOExceptionIOException.
-
readString
- Specified by:
readStringin interfaceBufferSource- Parameters:
charset- Charset Removes all bytes from this, decodes them ascharset,- Returns:
- the string.
- Throws:
IOException-IOExceptionIOException.
-
readString
Description copied from interface:BufferSourceRemovesbyteCountbytes from this, decodes them ascharset,- Specified by:
readStringin interfaceBufferSource- Parameters:
byteCount- byteCountcharset- Charset- Returns:
- the string.
- Throws:
IOException-IOExceptionIOException.
-
readUtf8Line
Description copied from interface:BufferSourceRemoves and returns characters up to but not including the next line break. A line break is either"\n"or"\r\n"; these characters are not included in the result.Buffer buffer = new Buffer() .writeUtf8("I'm a hacker!\n") .writeUtf8("That's what I said: you're a nerd.\n") .writeUtf8("I prefer to be called a hacker!\n"); assertEquals(81, buffer.size()); assertEquals("I'm a hacker!", buffer.readUtf8Line()); assertEquals(67, buffer.size()); assertEquals("That's what I said: you're a nerd.", buffer.readUtf8Line()); assertEquals(32, buffer.size()); assertEquals("I prefer to be called a hacker!", buffer.readUtf8Line()); assertEquals(0, buffer.size()); assertEquals(null, buffer.readUtf8Line()); assertEquals(0, buffer.size());On the end of the stream this method returns null, just like
BufferedReader. If the source doesn't end with a line break then an implicit line break is assumed. Null is returned once the source is exhausted. Use this for human-generated data, where a trailing line break is optional.- Specified by:
readUtf8Linein interfaceBufferSource- Returns:
- the String
- Throws:
IOException-IOExceptionIOException.
-
readUtf8LineStrict
Description copied from interface:BufferSourceRemoves and returns characters up to but not including the next line break. A line break is either"\n"or"\r\n"; these characters are not included in the result.On the end of the stream this method throws. Every call must consume either '\r\n' or '\n'. If these characters are absent in the stream, an
IOExceptionis thrown. Use this for machine-generated data where a missing line break implies truncated input.- Specified by:
readUtf8LineStrictin interfaceBufferSource- Returns:
- the String
- Throws:
IOException-IOExceptionIOException.
-
readUtf8LineStrict
Description copied from interface:BufferSourceLikeBufferSource.readUtf8LineStrict(), except this allows the caller to specify the longest allowed match. Use this to protect against streams that may not include"\n"or"\r\n".The returned string will have at most
limitUTF-8 bytes, and the maximum number of bytes scanned islimit + 2. Iflimit == 0this will always throw anIOExceptionbecause no bytes will be scanned.This method is safe. No bytes are discarded if the match fails, and the caller is free to try another match:
Buffer buffer = new Buffer(); buffer.writeUtf8("12345\r\n"); // This will throw! There must be \r\n or \n at the limit or before it. buffer.readUtf8LineStrict(4); // No bytes have been consumed so the caller can retry. assertEquals("12345", buffer.readUtf8LineStrict(5));- Specified by:
readUtf8LineStrictin interfaceBufferSource- Parameters:
limit- long- Returns:
- String String
- Throws:
IOException-IOExceptionIOException.
-
readUtf8CodePoint
- Specified by:
readUtf8CodePointin interfaceBufferSource- Returns:
- Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary.
If this source is exhausted before a complete code point can be read, this throws an
IOExceptionand consumes no input.If this source doesn't start with a properly-encoded UTF-8 code point, this method will remove 1 or more non-UTF-8 bytes and return the replacement character (
U+FFFD). This covers encoding problems (the input is not properly-encoded UTF-8), characters out of range (beyond the 0x10ffff limit of Unicode), code points for UTF-16 surrogates (U+d800..U+dfff) and overlong encodings (such as0xc080for the NUL character in modified UTF-8). - Throws:
IOException-IOExceptionIOException.
-
readShort
- Specified by:
readShortin interfaceBufferSource- Returns:
- two bytes from this source and returns a big-endian short.
Buffer buffer = new Buffer() .writeByte(0x7f) .writeByte(0xff) .writeByte(0x00) .writeByte(0x0f); assertEquals(4, buffer.size()); assertEquals(32767, buffer.readShort()); assertEquals(2, buffer.size()); assertEquals(15, buffer.readShort()); assertEquals(0, buffer.size()); - Throws:
IOException-IOExceptionIOException.
-
readShortLe
Description copied from interface:BufferSourceRemoves two bytes from this source and returns a little-endian short.Buffer buffer = new Buffer() .writeByte(0xff) .writeByte(0x7f) .writeByte(0x0f) .writeByte(0x00); assertEquals(4, buffer.size()); assertEquals(32767, buffer.readShortLe()); assertEquals(2, buffer.size()); assertEquals(15, buffer.readShortLe()); assertEquals(0, buffer.size());- Specified by:
readShortLein interfaceBufferSource- Returns:
- the short
- Throws:
IOException-IOExceptionIOException.
-
readInt
- Specified by:
readIntin interfaceBufferSource- Returns:
- the int
- Throws:
IOException-IOExceptionIOException.
-
readIntLe
- Specified by:
readIntLein interfaceBufferSource- Returns:
- the int
- Throws:
IOException-IOExceptionIOException.
-
readLong
- Specified by:
readLongin interfaceBufferSource- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
readLongLe
Description copied from interface:BufferSourceRemoves eight bytes from this source and returns a little-endian long.Buffer buffer = new Buffer() .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0x7f) .writeByte(0x0f) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00); assertEquals(16, buffer.size()); assertEquals(9223372036854775807L, buffer.readLongLe()); assertEquals(8, buffer.size()); assertEquals(15, buffer.readLongLe()); assertEquals(0, buffer.size());- Specified by:
readLongLein interfaceBufferSource- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
readDecimalLong
Description copied from interface:BufferSourceReads a long from this source in signed decimal form (i.e., as a string in base 10 with optional leading '-'). This will iterate until a non-digit character is found.Buffer buffer = new Buffer() .writeUtf8("8675309 -123 00001"); assertEquals(8675309L, buffer.readDecimalLong()); assertEquals(' ', buffer.readByte()); assertEquals(-123L, buffer.readDecimalLong()); assertEquals(' ', buffer.readByte()); assertEquals(1L, buffer.readDecimalLong());- Specified by:
readDecimalLongin interfaceBufferSource- Returns:
- the long
- Throws:
IOException- if the found digits do not fit into alongor a decimal number was not present.
-
readHexadecimalUnsignedLong
Description copied from interface:BufferSourceReads a long form this source in hexadecimal form (i.e., as a string in base 16). This will iterate until a non-hexadecimal character is found.Buffer buffer = new Buffer() .writeUtf8("ffff CAFEBABE 10"); assertEquals(65535L, buffer.readHexadecimalUnsignedLong()); assertEquals(' ', buffer.readByte()); assertEquals(0xcafebabeL, buffer.readHexadecimalUnsignedLong()); assertEquals(' ', buffer.readByte()); assertEquals(0x10L, buffer.readHexadecimalUnsignedLong());- Specified by:
readHexadecimalUnsignedLongin interfaceBufferSource- Returns:
- the long
- Throws:
IOException- if the found hexadecimal does not fit into alongor hexadecimal was not found.
-
skip
Description copied from interface:BufferSourceReads and discardsbyteCountbytes from this source. Throws anIOExceptionif the source is exhausted before the requested bytes can be skipped.- Specified by:
skipin interfaceBufferSource- Parameters:
byteCount- long- Throws:
IOException-IOExceptionIOException.
-
indexOf
Description copied from interface:BufferSourceEquivalent toindexOf(b, 0).- Specified by:
indexOfin interfaceBufferSource- Parameters:
b- byte- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
indexOf
Description copied from interface:BufferSourceReturns the index of the firstbin the buffer at or afterfromIndex. This expands the buffer as necessary untilbis found. This reads an unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the requested byte is found.Buffer buffer = new Buffer(); buffer.writeUtf8("Don't move! He can't see us if we don't move."); byte m = 'm'; assertEquals(6, buffer.indexOf(m)); assertEquals(40, buffer.indexOf(m, 12));- Specified by:
indexOfin interfaceBufferSource- Parameters:
b- bytefromIndex- long- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
indexOf
Description copied from interface:BufferSourceReturns the index ofbif it is found in the range offromIndexinclusive totoIndexexclusive. Ifbisn't found, or iffromIndex == toIndex, then -1 is returned.The scan terminates at either
toIndexor the end of the buffer, whichever comes first. The maximum number of bytes scanned istoIndex-fromIndex.- Specified by:
indexOfin interfaceBufferSource- Parameters:
b- bytefromIndex- longtoIndex- long- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
indexOf
Description copied from interface:BufferSourceEquivalent toindexOf(bytes, 0).- Specified by:
indexOfin interfaceBufferSource- Parameters:
bytes- ByteString- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
indexOf
Description copied from interface:BufferSourceReturns the index of the first match forbytesin the buffer at or afterfromIndex. This expands the buffer as necessary untilbytesis found. This reads an unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the requested bytes are found.ByteString MOVE = ByteString.encodeUtf8("move"); Buffer buffer = new Buffer(); buffer.writeUtf8("Don't move! He can't see us if we don't move."); assertEquals(6, buffer.indexOf(MOVE)); assertEquals(40, buffer.indexOf(MOVE, 12));- Specified by:
indexOfin interfaceBufferSource- Parameters:
bytes- ByteStringfromIndex- long- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
indexOfElement
- Specified by:
indexOfElementin interfaceBufferSource- Parameters:
targetBytes- ByteString Equivalent toindexOfElement(targetBytes, 0).- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
indexOfElement
- Specified by:
indexOfElementin interfaceBufferSource- Parameters:
targetBytes- ByteStringfromIndex- long- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
rangeEquals
Description copied from interface:BufferSourcetrue if the bytes atoffsetin this source equalbytes. This expands the buffer as necessary until a byte does not match, all bytes are matched, or if the stream is exhausted before enough bytes could determine a match.ByteString simonSays = ByteString.encodeUtf8("Simon says:"); Buffer standOnOneLeg = new Buffer().writeUtf8("Simon says: Stand on first leg."); assertTrue(standOnOneLeg.rangeEquals(0, simonSays)); Buffer payMeMoney = new Buffer().writeUtf8("Pay me $1,000,000."); assertFalse(payMeMoney.rangeEquals(0, simonSays));- Specified by:
rangeEqualsin interfaceBufferSource- Parameters:
offset- longbytes- ByteString- Returns:
- the long
- Throws:
IOException-IOExceptionIOException.
-
rangeEquals
public boolean rangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount) throws IOException Description copied from interface:BufferSourceifbyteCountbytes atoffsetin this source equalbytes* atbytesOffset. This expands the buffer as necessary until a byte does not match, all * bytes are matched, or if the stream is exhausted before enough bytes could determine a match.- Specified by:
rangeEqualsin interfaceBufferSource- Parameters:
offset- longbytes- ByteStringbytesOffset- intbyteCount- int- Returns:
- the true
- Throws:
IOException-IOExceptionIOException.
-
peek
Description copied from interface:BufferSourcea newBufferSourcethat can read data from thisBufferSourcewithout consuming it. The returned source becomes invalid once this source is next read or closed.For example, we can use
peek()to lookahead and read the same data multiple times.Buffer buffer = new Buffer(); buffer.writeUtf8("abcdefghi"); buffer.readUtf8(3) // returns "abc", buffer contains "defghi" BufferSource peek = buffer.peek(); peek.readUtf8(3); // returns "def", buffer contains "defghi" peek.readUtf8(3); // returns "ghi", buffer contains "defghi" buffer.readUtf8(3); // returns "def", buffer contains "ghi"- Specified by:
peekin interfaceBufferSource- Returns:
- the long
-
inputStream
Description copied from interface:BufferSourcean input stream that reads from this source.- Specified by:
inputStreamin interfaceBufferSource- Returns:
- the InputStream
-
isOpen
public boolean isOpen() -
close
Description copied from interface:Source关闭此源并释放此源持有的资源. 读取闭源是一个错误。多次关闭源是安全的.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceSource- Throws:
IOException-IOExceptionIOException.
-
timeout
Description copied from interface:Source返回此源的超时时间. -
toString
-