public final class Buffer extends java.lang.Object implements BufferedSource, BufferedSink, java.lang.Cloneable
Moving data from one buffer to another is fast. Instead of copying bytes from one place in memory to another, this class just changes ownership of the underlying byte arrays.
This buffer grows with your data. Just like ArrayList, each buffer starts small. It consumes only the memory it needs to.
This buffer pools its byte arrays. When you allocate a byte array in Java, the runtime must zero-fill the requested array before returning it to you. Even if you're going to write over that space anyway. This class avoids zero-fill and GC churn by pooling byte arrays.
| 构造器和说明 |
|---|
Buffer() |
| 限定符和类型 | 方法和说明 |
|---|---|
Buffer |
buffer()
Returns this source's internal buffer.
|
void |
clear()
Discards all bytes in this buffer.
|
Buffer |
clone()
Returns a deep copy of this buffer.
|
void |
close()
Closes this source and releases the resources held by this source.
|
long |
completeSegmentByteCount()
Returns the number of bytes in segments that are not writable.
|
Buffer |
copyTo(Buffer out,
long offset,
long byteCount)
Copy
byteCount bytes from this, starting at offset, to out. |
Buffer |
copyTo(java.io.OutputStream out)
Copy the contents of this to
out. |
Buffer |
copyTo(java.io.OutputStream out,
long offset,
long byteCount)
Copy
byteCount bytes from this, starting at offset, to
out. |
BufferedSink |
emit()
Writes all buffered data to the underlying sink, if one exists.
|
Buffer |
emitCompleteSegments()
Writes complete segments to the underlying sink, if one exists.
|
boolean |
equals(java.lang.Object o) |
boolean |
exhausted()
Returns true if there are no more bytes in this source.
|
void |
flush()
Pushes all buffered bytes to their final destination.
|
byte |
getByte(long pos)
Returns the byte at
pos. |
int |
hashCode() |
long |
indexOf(byte b)
Returns the index of the first
b in the buffer. |
long |
indexOf(byte b,
long fromIndex)
Returns the index of
b in this at or beyond fromIndex, or
-1 if this buffer does not contain b in that range. |
long |
indexOf(ByteString bytes)
Returns the index of the first match for
bytes in the buffer. |
long |
indexOf(ByteString bytes,
long fromIndex)
Returns the index of the first match for
bytes in the buffer at or after fromIndex. |
long |
indexOfElement(ByteString targetBytes)
Returns the index of the first byte in
targetBytes in the buffer. |
long |
indexOfElement(ByteString targetBytes,
long fromIndex)
Returns the index of the first byte in
targetBytes in the buffer
at or after fromIndex. |
java.io.InputStream |
inputStream()
Returns an input stream that reads from this source.
|
java.io.OutputStream |
outputStream()
Returns an output stream that writes to this sink.
|
long |
read(Buffer sink,
long byteCount)
Removes at least 1, and up to
byteCount bytes from this and appends
them to sink. |
int |
read(byte[] sink)
Removes up to
sink.length bytes from this and copies them into sink. |
int |
read(byte[] sink,
int offset,
int byteCount)
Removes up to
byteCount bytes from this and copies them into sink at
offset. |
long |
readAll(Sink sink)
Removes all bytes from this and appends them to
sink. |
byte |
readByte()
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)
Removes
byteCount bytes from this and returns them as a byte array. |
ByteString |
readByteString()
Removes all bytes bytes from this and returns them as a byte string.
|
ByteString |
readByteString(long byteCount)
Removes
byteCount bytes from this and returns them as a byte string. |
long |
readDecimalLong()
Reads a long from this source in signed decimal form (i.e., as a string in base 10 with
optional leading '-').
|
Buffer |
readFrom(java.io.InputStream in)
Read and exhaust bytes from
in to this. |
Buffer |
readFrom(java.io.InputStream in,
long byteCount)
Read
byteCount bytes from in to this. |
void |
readFully(Buffer sink,
long byteCount)
Removes exactly
byteCount bytes from this and appends them to
sink. |
void |
readFully(byte[] sink)
Removes exactly
sink.length bytes from this and copies them into sink. |
long |
readHexadecimalUnsignedLong()
Reads a long form this source in hexadecimal form (i.e., as a string in base 16).
|
int |
readInt()
Removes four bytes from this source and returns a big-endian int.
|
int |
readIntLe()
Removes four bytes from this source and returns a little-endian int.
|
long |
readLong()
Removes eight bytes from this source and returns a big-endian long.
|
long |
readLongLe()
Removes eight bytes from this source and returns a little-endian long.
|
short |
readShort()
Removes two bytes from this source and returns a big-endian short.
|
short |
readShortLe()
Removes two bytes from this source and returns a little-endian short.
|
java.lang.String |
readString(java.nio.charset.Charset charset)
Removes all bytes from this, decodes them as
charset, and returns
the string. |
java.lang.String |
readString(long byteCount,
java.nio.charset.Charset charset)
Removes
byteCount bytes from this, decodes them as charset,
and returns the string. |
java.lang.String |
readUtf8()
Removes all bytes from this, decodes them as UTF-8, and returns the string.
|
java.lang.String |
readUtf8(long byteCount)
Removes
byteCount bytes from this, decodes them as UTF-8, and
returns the string. |
int |
readUtf8CodePoint()
Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary.
|
java.lang.String |
readUtf8Line()
Removes and returns characters up to but not including the next line break.
|
java.lang.String |
readUtf8LineStrict()
Removes and returns characters up to but not including the next line break.
|
boolean |
request(long byteCount)
Returns true when the buffer contains at least
byteCount bytes,
expanding it as necessary. |
void |
require(long byteCount)
Returns when the buffer contains at least
byteCount bytes. |
long |
size()
Returns the number of bytes currently in this buffer.
|
void |
skip(long byteCount)
Discards
byteCount bytes from the head of this buffer. |
ByteString |
snapshot()
Returns an immutable copy of this buffer as a byte string.
|
ByteString |
snapshot(int byteCount)
Returns an immutable copy of the first
byteCount bytes of this buffer as a byte string. |
Timeout |
timeout()
Returns the timeout for this source.
|
java.lang.String |
toString() |
void |
write(Buffer source,
long byteCount)
Removes
byteCount bytes from source and appends them to this. |
Buffer |
write(byte[] source)
Like
OutputStream.write(byte[]), this writes a complete byte array to
this sink. |
Buffer |
write(byte[] source,
int offset,
int byteCount)
Like
OutputStream.write(byte[], int, int), this writes byteCount
bytes of source, starting at offset. |
Buffer |
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. |
Buffer |
writeByte(int b)
Writes a byte to this sink.
|
Buffer |
writeDecimalLong(long v)
Writes a long to this sink in signed decimal form (i.e., as a string in base 10).
|
Buffer |
writeHexadecimalUnsignedLong(long v)
Writes a long to this sink in hexadecimal form (i.e., as a string in base 16).
|
Buffer |
writeInt(int i)
Writes a big-endian int to this sink using four bytes.
|
Buffer |
writeIntLe(int i)
Writes a little-endian int to this sink using four bytes.
|
Buffer |
writeLong(long v)
Writes a big-endian long to this sink using eight bytes.
|
Buffer |
writeLongLe(long v)
Writes a little-endian long to this sink using eight bytes.
|
Buffer |
writeShort(int s)
Writes a big-endian short to this sink using two bytes.
|
Buffer |
writeShortLe(int s)
Writes a little-endian short to this sink using two bytes.
|
Buffer |
writeString(java.lang.String string,
java.nio.charset.Charset charset)
Encodes
string in charset and writes it to this sink. |
Buffer |
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. |
Buffer |
writeTo(java.io.OutputStream out)
Write the contents of this to
out. |
Buffer |
writeTo(java.io.OutputStream out,
long byteCount)
Write
byteCount bytes from this to out. |
Buffer |
writeUtf8(java.lang.String string)
Encodes
string in UTF-8 and writes it to this sink. |
Buffer |
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. |
Buffer |
writeUtf8CodePoint(int codePoint)
Encodes
codePoint in UTF-8 and writes it to this sink. |
public long size()
public Buffer buffer()
BufferedSourcebuffer 在接口中 BufferedSinkbuffer 在接口中 BufferedSourcepublic java.io.OutputStream outputStream()
BufferedSinkoutputStream 在接口中 BufferedSinkpublic Buffer emitCompleteSegments()
BufferedSinkSink.flush(), but
weaker. Use this to limit the memory held in the buffer to a single segment.emitCompleteSegments 在接口中 BufferedSinkpublic BufferedSink emit()
BufferedSinkSink.flush(), but
weaker. Call this before this buffered sink goes out of scope so that its data can reach its
destination.emit 在接口中 BufferedSinkpublic boolean exhausted()
BufferedSourceexhausted 在接口中 BufferedSourcepublic void require(long byteCount)
throws java.io.EOFException
BufferedSourcebyteCount bytes. Throws
an EOFException if the source is exhausted before the
required bytes can be read.require 在接口中 BufferedSourcejava.io.EOFExceptionpublic boolean request(long byteCount)
BufferedSourcebyteCount bytes,
expanding it as necessary. Returns false if the source is exhausted before
the requested bytes can be read.request 在接口中 BufferedSourcepublic java.io.InputStream inputStream()
BufferedSourceinputStream 在接口中 BufferedSourcepublic Buffer copyTo(java.io.OutputStream out) throws java.io.IOException
out.java.io.IOExceptionpublic Buffer copyTo(java.io.OutputStream out, long offset, long byteCount) throws java.io.IOException
byteCount bytes from this, starting at offset, to
out.java.io.IOExceptionpublic Buffer copyTo(Buffer out, long offset, long byteCount)
byteCount bytes from this, starting at offset, to out.public Buffer writeTo(java.io.OutputStream out) throws java.io.IOException
out.java.io.IOExceptionpublic Buffer writeTo(java.io.OutputStream out, long byteCount) throws java.io.IOException
byteCount bytes from this to out.java.io.IOExceptionpublic Buffer readFrom(java.io.InputStream in) throws java.io.IOException
in to this.java.io.IOExceptionpublic Buffer readFrom(java.io.InputStream in, long byteCount) throws java.io.IOException
byteCount bytes from in to this.java.io.IOExceptionpublic long completeSegmentByteCount()
public byte readByte()
BufferedSourcereadByte 在接口中 BufferedSourcepublic byte getByte(long pos)
pos.public short readShort()
BufferedSourcereadShort 在接口中 BufferedSourcepublic int readInt()
BufferedSourcereadInt 在接口中 BufferedSourcepublic long readLong()
BufferedSourcereadLong 在接口中 BufferedSourcepublic short readShortLe()
BufferedSourcereadShortLe 在接口中 BufferedSourcepublic int readIntLe()
BufferedSourcereadIntLe 在接口中 BufferedSourcepublic long readLongLe()
BufferedSourcereadLongLe 在接口中 BufferedSourcepublic long readDecimalLong()
BufferedSourcereadDecimalLong 在接口中 BufferedSourcepublic long readHexadecimalUnsignedLong()
BufferedSourcereadHexadecimalUnsignedLong 在接口中 BufferedSourcepublic ByteString readByteString()
BufferedSourcereadByteString 在接口中 BufferedSourcepublic ByteString readByteString(long byteCount) throws java.io.EOFException
BufferedSourcebyteCount bytes from this and returns them as a byte string.readByteString 在接口中 BufferedSourcejava.io.EOFExceptionpublic void readFully(Buffer sink, long byteCount) throws java.io.EOFException
BufferedSourcebyteCount bytes from this and appends them to
sink. Throws an EOFException if the requested
number of bytes cannot be read.readFully 在接口中 BufferedSourcejava.io.EOFExceptionpublic long readAll(Sink sink) throws java.io.IOException
BufferedSourcesink. Returns the
total number of bytes written to sink which will be 0 if this is
exhausted.readAll 在接口中 BufferedSourcejava.io.IOExceptionpublic java.lang.String readUtf8()
BufferedSourcereadUtf8 在接口中 BufferedSourcepublic java.lang.String readUtf8(long byteCount)
throws java.io.EOFException
BufferedSourcebyteCount bytes from this, decodes them as UTF-8, and
returns the string.readUtf8 在接口中 BufferedSourcejava.io.EOFExceptionpublic java.lang.String readString(java.nio.charset.Charset charset)
BufferedSourcecharset, and returns
the string.readString 在接口中 BufferedSourcepublic java.lang.String readString(long byteCount,
java.nio.charset.Charset charset)
throws java.io.EOFException
BufferedSourcebyteCount bytes from this, decodes them as charset,
and returns the string.readString 在接口中 BufferedSourcejava.io.EOFExceptionpublic java.lang.String readUtf8Line()
throws java.io.EOFException
BufferedSource"\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.
readUtf8Line 在接口中 BufferedSourcejava.io.EOFExceptionpublic java.lang.String readUtf8LineStrict()
throws java.io.EOFException
BufferedSource"\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 EOFException is thrown. Use this for
machine-generated data where a missing line break implies truncated input.
readUtf8LineStrict 在接口中 BufferedSourcejava.io.EOFExceptionpublic int readUtf8CodePoint()
throws java.io.EOFException
BufferedSourceIf this source is exhausted before a complete code point can be read, this throws an EOFException and 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 as 0xc080 for the NUL character in modified UTF-8).
readUtf8CodePoint 在接口中 BufferedSourcejava.io.EOFExceptionpublic byte[] readByteArray()
BufferedSourcereadByteArray 在接口中 BufferedSourcepublic byte[] readByteArray(long byteCount)
throws java.io.EOFException
BufferedSourcebyteCount bytes from this and returns them as a byte array.readByteArray 在接口中 BufferedSourcejava.io.EOFExceptionpublic int read(byte[] sink)
BufferedSourcesink.length bytes from this and copies them into sink.
Returns the number of bytes read, or -1 if this source is exhausted.read 在接口中 BufferedSourcepublic void readFully(byte[] sink)
throws java.io.EOFException
BufferedSourcesink.length bytes from this and copies them into sink.
Throws an EOFException if the requested number of bytes cannot be read.readFully 在接口中 BufferedSourcejava.io.EOFExceptionpublic int read(byte[] sink,
int offset,
int byteCount)
BufferedSourcebyteCount bytes from this and copies them into sink at
offset. Returns the number of bytes read, or -1 if this source is exhausted.read 在接口中 BufferedSourcepublic void clear()
public void skip(long byteCount)
throws java.io.EOFException
byteCount bytes from the head of this buffer.skip 在接口中 BufferedSourcejava.io.EOFExceptionpublic Buffer write(ByteString byteString)
write 在接口中 BufferedSinkpublic Buffer writeUtf8(java.lang.String string)
BufferedSinkstring in UTF-8 and writes it to this sink.writeUtf8 在接口中 BufferedSinkpublic Buffer writeUtf8(java.lang.String string, int beginIndex, int endIndex)
BufferedSinkbeginIndex up to endIndex from string in
UTF-8 and writes it to this sink.writeUtf8 在接口中 BufferedSinkpublic Buffer writeUtf8CodePoint(int codePoint)
BufferedSinkcodePoint in UTF-8 and writes it to this sink.writeUtf8CodePoint 在接口中 BufferedSinkpublic Buffer writeString(java.lang.String string, java.nio.charset.Charset charset)
BufferedSinkstring in charset and writes it to this sink.writeString 在接口中 BufferedSinkpublic Buffer writeString(java.lang.String string, int beginIndex, int endIndex, java.nio.charset.Charset charset)
BufferedSinkbeginIndex up to endIndex from string in
charset and writes it to this sink.writeString 在接口中 BufferedSinkpublic Buffer write(byte[] source)
BufferedSinkOutputStream.write(byte[]), this writes a complete byte array to
this sink.write 在接口中 BufferedSinkpublic Buffer write(byte[] source, int offset, int byteCount)
BufferedSinkOutputStream.write(byte[], int, int), this writes byteCount
bytes of source, starting at offset.write 在接口中 BufferedSinkpublic 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 在接口中 BufferedSinkjava.io.IOExceptionpublic BufferedSink write(Source source, long byteCount) throws java.io.IOException
BufferedSinkbyteCount bytes from source and appends them to this sink.write 在接口中 BufferedSinkjava.io.IOExceptionpublic Buffer writeByte(int b)
BufferedSinkwriteByte 在接口中 BufferedSinkpublic Buffer writeShort(int s)
BufferedSinkwriteShort 在接口中 BufferedSinkpublic Buffer writeShortLe(int s)
BufferedSinkwriteShortLe 在接口中 BufferedSinkpublic Buffer writeInt(int i)
BufferedSinkwriteInt 在接口中 BufferedSinkpublic Buffer writeIntLe(int i)
BufferedSinkwriteIntLe 在接口中 BufferedSinkpublic Buffer writeLong(long v)
BufferedSinkwriteLong 在接口中 BufferedSinkpublic Buffer writeLongLe(long v)
BufferedSinkwriteLongLe 在接口中 BufferedSinkpublic Buffer writeDecimalLong(long v)
BufferedSinkwriteDecimalLong 在接口中 BufferedSinkpublic Buffer writeHexadecimalUnsignedLong(long v)
BufferedSinkwriteHexadecimalUnsignedLong 在接口中 BufferedSinkpublic void write(Buffer source, long byteCount)
SinkbyteCount bytes from source and appends them to this.public long read(Buffer sink, long byteCount)
SourcebyteCount bytes from this and appends
them to sink. Returns the number of bytes read, or -1 if this
source is exhausted.public long indexOf(byte b)
BufferedSourceb in the buffer. This expands the
buffer as necessary until b 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.indexOf 在接口中 BufferedSourcepublic long indexOf(byte b,
long fromIndex)
b in this at or beyond fromIndex, or
-1 if this buffer does not contain b in that range.indexOf 在接口中 BufferedSourcepublic long indexOf(ByteString bytes) throws java.io.IOException
BufferedSourcebytes in the buffer. This expands the buffer
as necessary until bytes is found. This reads an unbounded number of bytes into the
buffer. Returns -1 if the stream is exhausted before the requested bytes are found.indexOf 在接口中 BufferedSourcejava.io.IOExceptionpublic long indexOf(ByteString bytes, long fromIndex) throws java.io.IOException
BufferedSourcebytes in the buffer at or after fromIndex. This expands the buffer as necessary until bytes is found. This reads an
unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the
requested bytes are found.indexOf 在接口中 BufferedSourcejava.io.IOExceptionpublic long indexOfElement(ByteString targetBytes)
BufferedSourcetargetBytes in 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.indexOfElement 在接口中 BufferedSourcepublic long indexOfElement(ByteString targetBytes, long fromIndex)
BufferedSourcetargetBytes in the buffer
at or after fromIndex. 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.indexOfElement 在接口中 BufferedSourcepublic void flush()
Sinkpublic void close()
Sourcepublic boolean equals(java.lang.Object o)
equals 在类中 java.lang.Objectpublic int hashCode()
hashCode 在类中 java.lang.Objectpublic java.lang.String toString()
toString 在类中 java.lang.Objectpublic Buffer clone()
clone 在类中 java.lang.Objectpublic ByteString snapshot()
public ByteString snapshot(int byteCount)
byteCount bytes of this buffer as a byte string.