Class Buffer

java.lang.Object
org.aoju.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
  • 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()
      Specified by:
      buffer in interface BufferSink
      Specified by:
      buffer in interface BufferSource
      Returns:
      this source's internal buffer. use getBuffer() instead.
    • getBuffer

      public Buffer getBuffer()
      Specified by:
      getBuffer in interface BufferSource
      Returns:
      This source's internal buffer.
    • outputStream

      public OutputStream outputStream()
      Specified by:
      outputStream in interface BufferSink
    • emitCompleteSegments

      public Buffer emitCompleteSegments()
      Specified by:
      emitCompleteSegments in interface BufferSink
    • emit

      public BufferSink emit()
      Specified by:
      emit in interface BufferSink
    • exhausted

      public boolean exhausted()
      Specified by:
      exhausted in interface BufferSource
      Returns:
      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.
    • require

      public void require(long byteCount) throws EOFException
      Description copied from interface: BufferSource
      when the buffer contains at least byteCount bytes.
      Specified by:
      require in interface BufferSource
      Parameters:
      byteCount - long
      Throws:
      EOFException
    • request

      public boolean request(long byteCount)
      Specified by:
      request in interface BufferSource
      Parameters:
      byteCount - long
      Returns:
      true when the buffer contains at least byteCount bytes, expanding it as necessary. Returns false if the source is exhausted before the requested bytes can be read.
    • peek

      public BufferSource peek()
      Description copied from interface: BufferSource
      a new BufferSource that can read data from this BufferSource without 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"
      
         BufferSource 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:
      the long
    • inputStream

      public InputStream inputStream()
      Description copied from interface: BufferSource
      an input stream that reads from this source.
      Specified by:
      inputStream in interface BufferSource
      Returns:
      the 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()
    • readByte

      public byte readByte()
      Specified by:
      readByte in interface BufferSource
      Returns:
      Removes a byte from this source and returns it.
    • getByte

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

      public short readShort()
      Specified by:
      readShort in interface BufferSource
      Returns:
      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());
       
    • readInt

      public int readInt()
      Specified by:
      readInt in interface BufferSource
      Returns:
      the int
    • readLong

      public long readLong()
      Specified by:
      readLong in interface BufferSource
      Returns:
      the long
    • readShortLe

      public short readShortLe()
      Description copied from interface: BufferSource
      Removes 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());
       
      Specified by:
      readShortLe in interface BufferSource
      Returns:
      the short
    • readIntLe

      public int readIntLe()
      Specified by:
      readIntLe in interface BufferSource
      Returns:
      the int
    • readLongLe

      public long readLongLe()
      Description copied from interface: BufferSource
      Removes 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());
       
      Specified by:
      readLongLe in interface BufferSource
      Returns:
      the long
    • readDecimalLong

      public long readDecimalLong()
      Description copied from interface: BufferSource
      Reads 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());
       
      Specified by:
      readDecimalLong in interface BufferSource
      Returns:
      the long
    • readHexadecimalUnsignedLong

      public long readHexadecimalUnsignedLong()
      Description copied from interface: BufferSource
      Reads 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());
       
      Specified by:
      readHexadecimalUnsignedLong in interface BufferSource
      Returns:
      the long
    • readByteString

      public ByteString readByteString()
      Description copied from interface: BufferSource
      Removes all bytes bytes from this and returns them as a byte string.
      Specified by:
      readByteString in interface BufferSource
      Returns:
      the ByteString
    • readByteString

      public ByteString readByteString(long byteCount) throws EOFException
      Description copied from interface: BufferSource
      Removes byteCount bytes from this and returns them as a byte string.
      Specified by:
      readByteString in interface BufferSource
      Parameters:
      byteCount - long
      Returns:
      the ByteString
      Throws:
      EOFException
    • select

      public int select(Blending options)
      Description copied from interface: BufferSource
      Finds the first string in options that is a prefix of this buffer, consumes it from this buffer, and returns its index. If no byte string in options is a prefix of this buffer this returns -1 and no bytes are consumed.

      This can be used as an alternative to BufferSource.readByteString() or even BufferSource.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());
       
      Specified by:
      select in interface BufferSource
      Parameters:
      options - Options
      Returns:
      the int
    • selectPrefix

      public int selectPrefix(Blending options, 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
      Removes exactly byteCount bytes from this and appends them to sink. Throws an IOException if the requested number of bytes cannot be read.
      Specified by:
      readFully in interface BufferSource
      Parameters:
      sink - Buffer
      byteCount - long
      Throws:
      EOFException
    • readAll

      public long readAll(Sink sink) throws IOException
      Description copied from interface: BufferSource
      Removes all bytes from this and appends them to sink. Returns the total number of bytes written to sink which will be 0 if this is exhausted.
      Specified by:
      readAll in interface BufferSource
      Parameters:
      sink - Sink
      Returns:
      the long
      Throws:
      IOException - IOException IOException.
    • readUtf8

      public String readUtf8()
      Description copied from interface: BufferSource
      Removes 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());
       
      Specified by:
      readUtf8 in interface BufferSource
      Returns:
      the String
    • readUtf8

      public String readUtf8(long byteCount) throws EOFException
      Description copied from interface: BufferSource
      Removes byteCount bytes 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());
       
      Specified by:
      readUtf8 in interface BufferSource
      Parameters:
      byteCount - long
      Returns:
      the String
      Throws:
      EOFException
    • readString

      public String readString(Charset charset)
      Specified by:
      readString in interface BufferSource
      Parameters:
      charset - Charset Removes all bytes from this, decodes them as charset,
      Returns:
      the string.
    • readString

      public String readString(long byteCount, Charset charset) throws EOFException
      Description copied from interface: BufferSource
      Removes byteCount bytes from this, decodes them as charset,
      Specified by:
      readString in interface BufferSource
      Parameters:
      byteCount - byteCount
      charset - Charset
      Returns:
      the string.
      Throws:
      EOFException
    • readUtf8Line

      public String readUtf8Line() throws EOFException
      Description copied from interface: BufferSource
      Removes and returns characters up to but not including the next line break. A line break is either "\n" or "\r\n"; these characters are not included in the result.
      
      
         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.

      Specified by:
      readUtf8Line in interface BufferSource
      Returns:
      the String
      Throws:
      EOFException
    • readUtf8LineStrict

      public String readUtf8LineStrict() throws EOFException
      Description copied from interface: BufferSource
      Removes and returns characters up to but not including the next line break. A line break is either "\n" or "\r\n"; these characters are not included in the result.

      On the end of the stream this method throws. Every call must consume either '\r\n' or '\n'. If these characters are absent in the stream, an IOException is thrown. Use this for machine-generated data where a missing line break implies truncated input.

      Specified by:
      readUtf8LineStrict in interface BufferSource
      Returns:
      the String
      Throws:
      EOFException
    • readUtf8LineStrict

      public String readUtf8LineStrict(long limit) throws EOFException
      Description copied from interface: BufferSource
      Like BufferSource.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 limit UTF-8 bytes, and the maximum number of bytes scanned is limit + 2. If limit == 0 this will always throw an IOException because 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));
       
      Specified by:
      readUtf8LineStrict in interface BufferSource
      Parameters:
      limit - long
      Returns:
      String String
      Throws:
      EOFException
    • readUtf8Line

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

      public int readUtf8CodePoint() throws EOFException
      Specified by:
      readUtf8CodePoint in interface BufferSource
      Returns:
      Removes 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 IOException 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).

      Throws:
      EOFException
    • readByteArray

      public byte[] readByteArray()
      Description copied from interface: BufferSource
      Removes all bytes from this and returns them as a byte array.
      Specified by:
      readByteArray in interface BufferSource
      Returns:
      the byte[]
    • readByteArray

      public byte[] readByteArray(long byteCount) throws EOFException
      Description copied from interface: BufferSource
      Removes byteCount bytes from this and returns them as a byte array.
      Specified by:
      readByteArray in interface BufferSource
      Parameters:
      byteCount - long
      Returns:
      the byte[]
      Throws:
      EOFException
    • read

      public int read(byte[] sink)
      Description copied from interface: BufferSource
      Removes up to sink.length bytes from this and copies them into sink. Returns the number of bytes read, or -1 if this source is exhausted.
      Specified by:
      read in interface BufferSource
      Parameters:
      sink - byte[]
      Returns:
      the long
    • readFully

      public void readFully(byte[] sink) throws EOFException
      Description copied from interface: BufferSource
      Removes exactly sink.length bytes from this and copies them into sink. Throws an IOException if the requested number of bytes cannot be read.
      Specified by:
      readFully in interface BufferSource
      Parameters:
      sink - byte[]
      Throws:
      EOFException
    • read

      public int read(byte[] sink, int offset, int byteCount)
      Description copied from interface: BufferSource
      Removes up to byteCount bytes from this and copies them into sink at offset. Returns the number of bytes read, or -1 if this source is exhausted.
      Specified by:
      read in interface BufferSource
      Parameters:
      sink - byte[]
      offset - int
      byteCount - int
      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 - long
      Throws:
      EOFException
    • write

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

      public Buffer writeUtf8(String string)
      Specified by:
      writeUtf8 in interface BufferSink
    • writeUtf8

      public Buffer writeUtf8(String string, int beginIndex, int endIndex)
      Specified by:
      writeUtf8 in interface BufferSink
    • writeUtf8CodePoint

      public Buffer writeUtf8CodePoint(int codePoint)
      Specified by:
      writeUtf8CodePoint in interface BufferSink
    • writeString

      public Buffer writeString(String string, Charset charset)
      Specified by:
      writeString in interface BufferSink
    • writeString

      public Buffer writeString(String string, int beginIndex, int endIndex, Charset charset)
      Specified by:
      writeString in interface BufferSink
    • write

      public Buffer write(byte[] source)
      Specified by:
      write in interface BufferSink
    • write

      public Buffer write(byte[] source, int offset, int byteCount)
      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
      Specified by:
      writeAll in interface BufferSink
      Throws:
      IOException
    • write

      public BufferSink write(Source source, long byteCount) throws IOException
      Specified by:
      write in interface BufferSink
      Throws:
      IOException
    • writeByte

      public Buffer writeByte(int b)
      Specified by:
      writeByte in interface BufferSink
    • writeShort

      public Buffer writeShort(int s)
      Specified by:
      writeShort in interface BufferSink
    • writeShortLe

      public Buffer writeShortLe(int s)
      Specified by:
      writeShortLe in interface BufferSink
    • writeInt

      public Buffer writeInt(int i)
      Specified by:
      writeInt in interface BufferSink
    • writeIntLe

      public Buffer writeIntLe(int i)
      Specified by:
      writeIntLe in interface BufferSink
    • writeLong

      public Buffer writeLong(long v)
      Specified by:
      writeLong in interface BufferSink
    • writeLongLe

      public Buffer writeLongLe(long v)
      Specified by:
      writeLongLe in interface BufferSink
    • writeDecimalLong

      public Buffer writeDecimalLong(long v)
      Specified by:
      writeDecimalLong in interface BufferSink
    • writeHexadecimalUnsignedLong

      public Buffer writeHexadecimalUnsignedLong(long v)
      Specified by:
      writeHexadecimalUnsignedLong in interface BufferSink
    • writableSegment

      public Segment writableSegment(int minimumCapacity)
      Parameters:
      minimumCapacity - int
      Returns:
      segment Segment 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)
      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 - byte
      Returns:
      the long
    • indexOf

      public long indexOf(byte b, long fromIndex)
      Description copied from interface: BufferSource
      Returns the index of the first b in the buffer at or after fromIndex. 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.
      
      
         Buffer buffer = new Buffer();
         buffer.writeUtf8("Don't move! He can't see us if we don't move.");
      
         byte m = 'm';
         assertEquals(6,  buffer.indexOf(m));
         assertEquals(40, buffer.indexOf(m, 12));
       
      Specified by:
      indexOf in interface BufferSource
      Parameters:
      b - byte
      fromIndex - long
      Returns:
      the long
    • indexOf

      public long indexOf(byte b, long fromIndex, long toIndex)
      Description copied from interface: BufferSource
      Returns the index of b if it is found in the range of fromIndex inclusive to toIndex exclusive. If b isn't found, or if fromIndex == toIndex, then -1 is returned.

      The scan terminates at either toIndex or the end of the buffer, whichever comes first. The maximum number of bytes scanned is toIndex-fromIndex.

      Specified by:
      indexOf in interface BufferSource
      Parameters:
      b - byte
      fromIndex - long
      toIndex - long
      Returns:
      the long
    • indexOf

      public long indexOf(ByteString bytes) throws IOException
      Description copied from interface: BufferSource
      Equivalent to indexOf(bytes, 0).
      Specified by:
      indexOf in interface BufferSource
      Parameters:
      bytes - ByteString
      Returns:
      the long
      Throws:
      IOException - IOException IOException.
    • indexOf

      public long indexOf(ByteString bytes, long fromIndex) throws IOException
      Description copied from interface: BufferSource
      Returns the index of the first match for 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.
      
      
         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 - ByteString
      fromIndex - long
      Returns:
      the long
      Throws:
      IOException - IOException IOException.
    • indexOfElement

      public long indexOfElement(ByteString targetBytes)
      Specified by:
      indexOfElement in interface BufferSource
      Parameters:
      targetBytes - ByteString Equivalent to indexOfElement(targetBytes, 0).
      Returns:
      the long
    • indexOfElement

      public long indexOfElement(ByteString targetBytes, long fromIndex)
      Specified by:
      indexOfElement in interface BufferSource
      Parameters:
      targetBytes - ByteString
      fromIndex - long
      Returns:
      the long
    • rangeEquals

      public boolean rangeEquals(long offset, ByteString bytes)
      Description copied from interface: BufferSource
      true if the bytes at offset in this source equal bytes. 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 first 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 - long
      bytes - ByteString
      Returns:
      the long
    • rangeEquals

      public boolean rangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount)
      Description copied from interface: BufferSource
      if byteCount bytes at offset in this source equal bytes * at bytesOffset. 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.
      Specified by:
      rangeEquals in interface BufferSource
      Parameters:
      offset - long
      bytes - ByteString
      bytesOffset - int
      byteCount - int
      Returns:
      the true
    • flush

      public void flush()
      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 final ByteString md5()
      Returns:
      the 128-bit MD5 hash of this buffer.
    • sha1

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

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

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

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

      public final 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()
      Overrides:
      toString in class Object
    • clone

      public Buffer clone()
      Overrides:
      clone in class Object
    • snapshot

      public final ByteString snapshot()
    • snapshot

      public final ByteString snapshot(int byteCount)
    • readUnsafe

      public final Buffer.UnsafeCursor readUnsafe()
    • readUnsafe

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

      public final Buffer.UnsafeCursor readAndWriteUnsafe()
    • readAndWriteUnsafe

      public final Buffer.UnsafeCursor readAndWriteUnsafe(Buffer.UnsafeCursor unsafeCursor)