类 Buffer
- 所有已实现的接口:
BufferedSink,BufferedSource,Sink,Source,Closeable,Flushable,AutoCloseable,Cloneable,ByteChannel,Channel,ReadableByteChannel,WritableByteChannel
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()Returns this source's internal buffer.final voidclear()Discards all bytes in this buffer.clone()Returns a deep copy of this buffer.voidclose()Closes this source and releases the resources held by this source.final longReturns the number of bytes in segments that are not writable.final BufferCopybyteCountbytes from this, starting atoffset, toout.final BuffercopyTo(OutputStream out) Copy the contents of this toout.final BuffercopyTo(OutputStream out, long offset, long byteCount) CopybyteCountbytes from this, starting atoffset, toout.emit()Writes all buffered data to the underlying sink, if one exists.Writes complete segments to the underlying sink, if one exists.booleanbooleanReturns true if there are no more bytes in this source.voidflush()Writes all buffered data to the underlying sink, if one exists.This source's internal buffer.final bytegetByte(long pos) Returns the byte atpos.inthashCode()final ByteStringhmacSha1(ByteString key) Returns the 160-bit SHA-1 HMAC of this buffer.final ByteStringhmacSha256(ByteString key) Returns the 256-bit SHA-256 HMAC of this buffer.final ByteStringhmacSha512(ByteString key) Returns the 512-bit SHA-512 HMAC of this buffer.longindexOf(byte b) Equivalent toindexOf(b, 0).longindexOf(byte b, long fromIndex) Returns the index ofbin this at or beyondfromIndex, or -1 if this buffer does not containbin that range.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) Equivalent toindexOfElement(targetBytes, 0).longindexOfElement(ByteString targetBytes, long fromIndex) Returns the first index in this buffer that is at or afterfromIndexand that contains any of the bytes intargetBytes.Returns an input stream that reads from this source.booleanisOpen()final ByteStringmd5()Returns the 128-bit MD5 hash of this buffer.Returns an output stream that writes to this sink.peek()Returns a newBufferedSourcethat can read data from thisBufferedSourcewithout consuming it.booleanrangeEquals(long offset, ByteString bytes) Returns true if the bytes atoffsetin this source equalbytes.booleanrangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount) Returns true ifbyteCountbytes atoffsetin this source equalbytesatbytesOffset.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.longRemoves at least 1, and up tobyteCountbytes from this and appends them tosink.intread(ByteBuffer sink) longRemoves all bytes from this and appends them tosink.final Buffer.UnsafeCursorfinal Buffer.UnsafeCursorreadAndWriteUnsafe(Buffer.UnsafeCursor unsafeCursor) bytereadByte()Removes a byte from this source and returns it.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 '-').final BufferreadFrom(InputStream in) Read and exhaust bytes frominto this.final BufferreadFrom(InputStream in, long byteCount) ReadbyteCountbytes frominto this.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()Removes four bytes from this source and returns a big-endian int.intRemoves four bytes from this source and returns a little-endian int.longreadLong()Removes eight bytes from this source and returns a big-endian long.longRemoves eight bytes from this source and returns a little-endian long.shortRemoves two bytes from this source and returns a big-endian short.shortRemoves two bytes from this source and returns a little-endian short.readString(long byteCount, Charset charset) RemovesbyteCountbytes from this, decodes them ascharset, and returns the string.readString(Charset charset) Removes all bytes from this, decodes them ascharset, and returns the string.final Buffer.UnsafeCursorfinal Buffer.UnsafeCursorreadUnsafe(Buffer.UnsafeCursor unsafeCursor) 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 a single UTF-8 code point, reading between 1 and 4 bytes as necessary.Removes 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) LikeBufferedSource.readUtf8LineStrict(), except this allows the caller to specify the longest allowed match.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.intFinds the first string inoptionsthat is a prefix of this buffer, consumes it from this buffer, and returns its index.final ByteStringsha1()Returns the 160-bit SHA-1 hash of this buffer.final ByteStringsha256()Returns the 256-bit SHA-256 hash of this buffer.final ByteStringsha512()Returns the 512-bit SHA-512 hash of this buffer.final longsize()Returns the number of bytes currently in this buffer.voidskip(long byteCount) DiscardsbyteCountbytes from the head of this buffer.final ByteStringsnapshot()Returns an immutable copy of this buffer as a byte string.final ByteStringsnapshot(int byteCount) Returns an immutable copy of the firstbyteCountbytes of this buffer as a byte string.timeout()Returns the timeout for this source.toString()Returns a human-readable string that describes the contents of this buffer.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.voidRemovesbyteCountbytes fromsourceand appends them to this.write(ByteString byteString) RemovesbyteCountbytes fromsourceand appends them to this sink.intwrite(ByteBuffer source) 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.final BufferwriteTo(OutputStream out) Write the contents of this toout.final BufferwriteTo(OutputStream out, long byteCount) WritebyteCountbytes from this toout.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.
-
构造器详细资料
-
Buffer
public Buffer()
-
-
方法详细资料
-
size
public final long size()Returns the number of bytes currently in this buffer. -
buffer
从接口复制的说明:BufferedSourceReturns this source's internal buffer.- 指定者:
buffer在接口中BufferedSink- 指定者:
buffer在接口中BufferedSource
-
getBuffer
从接口复制的说明:BufferedSourceThis source's internal buffer.- 指定者:
getBuffer在接口中BufferedSource
-
outputStream
从接口复制的说明:BufferedSinkReturns an output stream that writes to this sink.- 指定者:
outputStream在接口中BufferedSink
-
emitCompleteSegments
从接口复制的说明:BufferedSinkWrites complete segments to the underlying sink, if one exists. LikeBufferedSink.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.- 指定者:
emitCompleteSegments在接口中BufferedSink
-
emit
从接口复制的说明:BufferedSinkWrites all buffered data to the underlying sink, if one exists. LikeBufferedSink.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());- 指定者:
emit在接口中BufferedSink
-
exhausted
public boolean exhausted()从接口复制的说明:BufferedSourceReturns 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.- 指定者:
exhausted在接口中BufferedSource
-
require
从接口复制的说明:BufferedSourceReturns when the buffer contains at leastbyteCountbytes. Throws anEOFExceptionif the source is exhausted before the required bytes can be read.- 指定者:
require在接口中BufferedSource- 抛出:
EOFException
-
request
public boolean request(long byteCount) 从接口复制的说明:BufferedSourceReturns 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.- 指定者:
request在接口中BufferedSource
-
peek
从接口复制的说明:BufferedSourceReturns a newBufferedSourcethat can read data from thisBufferedSourcewithout 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" BufferedSource 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"- 指定者:
peek在接口中BufferedSource
-
inputStream
从接口复制的说明:BufferedSourceReturns an input stream that reads from this source.- 指定者:
inputStream在接口中BufferedSource
-
copyTo
Copy the contents of this toout.- 抛出:
IOException
-
copyTo
CopybyteCountbytes from this, starting atoffset, toout.- 抛出:
IOException
-
copyTo
CopybyteCountbytes from this, starting atoffset, toout. -
writeTo
Write the contents of this toout.- 抛出:
IOException
-
writeTo
WritebyteCountbytes from this toout.- 抛出:
IOException
-
readFrom
Read and exhaust bytes frominto this.- 抛出:
IOException
-
readFrom
ReadbyteCountbytes frominto this.- 抛出:
IOException
-
completeSegmentByteCount
public final long completeSegmentByteCount()Returns the number of bytes in segments that are not writable. This is the number of bytes that can be flushed immediately to an underlying sink without harming throughput. -
readByte
public byte readByte()从接口复制的说明:BufferedSourceRemoves a byte from this source and returns it.- 指定者:
readByte在接口中BufferedSource
-
getByte
public final byte getByte(long pos) Returns the byte atpos. -
readShort
public short readShort()从接口复制的说明:BufferedSourceRemoves 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());- 指定者:
readShort在接口中BufferedSource
-
readInt
public int readInt()从接口复制的说明:BufferedSourceRemoves four bytes from this source and returns a big-endian int.Buffer buffer = new Buffer() .writeByte(0x7f) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x0f); assertEquals(8, buffer.size()); assertEquals(2147483647, buffer.readInt()); assertEquals(4, buffer.size()); assertEquals(15, buffer.readInt()); assertEquals(0, buffer.size());- 指定者:
readInt在接口中BufferedSource
-
readLong
public long readLong()从接口复制的说明:BufferedSourceRemoves eight bytes from this source and returns a big-endian long.Buffer buffer = new Buffer() .writeByte(0x7f) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00) .writeByte(0x0f); assertEquals(16, buffer.size()); assertEquals(9223372036854775807L, buffer.readLong()); assertEquals(8, buffer.size()); assertEquals(15, buffer.readLong()); assertEquals(0, buffer.size());- 指定者:
readLong在接口中BufferedSource
-
readShortLe
public short readShortLe()从接口复制的说明:BufferedSourceRemoves 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());- 指定者:
readShortLe在接口中BufferedSource
-
readIntLe
public int readIntLe()从接口复制的说明:BufferedSourceRemoves four bytes from this source and returns a little-endian int.Buffer buffer = new Buffer() .writeByte(0xff) .writeByte(0xff) .writeByte(0xff) .writeByte(0x7f) .writeByte(0x0f) .writeByte(0x00) .writeByte(0x00) .writeByte(0x00); assertEquals(8, buffer.size()); assertEquals(2147483647, buffer.readIntLe()); assertEquals(4, buffer.size()); assertEquals(15, buffer.readIntLe()); assertEquals(0, buffer.size());- 指定者:
readIntLe在接口中BufferedSource
-
readLongLe
public long readLongLe()从接口复制的说明:BufferedSourceRemoves 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());- 指定者:
readLongLe在接口中BufferedSource
-
readDecimalLong
public long readDecimalLong()从接口复制的说明:BufferedSourceReads 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());- 指定者:
readDecimalLong在接口中BufferedSource
-
readHexadecimalUnsignedLong
public long readHexadecimalUnsignedLong()从接口复制的说明:BufferedSourceReads 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());- 指定者:
readHexadecimalUnsignedLong在接口中BufferedSource
-
readByteString
从接口复制的说明:BufferedSourceRemoves all bytes bytes from this and returns them as a byte string.- 指定者:
readByteString在接口中BufferedSource
-
readByteString
从接口复制的说明:BufferedSourceRemovesbyteCountbytes from this and returns them as a byte string.- 指定者:
readByteString在接口中BufferedSource- 抛出:
EOFException
-
select
从接口复制的说明:BufferedSourceFinds 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
BufferedSource.readByteString()or evenBufferedSource.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());- 指定者:
select在接口中BufferedSource
-
readFully
从接口复制的说明:BufferedSourceRemoves exactlybyteCountbytes from this and appends them tosink. Throws anEOFExceptionif the requested number of bytes cannot be read.- 指定者:
readFully在接口中BufferedSource- 抛出:
EOFException
-
readAll
从接口复制的说明:BufferedSourceRemoves all bytes from this and appends them tosink. Returns the total number of bytes written tosinkwhich will be 0 if this is exhausted.- 指定者:
readAll在接口中BufferedSource- 抛出:
IOException
-
readUtf8
从接口复制的说明:BufferedSourceRemoves 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());- 指定者:
readUtf8在接口中BufferedSource
-
readUtf8
从接口复制的说明:BufferedSourceRemovesbyteCountbytes 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());- 指定者:
readUtf8在接口中BufferedSource- 抛出:
EOFException
-
readString
从接口复制的说明:BufferedSourceRemoves all bytes from this, decodes them ascharset, and returns the string.- 指定者:
readString在接口中BufferedSource
-
readString
从接口复制的说明:BufferedSourceRemovesbyteCountbytes from this, decodes them ascharset, and returns the string.- 指定者:
readString在接口中BufferedSource- 抛出:
EOFException
-
readUtf8Line
从接口复制的说明:BufferedSourceRemoves 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.- 指定者:
readUtf8Line在接口中BufferedSource- 抛出:
EOFException
-
readUtf8LineStrict
从接口复制的说明:BufferedSourceRemoves 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.- 指定者:
readUtf8LineStrict在接口中BufferedSource- 抛出:
EOFException
-
readUtf8LineStrict
从接口复制的说明:BufferedSourceLikeBufferedSource.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 anEOFExceptionbecause 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));- 指定者:
readUtf8LineStrict在接口中BufferedSource- 抛出:
EOFException
-
readUtf8CodePoint
从接口复制的说明:BufferedSourceRemoves 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).- 指定者:
readUtf8CodePoint在接口中BufferedSource- 抛出:
EOFException
-
readByteArray
public byte[] readByteArray()从接口复制的说明:BufferedSourceRemoves all bytes from this and returns them as a byte array.- 指定者:
readByteArray在接口中BufferedSource
-
readByteArray
从接口复制的说明:BufferedSourceRemovesbyteCountbytes from this and returns them as a byte array.- 指定者:
readByteArray在接口中BufferedSource- 抛出:
EOFException
-
read
public int read(byte[] sink) 从接口复制的说明:BufferedSourceRemoves up tosink.lengthbytes from this and copies them intosink. Returns the number of bytes read, or -1 if this source is exhausted.- 指定者:
read在接口中BufferedSource
-
readFully
从接口复制的说明:BufferedSourceRemoves exactlysink.lengthbytes from this and copies them intosink. Throws anEOFExceptionif the requested number of bytes cannot be read.- 指定者:
readFully在接口中BufferedSource- 抛出:
EOFException
-
read
public int read(byte[] sink, int offset, int byteCount) 从接口复制的说明:BufferedSourceRemoves up tobyteCountbytes from this and copies them intosinkatoffset. Returns the number of bytes read, or -1 if this source is exhausted.- 指定者:
read在接口中BufferedSource
-
read
- 指定者:
read在接口中ReadableByteChannel- 抛出:
IOException
-
clear
public final void clear()Discards all bytes in this buffer. Calling this method when you're done with a buffer will return its segments to the pool. -
skip
DiscardsbyteCountbytes from the head of this buffer.- 指定者:
skip在接口中BufferedSource- 抛出:
EOFException
-
write
- 指定者:
write在接口中BufferedSink
-
writeUtf8
从接口复制的说明:BufferedSinkEncodesstringin 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());- 指定者:
writeUtf8在接口中BufferedSink
-
writeUtf8
从接口复制的说明:BufferedSinkEncodes 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());- 指定者:
writeUtf8在接口中BufferedSink
-
writeUtf8CodePoint
从接口复制的说明:BufferedSinkEncodescodePointin UTF-8 and writes it to this sink.- 指定者:
writeUtf8CodePoint在接口中BufferedSink
-
writeString
从接口复制的说明:BufferedSinkEncodesstringincharsetand writes it to this sink.- 指定者:
writeString在接口中BufferedSink
-
writeString
从接口复制的说明:BufferedSinkEncodes the characters atbeginIndexup toendIndexfromstringincharsetand writes it to this sink.- 指定者:
writeString在接口中BufferedSink
-
write
从接口复制的说明:BufferedSinkLikeOutputStream.write(byte[]), this writes a complete byte array to this sink.- 指定者:
write在接口中BufferedSink
-
write
从接口复制的说明:BufferedSinkLikeOutputStream.write(byte[], int, int), this writesbyteCountbytes ofsource, starting atoffset.- 指定者:
write在接口中BufferedSink
-
write
- 指定者:
write在接口中WritableByteChannel- 抛出:
IOException
-
writeAll
从接口复制的说明:BufferedSinkRemoves all bytes fromsourceand appends them to this sink. Returns the number of bytes read which will be 0 ifsourceis exhausted.- 指定者:
writeAll在接口中BufferedSink- 抛出:
IOException
-
write
从接口复制的说明:BufferedSinkRemovesbyteCountbytes fromsourceand appends them to this sink.- 指定者:
write在接口中BufferedSink- 抛出:
IOException
-
writeByte
从接口复制的说明:BufferedSinkWrites a byte to this sink.- 指定者:
writeByte在接口中BufferedSink
-
writeShort
从接口复制的说明:BufferedSinkWrites 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());- 指定者:
writeShort在接口中BufferedSink
-
writeShortLe
从接口复制的说明:BufferedSinkWrites 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());- 指定者:
writeShortLe在接口中BufferedSink
-
writeInt
从接口复制的说明:BufferedSinkWrites 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());- 指定者:
writeInt在接口中BufferedSink
-
writeIntLe
从接口复制的说明:BufferedSinkWrites 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());- 指定者:
writeIntLe在接口中BufferedSink
-
writeLong
从接口复制的说明:BufferedSinkWrites 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());- 指定者:
writeLong在接口中BufferedSink
-
writeLongLe
从接口复制的说明:BufferedSinkWrites 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());- 指定者:
writeLongLe在接口中BufferedSink
-
writeDecimalLong
从接口复制的说明:BufferedSinkWrites 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());- 指定者:
writeDecimalLong在接口中BufferedSink
-
writeHexadecimalUnsignedLong
从接口复制的说明:BufferedSinkWrites 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());- 指定者:
writeHexadecimalUnsignedLong在接口中BufferedSink
-
write
从接口复制的说明:SinkRemovesbyteCountbytes fromsourceand appends them to this. -
read
从接口复制的说明:SourceRemoves at least 1, and up tobyteCountbytes from this and appends them tosink. Returns the number of bytes read, or -1 if this source is exhausted. -
indexOf
public long indexOf(byte b) 从接口复制的说明:BufferedSourceEquivalent toindexOf(b, 0).- 指定者:
indexOf在接口中BufferedSource
-
indexOf
public long indexOf(byte b, long fromIndex) Returns the index ofbin this at or beyondfromIndex, or -1 if this buffer does not containbin that range.- 指定者:
indexOf在接口中BufferedSource
-
indexOf
public long indexOf(byte b, long fromIndex, long toIndex) 从接口复制的说明:BufferedSourceReturns 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.- 指定者:
indexOf在接口中BufferedSource
-
indexOf
从接口复制的说明:BufferedSourceEquivalent toindexOf(bytes, 0).- 指定者:
indexOf在接口中BufferedSource- 抛出:
IOException
-
indexOf
从接口复制的说明:BufferedSourceReturns 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));- 指定者:
indexOf在接口中BufferedSource- 抛出:
IOException
-
indexOfElement
从接口复制的说明:BufferedSourceEquivalent toindexOfElement(targetBytes, 0).- 指定者:
indexOfElement在接口中BufferedSource
-
indexOfElement
从接口复制的说明:BufferedSourceReturns the first index in this buffer that is at or afterfromIndexand that contains any of the bytes intargetBytes. 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.ByteString ANY_VOWEL = ByteString.encodeUtf8("AEOIUaeoiu"); Buffer buffer = new Buffer(); buffer.writeUtf8("Dr. Alan Grant"); assertEquals(4, buffer.indexOfElement(ANY_VOWEL)); // 'A' in 'Alan'. assertEquals(11, buffer.indexOfElement(ANY_VOWEL, 9)); // 'a' in 'Grant'.- 指定者:
indexOfElement在接口中BufferedSource
-
rangeEquals
从接口复制的说明:BufferedSourceReturns true 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 one leg."); assertTrue(standOnOneLeg.rangeEquals(0, simonSays)); Buffer payMeMoney = new Buffer().writeUtf8("Pay me $1,000,000."); assertFalse(payMeMoney.rangeEquals(0, simonSays));- 指定者:
rangeEquals在接口中BufferedSource
-
rangeEquals
从接口复制的说明:BufferedSourceReturns true ifbyteCountbytes atoffsetin this source equalbytesatbytesOffset. 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.- 指定者:
rangeEquals在接口中BufferedSource
-
flush
public void flush()从接口复制的说明:BufferedSinkWrites 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()); -
isOpen
public boolean isOpen() -
close
public void close()从接口复制的说明:SourceCloses this source and releases the resources held by this source. It is an error to read a closed source. It is safe to close a source more than once. -
timeout
从接口复制的说明:SourceReturns the timeout for this source. -
md5
Returns the 128-bit MD5 hash of this buffer. -
sha1
Returns the 160-bit SHA-1 hash of this buffer. -
sha256
Returns the 256-bit SHA-256 hash of this buffer. -
sha512
Returns the 512-bit SHA-512 hash of this buffer. -
hmacSha1
Returns the 160-bit SHA-1 HMAC of this buffer. -
hmacSha256
Returns the 256-bit SHA-256 HMAC of this buffer. -
hmacSha512
Returns the 512-bit SHA-512 HMAC of this buffer. -
equals
-
hashCode
public int hashCode() -
toString
Returns a human-readable string that describes the contents of this buffer. Typically this is a string like[text=Hello]or[hex=0000ffff]. -
clone
Returns a deep copy of this buffer. -
snapshot
Returns an immutable copy of this buffer as a byte string. -
snapshot
Returns an immutable copy of the firstbyteCountbytes of this buffer as a byte string. -
readUnsafe
-
readUnsafe
-
readAndWriteUnsafe
-
readAndWriteUnsafe
-