Interface BufferedSource
-
- All Superinterfaces:
AutoCloseable,Closeable,Source
- All Known Implementing Classes:
Buffer
public interface BufferedSource extends Source
A source that keeps a buffer internally so that callers can do small reads without a performance penalty. It also allows clients to read ahead, buffering as much as necessary before consuming input.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Bufferbuffer()Returns this source's internal buffer.booleanexhausted()Returns true if there are no more bytes in this source.longindexOf(byte b)Returns the index of the firstbin the buffer.longindexOf(byte b, long fromIndex)Returns the index of the firstbin the buffer at or afterfromIndex.longindexOf(ByteString bytes)Returns the index of the first match forbytesin the buffer.longindexOf(ByteString bytes, long fromIndex)Returns the index of the first match forbytesin the buffer at or afterfromIndex.longindexOfElement(ByteString targetBytes)Returns the index of the first byte intargetBytesin the buffer.longindexOfElement(ByteString targetBytes, long fromIndex)Returns the index of the first byte intargetBytesin the buffer at or afterfromIndex.InputStreaminputStream()Returns an input stream that reads from this source.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.longreadAll(Sink sink)Removes all bytes from this and appends them tosink.bytereadByte()Removes a byte from this source and returns it.byte[]readByteArray()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.ByteStringreadByteString()Removes all bytes bytes from this and returns them as a byte string.ByteStringreadByteString(long byteCount)RemovesbyteCountbytes from this and returns them as a byte string.longreadDecimalLong()Reads 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.voidreadFully(Buffer sink, long byteCount)Removes exactlybyteCountbytes from this and appends them tosink.longreadHexadecimalUnsignedLong()Reads a long form this source in hexadecimal form (i.e., as a string in base 16).intreadInt()Removes four bytes from this source and returns a big-endian int.intreadIntLe()Removes four bytes from this source and returns a little-endian int.longreadLong()Removes eight bytes from this source and returns a big-endian long.longreadLongLe()Removes eight bytes from this source and returns a little-endian long.shortreadShort()Removes two bytes from this source and returns a big-endian short.shortreadShortLe()Removes two bytes from this source and returns a little-endian short.StringreadString(long byteCount, Charset charset)RemovesbyteCountbytes from this, decodes them ascharset, and returns the string.StringreadString(Charset charset)Removes all bytes from this, decodes them ascharset, and returns the string.StringreadUtf8()Removes all bytes from this, decodes them as UTF-8, and returns the string.StringreadUtf8(long byteCount)RemovesbyteCountbytes from this, decodes them as UTF-8, and returns the string.intreadUtf8CodePoint()Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary.StringreadUtf8Line()Removes and returns characters up to but not including the next line break.StringreadUtf8LineStrict()Removes and returns characters up to but not including the next line break.booleanrequest(long byteCount)Returns true when the buffer contains at leastbyteCountbytes, expanding it as necessary.voidrequire(long byteCount)Returns when the buffer contains at leastbyteCountbytes.voidskip(long byteCount)Reads and discardsbyteCountbytes from this source.
-
-
-
Method Detail
-
buffer
Buffer buffer()
Returns this source's internal buffer.
-
exhausted
boolean exhausted() throws IOExceptionReturns 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
-
require
void require(long byteCount) throws IOExceptionReturns when the buffer contains at leastbyteCountbytes. Throws anEOFExceptionif the source is exhausted before the required bytes can be read.- Throws:
IOException
-
request
boolean request(long byteCount) throws IOExceptionReturns true when the buffer contains at leastbyteCountbytes, expanding it as necessary. Returns false if the source is exhausted before the requested bytes can be read.- Throws:
IOException
-
readByte
byte readByte() throws IOExceptionRemoves a byte from this source and returns it.- Throws:
IOException
-
readShort
short readShort() throws IOExceptionRemoves two bytes from this source and returns a big-endian short.- Throws:
IOException
-
readShortLe
short readShortLe() throws IOExceptionRemoves two bytes from this source and returns a little-endian short.- Throws:
IOException
-
readInt
int readInt() throws IOExceptionRemoves four bytes from this source and returns a big-endian int.- Throws:
IOException
-
readIntLe
int readIntLe() throws IOExceptionRemoves four bytes from this source and returns a little-endian int.- Throws:
IOException
-
readLong
long readLong() throws IOExceptionRemoves eight bytes from this source and returns a big-endian long.- Throws:
IOException
-
readLongLe
long readLongLe() throws IOExceptionRemoves eight bytes from this source and returns a little-endian long.- Throws:
IOException
-
readDecimalLong
long readDecimalLong() throws IOExceptionReads 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.- Throws:
NumberFormatException- if the found digits do not fit into alongor a decimal number was not present.IOException
-
readHexadecimalUnsignedLong
long readHexadecimalUnsignedLong() throws IOExceptionReads 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.- Throws:
NumberFormatException- if the found hexadecimal does not fit into alongor hexadecimal was not found.IOException
-
skip
void skip(long byteCount) throws IOException
Reads and discardsbyteCountbytes from this source. Throws anEOFExceptionif the source is exhausted before the requested bytes can be skipped.- Throws:
IOException
-
readByteString
ByteString readByteString() throws IOException
Removes all bytes bytes from this and returns them as a byte string.- Throws:
IOException
-
readByteString
ByteString readByteString(long byteCount) throws IOException
RemovesbyteCountbytes from this and returns them as a byte string.- Throws:
IOException
-
readByteArray
byte[] readByteArray() throws IOExceptionRemoves all bytes from this and returns them as a byte array.- Throws:
IOException
-
readByteArray
byte[] readByteArray(long byteCount) throws IOExceptionRemovesbyteCountbytes from this and returns them as a byte array.- Throws:
IOException
-
read
int read(byte[] sink) throws IOException
Removes up tosink.lengthbytes from this and copies them intosink. Returns the number of bytes read, or -1 if this source is exhausted.- Throws:
IOException
-
readFully
void readFully(byte[] sink) throws IOExceptionRemoves exactlysink.lengthbytes from this and copies them intosink. Throws anEOFExceptionif the requested number of bytes cannot be read.- Throws:
IOException
-
read
int read(byte[] sink, int offset, int byteCount) throws IOExceptionRemoves up tobyteCountbytes from this and copies them intosinkatoffset. Returns the number of bytes read, or -1 if this source is exhausted.- Throws:
IOException
-
readFully
void readFully(Buffer sink, long byteCount) throws IOException
Removes exactlybyteCountbytes from this and appends them tosink. Throws anEOFExceptionif the requested number of bytes cannot be read.- Throws:
IOException
-
readAll
long readAll(Sink sink) throws IOException
Removes all bytes from this and appends them tosink. Returns the total number of bytes written tosinkwhich will be 0 if this is exhausted.- Throws:
IOException
-
readUtf8
String readUtf8() throws IOException
Removes all bytes from this, decodes them as UTF-8, and returns the string.- Throws:
IOException
-
readUtf8
String readUtf8(long byteCount) throws IOException
RemovesbyteCountbytes from this, decodes them as UTF-8, and returns the string.- Throws:
IOException
-
readUtf8Line
String readUtf8Line() throws IOException
Removes 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 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.- Throws:
IOException
-
readUtf8LineStrict
String readUtf8LineStrict() throws IOException
Removes 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
EOFExceptionis thrown. Use this for machine-generated data where a missing line break implies truncated input.- Throws:
IOException
-
readUtf8CodePoint
int readUtf8CodePoint() throws IOExceptionRemoves 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
EOFExceptionand 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
-
readString
String readString(Charset charset) throws IOException
Removes all bytes from this, decodes them ascharset, and returns the string.- Throws:
IOException
-
readString
String readString(long byteCount, Charset charset) throws IOException
RemovesbyteCountbytes from this, decodes them ascharset, and returns the string.- Throws:
IOException
-
indexOf
long indexOf(byte b) throws IOExceptionReturns the index of the firstbin the buffer. 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.- Throws:
IOException
-
indexOf
long indexOf(byte b, long fromIndex) throws IOExceptionReturns 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.- Throws:
IOException
-
indexOf
long indexOf(ByteString bytes) throws IOException
Returns the index of the first match forbytesin the buffer. 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.- Throws:
IOException
-
indexOf
long indexOf(ByteString bytes, long fromIndex) throws IOException
Returns 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.- Throws:
IOException
-
indexOfElement
long indexOfElement(ByteString targetBytes) throws IOException
Returns the index of the first byte intargetBytesin the buffer. This expands the buffer as necessary until a target byte is found. This reads an unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the requested byte is found.- Throws:
IOException
-
indexOfElement
long indexOfElement(ByteString targetBytes, long fromIndex) throws IOException
Returns the index of the first byte intargetBytesin the buffer at or afterfromIndex. This expands the buffer as necessary until a target byte is found. This reads an unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the requested byte is found.- Throws:
IOException
-
inputStream
InputStream inputStream()
Returns an input stream that reads from this source.
-
-