public final class Buffer extends Object implements BufferedSource, BufferedSink, 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.
Constructor and Description |
---|
Buffer() |
Modifier and Type | Method and Description |
---|---|
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(OutputStream out)
Copy the contents of this to
out . |
Buffer |
copyTo(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(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 . |
InputStream |
inputStream()
Returns an input stream that reads from this source.
|
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(InputStream in)
Read and exhaust bytes from
in to this. |
Buffer |
readFrom(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.
|
String |
readString(Charset charset)
Removes all bytes from this, decodes them as
charset , and returns
the string. |
String |
readString(long byteCount,
Charset charset)
Removes
byteCount bytes from this, decodes them as charset ,
and returns the string. |
String |
readUtf8()
Removes all bytes from this, decodes them as UTF-8, and returns the string.
|
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.
|
String |
readUtf8Line()
Removes and returns characters up to but not including the next line break.
|
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.
|
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(String string,
Charset charset)
Encodes
string in charset and writes it to this sink. |
Buffer |
writeString(String string,
int beginIndex,
int endIndex,
Charset charset)
Encodes the characters at
beginIndex up to endIndex from string in
charset and writes it to this sink. |
Buffer |
writeTo(OutputStream out)
Write the contents of this to
out . |
Buffer |
writeTo(OutputStream out,
long byteCount)
Write
byteCount bytes from this to out . |
Buffer |
writeUtf8(String string)
Encodes
string in UTF-8 and writes it to this sink. |
Buffer |
writeUtf8(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()
BufferedSource
buffer
in interface BufferedSink
buffer
in interface BufferedSource
public OutputStream outputStream()
BufferedSink
outputStream
in interface BufferedSink
public Buffer emitCompleteSegments()
BufferedSink
Sink.flush()
, but
weaker. Use this to limit the memory held in the buffer to a single segment.emitCompleteSegments
in interface BufferedSink
public BufferedSink emit()
BufferedSink
Sink.flush()
, but
weaker. Call this before this buffered sink goes out of scope so that its data can reach its
destination.emit
in interface BufferedSink
public boolean exhausted()
BufferedSource
exhausted
in interface BufferedSource
public void require(long byteCount) throws EOFException
BufferedSource
byteCount
bytes. Throws
an EOFException
if the source is exhausted before the
required bytes can be read.require
in interface BufferedSource
EOFException
public boolean request(long byteCount)
BufferedSource
byteCount
bytes,
expanding it as necessary. Returns false if the source is exhausted before
the requested bytes can be read.request
in interface BufferedSource
public InputStream inputStream()
BufferedSource
inputStream
in interface BufferedSource
public Buffer copyTo(OutputStream out) throws IOException
out
.IOException
public Buffer copyTo(OutputStream out, long offset, long byteCount) throws IOException
byteCount
bytes from this, starting at offset
, to
out
.IOException
public Buffer copyTo(Buffer out, long offset, long byteCount)
byteCount
bytes from this, starting at offset
, to out
.public Buffer writeTo(OutputStream out) throws IOException
out
.IOException
public Buffer writeTo(OutputStream out, long byteCount) throws IOException
byteCount
bytes from this to out
.IOException
public Buffer readFrom(InputStream in) throws IOException
in
to this.IOException
public Buffer readFrom(InputStream in, long byteCount) throws IOException
byteCount
bytes from in
to this.IOException
public long completeSegmentByteCount()
public byte readByte()
BufferedSource
readByte
in interface BufferedSource
public byte getByte(long pos)
pos
.public short readShort()
BufferedSource
readShort
in interface BufferedSource
public int readInt()
BufferedSource
readInt
in interface BufferedSource
public long readLong()
BufferedSource
readLong
in interface BufferedSource
public short readShortLe()
BufferedSource
readShortLe
in interface BufferedSource
public int readIntLe()
BufferedSource
readIntLe
in interface BufferedSource
public long readLongLe()
BufferedSource
readLongLe
in interface BufferedSource
public long readDecimalLong()
BufferedSource
readDecimalLong
in interface BufferedSource
public long readHexadecimalUnsignedLong()
BufferedSource
readHexadecimalUnsignedLong
in interface BufferedSource
public ByteString readByteString()
BufferedSource
readByteString
in interface BufferedSource
public ByteString readByteString(long byteCount) throws EOFException
BufferedSource
byteCount
bytes from this and returns them as a byte string.readByteString
in interface BufferedSource
EOFException
public void readFully(Buffer sink, long byteCount) throws EOFException
BufferedSource
byteCount
bytes from this and appends them to
sink
. Throws an EOFException
if the requested
number of bytes cannot be read.readFully
in interface BufferedSource
EOFException
public long readAll(Sink sink) throws IOException
BufferedSource
sink
. Returns the
total number of bytes written to sink
which will be 0 if this is
exhausted.readAll
in interface BufferedSource
IOException
public String readUtf8()
BufferedSource
readUtf8
in interface BufferedSource
public String readUtf8(long byteCount) throws EOFException
BufferedSource
byteCount
bytes from this, decodes them as UTF-8, and
returns the string.readUtf8
in interface BufferedSource
EOFException
public String readString(Charset charset)
BufferedSource
charset
, and returns
the string.readString
in interface BufferedSource
public String readString(long byteCount, Charset charset) throws EOFException
BufferedSource
byteCount
bytes from this, decodes them as charset
,
and returns the string.readString
in interface BufferedSource
EOFException
public String readUtf8Line() throws 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
in interface BufferedSource
EOFException
public String readUtf8LineStrict() throws 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
in interface BufferedSource
EOFException
public int readUtf8CodePoint() throws EOFException
BufferedSource
If 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
in interface BufferedSource
EOFException
public byte[] readByteArray()
BufferedSource
readByteArray
in interface BufferedSource
public byte[] readByteArray(long byteCount) throws EOFException
BufferedSource
byteCount
bytes from this and returns them as a byte array.readByteArray
in interface BufferedSource
EOFException
public int read(byte[] sink)
BufferedSource
sink.length
bytes from this and copies them into sink
.
Returns the number of bytes read, or -1 if this source is exhausted.read
in interface BufferedSource
public void readFully(byte[] sink) throws EOFException
BufferedSource
sink.length
bytes from this and copies them into sink
.
Throws an EOFException
if the requested number of bytes cannot be read.readFully
in interface BufferedSource
EOFException
public int read(byte[] sink, int offset, int byteCount)
BufferedSource
byteCount
bytes from this and copies them into sink
at
offset
. Returns the number of bytes read, or -1 if this source is exhausted.read
in interface BufferedSource
public void clear()
public void skip(long byteCount) throws EOFException
byteCount
bytes from the head of this buffer.skip
in interface BufferedSource
EOFException
public Buffer write(ByteString byteString)
write
in interface BufferedSink
public Buffer writeUtf8(String string)
BufferedSink
string
in UTF-8 and writes it to this sink.writeUtf8
in interface BufferedSink
public Buffer writeUtf8(String string, int beginIndex, int endIndex)
BufferedSink
beginIndex
up to endIndex
from string
in
UTF-8 and writes it to this sink.writeUtf8
in interface BufferedSink
public Buffer writeUtf8CodePoint(int codePoint)
BufferedSink
codePoint
in UTF-8 and writes it to this sink.writeUtf8CodePoint
in interface BufferedSink
public Buffer writeString(String string, Charset charset)
BufferedSink
string
in charset
and writes it to this sink.writeString
in interface BufferedSink
public Buffer writeString(String string, int beginIndex, int endIndex, Charset charset)
BufferedSink
beginIndex
up to endIndex
from string
in
charset
and writes it to this sink.writeString
in interface BufferedSink
public Buffer write(byte[] source)
BufferedSink
OutputStream.write(byte[])
, this writes a complete byte array to
this sink.write
in interface BufferedSink
public Buffer write(byte[] source, int offset, int byteCount)
BufferedSink
OutputStream.write(byte[], int, int)
, this writes byteCount
bytes of source
, starting at offset
.write
in interface BufferedSink
public long writeAll(Source source) throws IOException
BufferedSink
source
and appends them to this sink. Returns the
number of bytes read which will be 0 if source
is exhausted.writeAll
in interface BufferedSink
IOException
public BufferedSink write(Source source, long byteCount) throws IOException
BufferedSink
byteCount
bytes from source
and appends them to this sink.write
in interface BufferedSink
IOException
public Buffer writeByte(int b)
BufferedSink
writeByte
in interface BufferedSink
public Buffer writeShort(int s)
BufferedSink
writeShort
in interface BufferedSink
public Buffer writeShortLe(int s)
BufferedSink
writeShortLe
in interface BufferedSink
public Buffer writeInt(int i)
BufferedSink
writeInt
in interface BufferedSink
public Buffer writeIntLe(int i)
BufferedSink
writeIntLe
in interface BufferedSink
public Buffer writeLong(long v)
BufferedSink
writeLong
in interface BufferedSink
public Buffer writeLongLe(long v)
BufferedSink
writeLongLe
in interface BufferedSink
public Buffer writeDecimalLong(long v)
BufferedSink
writeDecimalLong
in interface BufferedSink
public Buffer writeHexadecimalUnsignedLong(long v)
BufferedSink
writeHexadecimalUnsignedLong
in interface BufferedSink
public void write(Buffer source, long byteCount)
Sink
byteCount
bytes from source
and appends them to this.public long read(Buffer sink, long byteCount)
Source
byteCount
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)
BufferedSource
b
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
in interface BufferedSource
public 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
in interface BufferedSource
public long indexOf(ByteString bytes) throws IOException
BufferedSource
bytes
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
in interface BufferedSource
IOException
public long indexOf(ByteString bytes, long fromIndex) throws IOException
BufferedSource
bytes
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
in interface BufferedSource
IOException
public long indexOfElement(ByteString targetBytes)
BufferedSource
targetBytes
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
in interface BufferedSource
public long indexOfElement(ByteString targetBytes, long fromIndex)
BufferedSource
targetBytes
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
in interface BufferedSource
public void flush()
Sink
public void close()
Source
public Timeout timeout()
Source
public ByteString snapshot()
public ByteString snapshot(int byteCount)
byteCount
bytes of this buffer as a byte string.Copyright © 2017. All rights reserved.