Class Buffer

java.lang.Object
org.miaixz.bus.core.io.buffer.Buffer
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Cloneable, ByteChannel, Channel, ReadableByteChannel, WritableByteChannel, BufferSink, Sink, BufferSource, Source

public class Buffer extends Object implements BufferSource, BufferSink, Cloneable, ByteChannel
内存中字节的集合.
Since:
Java 17+
Author:
Kimi Liu
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    不安全的游标
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    头部信息
    static final int
     
    long
    信息大小
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns this sink's internal buffer.
    final void
    丢弃此缓冲区中的所有字节。在使用完缓冲区后调用此方法将把它的段返回到池中
    Returns a deep copy of this buffer.
    void
    关闭此源并释放此源持有的资源.
    final long
    Returns the number of bytes in segments that are not writable.
    final Buffer
    将其内容复制到 out.
    final Buffer
    copyTo(OutputStream out, long offset, long byteCount)
    从这里复制byteCount字节,从offset开始,复制到 out.
    final Buffer
    copyTo(Buffer out, long offset, long byteCount)
    从这里复制byteCount字节,从offset开始,复制到out.
    Writes all buffered data to the underlying sink, if one exists.
    Writes complete segments to the underlying sink, if one exists.
    boolean
     
    boolean
    如果此源中没有更多字节,则返回 true。 这将阻塞,直到有字节可读取或源确实已耗尽
    void
    Writes all buffered data to the underlying sink, if one exists.
    该源的内部缓冲区
    final byte
    getByte(long pos)
    返回pos处的字节.
    int
     
     
     
     
    long
    indexOf(byte b)
    Equivalent to indexOf(b, 0).
    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(byte b, long fromIndex, long toIndex)
    如果在 fromIndextoIndex 范围内找到 b,则返回其索引。 如果未找到 b,或者 fromIndex == toIndex,则返回 -1。
    long
    相当于indexOf(bytes, 0)
    long
    indexOf(ByteString bytes, long fromIndex)
    返回缓冲区中 bytes 的第一个匹配项的索引,位于 fromIndex 处或之后。 这会根据需要扩展缓冲区,直到找到 bytes。这会将无限数量的字节读入缓冲区。如果在找到请求的字节之前流已耗尽,则返回 -1。
    long
    long
    indexOfElement(ByteString targetBytes, long fromIndex)
    返回此缓冲区中位于 fromIndex 或之后且包含 targetBytes 中任意字节的第一个索引。 这会根据需要扩展缓冲区,直到找到目标字节。这会将无限数量的字节读入缓冲区。如果在找到请求的字节之前流已耗尽,则返回 -1。
    返回从该源读取的输入流
    boolean
     
    md5()
     
    Returns an output stream that writes to this sink.
    返回一个新的 BufferedSource,可从此 BufferedSource读取数据但不使用它。 一旦下次读取或关闭此源,返回的源将变为无效。
    boolean
    rangeEquals(long offset, ByteString bytes)
    如果此源中 offset 处的字节等于 bytes,则返回 true。 这会根据需要扩展缓冲区,直到某个字节不匹配、所有字节都匹配,或者在足够的字节确定匹配之前流已耗尽。
    boolean
    rangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount)
    如果此源中 offset 处的 byteCount 个字节等于 bytesOffset 处的 bytes,则返回 true。 这会根据需要扩展缓冲区,直到某个字节不匹配、所有字节都匹配,或者在足够的字节确定匹配之前流已耗尽。
    int
    read(byte[] sink)
    从中移除最多 sink.length 个字节并将其复制到 sink。 返回读取的字节数,如果此源已耗尽,则返回 -1。
    int
    read(byte[] sink, int offset, int byteCount)
    从此处删除最多 byteCount 个字节并将其复制到 offset 处的 sink。 返回读取的字节数,如果此源已耗尽,则返回 -1。
    int
     
    long
    read(Buffer sink, long byteCount)
    从中删除至少1个字节,最多为byteCount字节, 并将它们 附加到sink。返回读取的字节数,如果该源已耗尽,则返回-1
    long
    readAll(Sink sink)
    从中删除所有字节并将其附加到 sink。返回写入 sink 的总字节数,如果已用尽,则为 0。
     
     
    byte
    从该源中删除一个字节并返回它
    byte[]
    从中删除所有字节并将它们作为字节数组返回。
    byte[]
    readByteArray(long byteCount)
    从中删除 byteCount 个字节并将其作为字节数组返回。
    从中删除所有字节并将它们作为字节字符串返回
    readByteString(long byteCount)
    从中删除 byteCount 个字节并将其作为字节字符串返回。
    long
    以有符号十进制形式从此源读取一个长整型值(即以十进制为基数的字符串,前导字符可选为“-”)。此操作将不断迭代,直到找到非数字字符。
    final Buffer
    in中的字节读入并转为bytes
    final Buffer
    readFrom(InputStream in, long byteCount)
    Read byteCount bytes from in to this.
    void
    readFully(byte[] sink)
    从中删除恰好 sink.length 个字节并将其复制到 sink。 如果无法读取请求的字节数,则抛出 EOFException
    void
    readFully(Buffer sink, long byteCount)
    从中删除精确的 byteCount 个字节并将其附加到 sink。 如果无法读取请求的字节数,则抛出 EOFException
    long
    以十六进制形式(即以 16 进制表示的字符串)读取此源的长格式。此过程将不断迭代,直到找到非十六进制字符
    int
    从此源中删除四个字节并返回一个大整数
    int
    从该源中删除四个字节并返回一个小整数
    long
    该源中删除八个字节并返回一个大长整型
    long
    从此源中删除八个字节并返回一个小长整型
    short
    从此源中删除两个字节并返回一个短整型
    short
    从此源中删除两个字节并返回一个整型
    readString(long byteCount, Charset charset)
    从中删除 byteCount 个字节,将其解码为 charset,并返回字符串。
    从中删除所有字节,将其解码为charset,并返回字符串。
     
     
    从中删除所有字节,将其解码为 UTF-8,然后返回字符串。如果此源为空,则返回空字符串。
    readUtf8(long byteCount)
    从中删除 byteCount 个字节,将其解码为 UTF-8,并返回字符串。
    int
    删除并返回单个 UTF-8 代码点,根据需要读取 1 到 4 个字节。 如果此源不是以正确编码的 UTF-8 代码点开头,则此方法将删除 1 个或多个非 UTF-8 字节并返回替换字符 (U+FFFD)。 这包括编码问题(输入不是正确编码的 UTF-8)、字符超出范围(超出 Unicode 的 0x10ffff 限制)、UTF-16 代理的代码 点 (U+d800..U+dfff) 和过长编码(例如,修改版 UTF-8 中的 NUL 字符为 0xc080
    删除并返回直到下一个换行符(但不包括该换行符)的字符。 换行符为 "\n""\r\n";这些字符不包含在结果中。
    readUtf8Line(long newline)
     
    删除并返回直到下一个换行符(但不包括该换行符)的字符。 换行符为 "\n""\r\n";这些字符不包含在结果中
    readUtf8LineStrict(long limit)
    BufferSource.readUtf8LineStrict() 类似,不同之处在于它允许调用者指定允许的最长匹配。 使用它来防止可能不包含 "\n""\r\n" 的流。
    boolean
    request(long byteCount)
    如果缓冲区至少包含 byteCount 个字节,则返回 true,并根据需要对其进行扩展。 如果在读取请求的字节之前源已耗尽,则返回 false。
    void
    require(long byteCount)
    当缓冲区至少包含 byteCount 个字节时返回。 如果在读取所需字节之前源已耗尽,则抛出 EOFException
    int
    select(SegmentBuffer segmentBuffer)
    options 中查找第一个作为此缓冲区前缀的字符串,从此缓冲区中使用它,并返回其索引。 如果 options 中没有字节字符串是此缓冲区的前缀,则返回 -1,并且不消耗任何字节。 如果事先知道预期值集,则可以将其用作 BufferSource.readByteString() 甚至 BufferSource.readUtf8() 的替代。
    int
    selectPrefix(SegmentBuffer segmentBuffer, boolean selectTruncated)
    返回此缓冲区前缀的选项中的值的索引。如果没有找到值,则返回-1 此方法执行两个同步迭代:迭代trie和迭代这个缓冲区。当它在trie中到达一个结果时, 当它在trie中不匹配时,以及当缓冲区耗尽时,它将返回
     
     
     
    final long
     
    void
    skip(long byteCount)
    从这个缓冲区的头部丢弃byteCount字节.
    Returns an immutable copy of this buffer as a byte string.
    snapshot(int byteCount)
    Returns an immutable copy of the first byteCount bytes of this buffer as a byte string.
    返回此源的超时时间.
    Returns a human-readable string that describes the contents of this buffer.
    writableSegment(int minimumCapacity)
     
    write(byte[] source)
    Like OutputStream.write(byte[]), this writes a complete byte array to this sink.
    write(byte[] source, int offset, int byteCount)
    Like OutputStream.write(byte[], int, int), this writes byteCount bytes of source, starting at offset.
    int
    write(ByteBuffer source)
     
    void
    write(Buffer source, long byteCount)
    Removes byteCount bytes from source and appends them to this.
    write(ByteString byteString)
     
    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.
    writeByte(int b)
    Writes a byte to this sink.
    Writes a long to this sink in signed decimal form (i.e., as a string in base 10).
    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 at beginIndex up to endIndex from string in charset and writes it to this sink.
    writeString(String string, Charset charset)
    Encodes string in charset and writes it to this sink.
    final Buffer
    将其内容写入out.
    final Buffer
    writeTo(OutputStream out, long byteCount)
    byteCount字节写入out.
    writeUtf8(String string)
    Encodes string in UTF-8 and writes it to this sink.
    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.
    writeUtf8CodePoint(int codePoint)
    Encodes codePoint in UTF-8 and writes it to this sink.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • REPLACEMENT_CHARACTER

      public static final int REPLACEMENT_CHARACTER
      See Also:
    • size

      public long size
      信息大小
  • Constructor Details

    • Buffer

      public Buffer()
  • Method Details

    • size

      public final long size()
    • buffer

      public Buffer buffer()
      Description copied from interface: BufferSink
      Returns this sink's internal buffer.
      Specified by:
      buffer in interface BufferSink
    • getBuffer

      public Buffer getBuffer()
      Description copied from interface: BufferSource
      该源的内部缓冲区
      Specified by:
      getBuffer in interface BufferSource
      Returns:
      Buffer
    • outputStream

      public OutputStream outputStream()
      Description copied from interface: BufferSink
      Returns an output stream that writes to this sink.
      Specified by:
      outputStream in interface BufferSink
    • emitCompleteSegments

      public Buffer emitCompleteSegments()
      Description copied from interface: BufferSink
      Writes complete segments to the underlying sink, if one exists. Like BufferSink.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.
       
      Specified by:
      emitCompleteSegments in interface BufferSink
    • emit

      public BufferSink emit()
      Description copied from interface: BufferSink
      Writes all buffered data to the underlying sink, if one exists. Like BufferSink.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());
       
      Specified by:
      emit in interface BufferSink
    • exhausted

      public boolean exhausted()
      Description copied from interface: BufferSource
      如果此源中没有更多字节,则返回 true。 这将阻塞,直到有字节可读取或源确实已耗尽
      Specified by:
      exhausted in interface BufferSource
      Returns:
      the true/false
    • require

      public void require(long byteCount) throws EOFException
      Description copied from interface: BufferSource
      当缓冲区至少包含 byteCount 个字节时返回。 如果在读取所需字节之前源已耗尽,则抛出 EOFException
      Specified by:
      require in interface BufferSource
      Parameters:
      byteCount - 字节数
      Throws:
      EOFException
    • request

      public boolean request(long byteCount)
      Description copied from interface: BufferSource
      如果缓冲区至少包含 byteCount 个字节,则返回 true,并根据需要对其进行扩展。 如果在读取请求的字节之前源已耗尽,则返回 false。
      Specified by:
      request in interface BufferSource
      Parameters:
      byteCount - 字节数
      Returns:
      the true/false
    • peek

      public BufferSource peek()
      Description copied from interface: BufferSource
      返回一个新的 BufferedSource,可从此 BufferedSource读取数据但不使用它。 一旦下次读取或关闭此源,返回的源将变为无效。
       
         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"
       
      Specified by:
      peek in interface BufferSource
      Returns:
      BufferSource
    • inputStream

      public InputStream inputStream()
      Description copied from interface: BufferSource
      返回从该源读取的输入流
      Specified by:
      inputStream in interface BufferSource
      Returns:
      InputStream
    • copyTo

      public final Buffer copyTo(OutputStream out) throws IOException
      将其内容复制到 out.
      Parameters:
      out - 输出流
      Returns:
      Buffer 内容
      Throws:
      IOException - 抛出异常
    • copyTo

      public final Buffer copyTo(OutputStream out, long offset, long byteCount) throws IOException
      从这里复制byteCount字节,从offset开始,复制到 out.
      Parameters:
      out - 输出流
      offset - 偏移量
      byteCount - 偏移量
      Returns:
      Buffer 内容
      Throws:
      IOException - 抛出异常
    • copyTo

      public final Buffer copyTo(Buffer out, long offset, long byteCount)
      从这里复制byteCount字节,从offset开始,复制到out.
      Parameters:
      out - 输出流
      offset - 偏移量
      byteCount - 偏移量
      Returns:
      Buffer 内容
    • writeTo

      public final Buffer writeTo(OutputStream out) throws IOException
      将其内容写入out.
      Parameters:
      out - 输出流
      Returns:
      Buffer 内容
      Throws:
      IOException - 抛出异常
    • writeTo

      public final Buffer writeTo(OutputStream out, long byteCount) throws IOException
      byteCount字节写入out.
      Parameters:
      out - 输出流
      byteCount - 偏移量
      Returns:
      Buffer 内容
      Throws:
      IOException - 抛出异常
    • readFrom

      public final Buffer readFrom(InputStream in) throws IOException
      in中的字节读入并转为bytes
      Parameters:
      in - 输入流
      Returns:
      Buffer 内容
      Throws:
      IOException - 抛出异常
    • readFrom

      public final Buffer readFrom(InputStream in, long byteCount) throws IOException
      Read byteCount bytes from in to this.
      Parameters:
      in - 输入流
      byteCount - 偏移量
      Returns:
      Buffer 内容
      Throws:
      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()
      Description copied from interface: BufferSource
      从该源中删除一个字节并返回它
      Specified by:
      readByte in interface BufferSource
      Returns:
      the true/false
    • getByte

      public final byte getByte(long pos)
      返回pos处的字节.
      Parameters:
      pos - long
      Returns:
      byte 内容
    • readShort

      public short readShort()
      Description copied from interface: BufferSource
      从此源中删除两个字节并返回一个短整型
      
         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());
       
      Specified by:
      readShort in interface BufferSource
      Returns:
      the short
    • readInt

      public int readInt()
      Description copied from interface: BufferSource
      从此源中删除四个字节并返回一个大整数
      
         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());
       
      Specified by:
      readInt in interface BufferSource
      Returns:
      the int
    • readLong

      public long readLong()
      Description copied from interface: BufferSource
      该源中删除八个字节并返回一个大长整型
      
         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());
       
      Specified by:
      readLong in interface BufferSource
      Returns:
      the long
    • readShortLe

      public short readShortLe()
      Description copied from interface: BufferSource
      从此源中删除两个字节并返回一个整型
      
         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:
      readShortLe in interface BufferSource
      Returns:
      the short
    • readIntLe

      public int readIntLe()
      Description copied from interface: BufferSource
      从该源中删除四个字节并返回一个小整数
      
         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());
       
      Specified by:
      readIntLe in interface BufferSource
      Returns:
      the int
    • readLongLe

      public long readLongLe()
      Description copied from interface: BufferSource
      从此源中删除八个字节并返回一个小长整型
      
         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:
      readLongLe in interface BufferSource
      Returns:
      the long
    • readDecimalLong

      public long readDecimalLong()
      Description copied from interface: BufferSource
      以有符号十进制形式从此源读取一个长整型值(即以十进制为基数的字符串,前导字符可选为“-”)。此操作将不断迭代,直到找到非数字字符。
      
         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:
      readDecimalLong in interface BufferSource
      Returns:
      the long
    • readHexadecimalUnsignedLong

      public long readHexadecimalUnsignedLong()
      Description copied from interface: BufferSource
      以十六进制形式(即以 16 进制表示的字符串)读取此源的长格式。此过程将不断迭代,直到找到非十六进制字符
      
         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:
      readHexadecimalUnsignedLong in interface BufferSource
      Returns:
      the long
    • readByteString

      public ByteString readByteString()
      Description copied from interface: BufferSource
      从中删除所有字节并将它们作为字节字符串返回
      Specified by:
      readByteString in interface BufferSource
      Returns:
      the ByteString
    • readByteString

      public ByteString readByteString(long byteCount) throws EOFException
      Description copied from interface: BufferSource
      从中删除 byteCount 个字节并将其作为字节字符串返回。
      Specified by:
      readByteString in interface BufferSource
      Returns:
      the ByteString
      Throws:
      EOFException
    • select

      public int select(SegmentBuffer segmentBuffer)
      Description copied from interface: BufferSource
      options 中查找第一个作为此缓冲区前缀的字符串,从此缓冲区中使用它,并返回其索引。 如果 options 中没有字节字符串是此缓冲区的前缀,则返回 -1,并且不消耗任何字节。 如果事先知道预期值集,则可以将其用作 BufferSource.readByteString() 甚至 BufferSource.readUtf8() 的替代。
      
         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:
      select in interface BufferSource
      Parameters:
      segmentBuffer - 索引值
      Returns:
      the int
    • selectPrefix

      public int selectPrefix(SegmentBuffer segmentBuffer, boolean selectTruncated)
      返回此缓冲区前缀的选项中的值的索引。如果没有找到值,则返回-1 此方法执行两个同步迭代:迭代trie和迭代这个缓冲区。当它在trie中到达一个结果时, 当它在trie中不匹配时,以及当缓冲区耗尽时,它将返回
      Parameters:
      selectTruncated - 如果可能的结果出现但被截断,则true返回-2 例如,如果缓冲区包含[ab],并且选项是[abc, abd],则返回-2 请注意,由于选项是按优先顺序列出的,而且第一个选项可能是另一个选项的前缀, 这使得情况变得复杂。例如,如果缓冲区包含[ab]而选项是[abc, a],则返回-2
    • readFully

      public void readFully(Buffer sink, long byteCount) throws EOFException
      Description copied from interface: BufferSource
      从中删除精确的 byteCount 个字节并将其附加到 sink。 如果无法读取请求的字节数,则抛出 EOFException
      Specified by:
      readFully in interface BufferSource
      Parameters:
      sink - 字节集合
      byteCount - 字节数
      Throws:
      EOFException
    • readAll

      public long readAll(Sink sink) throws IOException
      Description copied from interface: BufferSource
      从中删除所有字节并将其附加到 sink。返回写入 sink 的总字节数,如果已用尽,则为 0。
      Specified by:
      readAll in interface BufferSource
      Parameters:
      sink - 字节集合
      Returns:
      the long
      Throws:
      IOException - 异常
    • readUtf8

      public String readUtf8()
      Description copied from interface: BufferSource
      从中删除所有字节,将其解码为 UTF-8,然后返回字符串。如果此源为空,则返回空字符串。
      
         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:
      readUtf8 in interface BufferSource
      Returns:
      the string
    • readUtf8

      public String readUtf8(long byteCount) throws EOFException
      Description copied from interface: BufferSource
      从中删除 byteCount 个字节,将其解码为 UTF-8,并返回字符串。
      
         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:
      readUtf8 in interface BufferSource
      Parameters:
      byteCount - 字节数
      Returns:
      the string
      Throws:
      EOFException
    • readString

      public String readString(Charset charset)
      Description copied from interface: BufferSource
      从中删除所有字节,将其解码为charset,并返回字符串。
      Specified by:
      readString in interface BufferSource
      Parameters:
      charset - 字符编码
      Returns:
      the string
    • readString

      public String readString(long byteCount, Charset charset) throws EOFException
      Description copied from interface: BufferSource
      从中删除 byteCount 个字节,将其解码为 charset,并返回字符串。
      Specified by:
      readString in interface BufferSource
      Parameters:
      byteCount - 字节数
      charset - 字符编码
      Returns:
      the string
      Throws:
      EOFException
    • readUtf8Line

      public String readUtf8Line() throws EOFException
      Description copied from interface: BufferSource
      删除并返回直到下一个换行符(但不包括该换行符)的字符。 换行符为 "\n""\r\n";这些字符不包含在结果中。
      
         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());
       
      在流的末尾,此方法返回 null, 就像 BufferedReader 一样。 如果源未以换行符结尾,则假定为隐式换行符。一旦源耗尽,将返回 Null。将此方法用于人工生成的数据,其中尾随换行符是可选的。
      Specified by:
      readUtf8Line in interface BufferSource
      Returns:
      the string
      Throws:
      EOFException
    • readUtf8LineStrict

      public String readUtf8LineStrict() throws EOFException
      Description copied from interface: BufferSource
      删除并返回直到下一个换行符(但不包括该换行符)的字符。 换行符为 "\n""\r\n";这些字符不包含在结果中
      Specified by:
      readUtf8LineStrict in interface BufferSource
      Returns:
      the string
      Throws:
      EOFException
    • readUtf8LineStrict

      public String readUtf8LineStrict(long limit) throws EOFException
      Description copied from interface: BufferSource
      BufferSource.readUtf8LineStrict() 类似,不同之处在于它允许调用者指定允许的最长匹配。 使用它来防止可能不包含 "\n""\r\n" 的流。
      
         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:
      readUtf8LineStrict in interface BufferSource
      Parameters:
      limit - 限制
      Returns:
      the string
      Throws:
      EOFException
    • readUtf8Line

      public String readUtf8Line(long newline) throws EOFException
      Throws:
      EOFException
    • readUtf8CodePoint

      public int readUtf8CodePoint() throws EOFException
      Description copied from interface: BufferSource
      删除并返回单个 UTF-8 代码点,根据需要读取 1 到 4 个字节。 如果此源不是以正确编码的 UTF-8 代码点开头,则此方法将删除 1 个或多个非 UTF-8 字节并返回替换字符 (U+FFFD)。 这包括编码问题(输入不是正确编码的 UTF-8)、字符超出范围(超出 Unicode 的 0x10ffff 限制)、UTF-16 代理的代码 点 (U+d800..U+dfff) 和过长编码(例如,修改版 UTF-8 中的 NUL 字符为 0xc080
      Specified by:
      readUtf8CodePoint in interface BufferSource
      Returns:
      the int
      Throws:
      EOFException
    • readByteArray

      public byte[] readByteArray()
      Description copied from interface: BufferSource
      从中删除所有字节并将它们作为字节数组返回。
      Specified by:
      readByteArray in interface BufferSource
      Returns:
      the byte
    • readByteArray

      public byte[] readByteArray(long byteCount) throws EOFException
      Description copied from interface: BufferSource
      从中删除 byteCount 个字节并将其作为字节数组返回。
      Specified by:
      readByteArray in interface BufferSource
      Returns:
      the byte
      Throws:
      EOFException
    • read

      public int read(byte[] sink)
      Description copied from interface: BufferSource
      从中移除最多 sink.length 个字节并将其复制到 sink。 返回读取的字节数,如果此源已耗尽,则返回 -1。
      Specified by:
      read in interface BufferSource
      Parameters:
      sink - 字节集合
      Returns:
      the int
    • readFully

      public void readFully(byte[] sink) throws EOFException
      Description copied from interface: BufferSource
      从中删除恰好 sink.length 个字节并将其复制到 sink。 如果无法读取请求的字节数,则抛出 EOFException
      Specified by:
      readFully in interface BufferSource
      Parameters:
      sink - 字节集合
      Throws:
      EOFException
    • read

      public int read(byte[] sink, int offset, int byteCount)
      Description copied from interface: BufferSource
      从此处删除最多 byteCount 个字节并将其复制到 offset 处的 sink。 返回读取的字节数,如果此源已耗尽,则返回 -1。
      Specified by:
      read in interface BufferSource
      Parameters:
      sink - 字节集合
      offset - 偏移位
      byteCount - 字节数
      Returns:
      the int
    • read

      public int read(ByteBuffer sink) throws IOException
      Specified by:
      read in interface ReadableByteChannel
      Throws:
      IOException
    • clear

      public final void clear()
      丢弃此缓冲区中的所有字节。在使用完缓冲区后调用此方法将把它的段返回到池中
    • skip

      public void skip(long byteCount) throws EOFException
      从这个缓冲区的头部丢弃byteCount字节.
      Specified by:
      skip in interface BufferSource
      Parameters:
      byteCount - 字节数
      Throws:
      EOFException
    • write

      public Buffer write(ByteString byteString)
      Specified by:
      write in interface BufferSink
    • writeUtf8

      public Buffer writeUtf8(String string)
      Description copied from interface: BufferSink
      Encodes string in 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());
       
      Specified by:
      writeUtf8 in interface BufferSink
    • writeUtf8

      public Buffer writeUtf8(String string, int beginIndex, int endIndex)
      Description copied from interface: BufferSink
      Encodes the characters at beginIndex up to endIndex from string in 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());
       
      Specified by:
      writeUtf8 in interface BufferSink
    • writeUtf8CodePoint

      public Buffer writeUtf8CodePoint(int codePoint)
      Description copied from interface: BufferSink
      Encodes codePoint in UTF-8 and writes it to this sink.
      Specified by:
      writeUtf8CodePoint in interface BufferSink
    • writeString

      public Buffer writeString(String string, Charset charset)
      Description copied from interface: BufferSink
      Encodes string in charset and writes it to this sink.
      Specified by:
      writeString in interface BufferSink
    • writeString

      public Buffer writeString(String string, int beginIndex, int endIndex, Charset charset)
      Description copied from interface: BufferSink
      Encodes the characters at beginIndex up to endIndex from string in charset and writes it to this sink.
      Specified by:
      writeString in interface BufferSink
    • write

      public Buffer write(byte[] source)
      Description copied from interface: BufferSink
      Like OutputStream.write(byte[]), this writes a complete byte array to this sink.
      Specified by:
      write in interface BufferSink
    • write

      public Buffer write(byte[] source, int offset, int byteCount)
      Description copied from interface: BufferSink
      Like OutputStream.write(byte[], int, int), this writes byteCount bytes of source, starting at offset.
      Specified by:
      write in interface BufferSink
    • write

      public int write(ByteBuffer source) throws IOException
      Specified by:
      write in interface WritableByteChannel
      Throws:
      IOException
    • writeAll

      public long writeAll(Source source) throws IOException
      Description copied from interface: BufferSink
      Removes all bytes from source and appends them to this sink. Returns the number of bytes read which will be 0 if source is exhausted.
      Specified by:
      writeAll in interface BufferSink
      Throws:
      IOException
    • write

      public BufferSink write(Source source, long byteCount) throws IOException
      Description copied from interface: BufferSink
      Removes byteCount bytes from source and appends them to this sink.
      Specified by:
      write in interface BufferSink
      Throws:
      IOException
    • writeByte

      public Buffer writeByte(int b)
      Description copied from interface: BufferSink
      Writes a byte to this sink.
      Specified by:
      writeByte in interface BufferSink
    • writeShort

      public Buffer writeShort(int s)
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      writeShort in interface BufferSink
    • writeShortLe

      public Buffer writeShortLe(int s)
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      writeShortLe in interface BufferSink
    • writeInt

      public Buffer writeInt(int i)
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      writeInt in interface BufferSink
    • writeIntLe

      public Buffer writeIntLe(int i)
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      writeIntLe in interface BufferSink
    • writeLong

      public Buffer writeLong(long v)
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      writeLong in interface BufferSink
    • writeLongLe

      public Buffer writeLongLe(long v)
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      writeLongLe in interface BufferSink
    • writeDecimalLong

      public Buffer writeDecimalLong(long v)
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      writeDecimalLong in interface BufferSink
    • writeHexadecimalUnsignedLong

      public Buffer writeHexadecimalUnsignedLong(long v)
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      writeHexadecimalUnsignedLong in interface BufferSink
    • writableSegment

      public SectionBuffer writableSegment(int minimumCapacity)
      Parameters:
      minimumCapacity - int
      Returns:
      segment SectionBuffer Returns a tail segment that we can write at least minimumCapacity bytes to, creating it if necessary.
    • write

      public void write(Buffer source, long byteCount)
      Description copied from interface: Sink
      Removes byteCount bytes from source and appends them to this.
      Specified by:
      write in interface Sink
    • read

      public long read(Buffer sink, long byteCount)
      Description copied from interface: Source
      从中删除至少1个字节,最多为byteCount字节, 并将它们 附加到sink。返回读取的字节数,如果该源已耗尽,则返回-1
      Specified by:
      read in interface Source
      Parameters:
      sink - 缓冲
      byteCount - 长度大小
      Returns:
      the long
    • indexOf

      public long indexOf(byte b)
      Description copied from interface: BufferSource
      Equivalent to indexOf(b, 0).
      Specified by:
      indexOf in interface BufferSource
      Parameters:
      b -
      Returns:
      the long
    • indexOf

      public 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.
      Specified by:
      indexOf in interface BufferSource
      Parameters:
      b -
      fromIndex -
      Returns:
      the long
    • indexOf

      public long indexOf(byte b, long fromIndex, long toIndex)
      Description copied from interface: BufferSource
      如果在 fromIndextoIndex 范围内找到 b,则返回其索引。 如果未找到 b,或者 fromIndex == toIndex,则返回 -1。
      Specified by:
      indexOf in interface BufferSource
      Parameters:
      b - 字节
      fromIndex - 开始索引
      toIndex - 目标索引
      Returns:
      the long
    • indexOf

      public long indexOf(ByteString bytes) throws IOException
      Description copied from interface: BufferSource
      相当于indexOf(bytes, 0)
      Specified by:
      indexOf in interface BufferSource
      Parameters:
      bytes - 字节
      Returns:
      the long
      Throws:
      IOException - 异常
    • indexOf

      public long indexOf(ByteString bytes, long fromIndex) throws IOException
      Description copied from interface: BufferSource
      返回缓冲区中 bytes 的第一个匹配项的索引,位于 fromIndex 处或之后。 这会根据需要扩展缓冲区,直到找到 bytes。这会将无限数量的字节读入缓冲区。如果在找到请求的字节之前流已耗尽,则返回 -1。
      
         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:
      indexOf in interface BufferSource
      Parameters:
      bytes - 字节
      fromIndex - 字节索引
      Returns:
      the long
      Throws:
      IOException - 异常
    • indexOfElement

      public long indexOfElement(ByteString targetBytes)
      Description copied from interface: BufferSource
      Specified by:
      indexOfElement in interface BufferSource
      Parameters:
      targetBytes - 目标字节
      Returns:
      the long
    • indexOfElement

      public long indexOfElement(ByteString targetBytes, long fromIndex)
      Description copied from interface: BufferSource
      返回此缓冲区中位于 fromIndex 或之后且包含 targetBytes 中任意字节的第一个索引。 这会根据需要扩展缓冲区,直到找到目标字节。这会将无限数量的字节读入缓冲区。如果在找到请求的字节之前流已耗尽,则返回 -1。
      
         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'.
       
      Specified by:
      indexOfElement in interface BufferSource
      Parameters:
      targetBytes - 目标字节
      fromIndex - 开始索引
      Returns:
      the long
    • rangeEquals

      public boolean rangeEquals(long offset, ByteString bytes)
      Description copied from interface: BufferSource
      如果此源中 offset 处的字节等于 bytes,则返回 true。 这会根据需要扩展缓冲区,直到某个字节不匹配、所有字节都匹配,或者在足够的字节确定匹配之前流已耗尽。
      
         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));
       
      Specified by:
      rangeEquals in interface BufferSource
      Parameters:
      offset - 偏移量
      bytes - 字节
      Returns:
    • rangeEquals

      public boolean rangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount)
      Description copied from interface: BufferSource
      如果此源中 offset 处的 byteCount 个字节等于 bytesOffset 处的 bytes,则返回 true。 这会根据需要扩展缓冲区,直到某个字节不匹配、所有字节都匹配,或者在足够的字节确定匹配之前流已耗尽。
      Specified by:
      rangeEquals in interface BufferSource
      Parameters:
      offset - 偏移量
      bytes - 字节
      bytesOffset - 字节偏移量
      byteCount - 字节数
      Returns:
      the true/false
    • flush

      public void flush()
      Description copied from interface: BufferSink
      Writes 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());
       
      Specified by:
      flush in interface BufferSink
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in interface Sink
    • isOpen

      public boolean isOpen()
      Specified by:
      isOpen in interface Channel
    • close

      public void close()
      Description copied from interface: Source
      关闭此源并释放此源持有的资源. 读取闭源是一个错误。多次关闭源是安全的.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface Sink
      Specified by:
      close in interface Source
    • timeout

      public Timeout timeout()
      Description copied from interface: Source
      返回此源的超时时间.
      Specified by:
      timeout in interface Sink
      Specified by:
      timeout in interface Source
      Returns:
      超时时间
    • md5

      public ByteString md5()
      Returns:
      the 128-bit MD5 hash of this buffer.
    • sha1

      public ByteString sha1()
      Returns:
      the 160-bit SHA-1 hash of this buffer.
    • sha256

      public ByteString sha256()
      Returns:
      the 256-bit SHA-256 hash of this buffer.
    • sha512

      public ByteString sha512()
      Returns:
      the 512-bit SHA-512 hash of this buffer.
    • hmacSha1

      public ByteString hmacSha1(ByteString key)
      Parameters:
      key - ByteString
      Returns:
      the 160-bit SHA-1 HMAC of this buffer.
    • hmacSha256

      public ByteString hmacSha256(ByteString key)
      Parameters:
      key - ByteString
      Returns:
      the 256-bit SHA-256 HMAC of this buffer.
    • hmacSha512

      public final ByteString hmacSha512(ByteString key)
      Parameters:
      key - ByteString
      Returns:
      the 512-bit SHA-512 HMAC of this buffer.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns a human-readable string that describes the contents of this buffer. Typically this is a string like [text=Hello] or [hex=0000ffff].
      Overrides:
      toString in class Object
    • clone

      public Buffer clone()
      Returns a deep copy of this buffer.
      Overrides:
      clone in class Object
    • snapshot

      public ByteString snapshot()
      Returns an immutable copy of this buffer as a byte string.
    • snapshot

      public ByteString snapshot(int byteCount)
      Returns an immutable copy of the first byteCount bytes of this buffer as a byte string.
    • readUnsafe

      public Buffer.UnsafeCursor readUnsafe()
    • readUnsafe

      public Buffer.UnsafeCursor readUnsafe(Buffer.UnsafeCursor unsafeCursor)
    • readAndWriteUnsafe

      public Buffer.UnsafeCursor readAndWriteUnsafe()
    • readAndWriteUnsafe

      public Buffer.UnsafeCursor readAndWriteUnsafe(Buffer.UnsafeCursor unsafeCursor)