Package okio

Class Buffer

java.lang.Object
okio.Buffer
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Cloneable, ByteChannel, Channel, ReadableByteChannel, WritableByteChannel, BufferedSink, BufferedSource, Sink, Source

public final class Buffer extends Object implements BufferedSource, BufferedSink, Cloneable, ByteChannel
A collection of bytes in memory.

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.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    A handle to the underlying data in a buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns this source's internal buffer.
    final void
    Discards all bytes in this buffer.
    Returns a deep copy of this buffer.
    void
    Closes this source and releases the resources held by this source.
    final long
    Returns the number of bytes in segments that are not writable.
    final Buffer
    Copy the contents of this to out.
    final Buffer
    copyTo(OutputStream out, long offset, long byteCount)
    Copy byteCount bytes from this, starting at offset, to out.
    final Buffer
    copyTo(Buffer out, long offset, long byteCount)
    Copy byteCount bytes from this, starting at offset, to out.
    Writes all buffered data to the underlying sink, if one exists.
    Writes complete segments to the underlying sink, if one exists.
    boolean
     
    boolean
    Returns true if there are no more bytes in this source.
    void
    Writes all buffered data to the underlying sink, if one exists.
    This source's internal buffer.
    final byte
    getByte(long pos)
    Returns the byte at pos.
    int
     
    Returns the 160-bit SHA-1 HMAC of this buffer.
    Returns the 256-bit SHA-256 HMAC of this buffer.
    Returns the 512-bit SHA-512 HMAC of this buffer.
    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)
    Returns the index of b if it is found in the range of fromIndex inclusive to toIndex exclusive.
    long
    Equivalent to indexOf(bytes, 0).
    long
    indexOf(ByteString bytes, long fromIndex)
    Returns the index of the first match for bytes in the buffer at or after fromIndex.
    long
    long
    indexOfElement(ByteString targetBytes, long fromIndex)
    Returns the first index in this buffer that is at or after fromIndex and that contains any of the bytes in targetBytes.
    Returns an input stream that reads from this source.
    boolean
     
    md5()
    Returns the 128-bit MD5 hash of this buffer.
    Returns an output stream that writes to this sink.
    Returns a new BufferedSource that can read data from this BufferedSource without consuming it.
    boolean
    rangeEquals(long offset, ByteString bytes)
    Returns true if the bytes at offset in this source equal bytes.
    boolean
    rangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount)
    Returns true if byteCount bytes at offset in this source equal bytes at bytesOffset.
    int
    read(byte[] sink)
    Removes up to sink.length bytes from this and copies them into sink.
    int
    read(byte[] sink, int offset, int byteCount)
    Removes up to byteCount bytes from this and copies them into sink at offset.
    int
     
    long
    read(Buffer sink, long byteCount)
    Removes at least 1, and up to byteCount bytes from this and appends them to sink.
    long
    readAll(Sink sink)
    Removes all bytes from this and appends them to sink.
     
     
    byte
    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)
    Removes byteCount bytes 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)
    Removes byteCount bytes from this and returns them as a byte string.
    long
    Reads a long from this source in signed decimal form (i.e., as a string in base 10 with optional leading '-').
    final Buffer
    Read and exhaust bytes from in to this.
    final Buffer
    readFrom(InputStream in, long byteCount)
    Read byteCount bytes from in to this.
    void
    readFully(byte[] sink)
    Removes exactly sink.length bytes from this and copies them into sink.
    void
    readFully(Buffer sink, long byteCount)
    Removes exactly byteCount bytes from this and appends them to sink.
    long
    Reads a long form this source in hexadecimal form (i.e., as a string in base 16).
    int
    Removes four bytes from this source and returns a big-endian int.
    int
    Removes four bytes from this source and returns a little-endian int.
    long
    Removes eight bytes from this source and returns a big-endian long.
    long
    Removes eight bytes from this source and returns a little-endian long.
    short
    Removes two bytes from this source and returns a big-endian short.
    short
    Removes two bytes from this source and returns a little-endian short.
    readString(long byteCount, Charset charset)
    Removes byteCount bytes from this, decodes them as charset, and returns the string.
    Removes all bytes from this, decodes them as charset, and returns the string.
     
     
    Removes all bytes from this, decodes them as UTF-8, and returns the string.
    readUtf8(long byteCount)
    Removes byteCount bytes from this, decodes them as UTF-8, and returns the string.
    int
    Removes 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)
    Like BufferedSource.readUtf8LineStrict(), except this allows the caller to specify the longest allowed match.
    boolean
    request(long byteCount)
    Returns true when the buffer contains at least byteCount bytes, expanding it as necessary.
    void
    require(long byteCount)
    Returns when the buffer contains at least byteCount bytes.
    int
    select(Options options)
    Finds the first string in options that is a prefix of this buffer, consumes it from this buffer, and returns its index.
    Returns the 160-bit SHA-1 hash of this buffer.
    Returns the 256-bit SHA-256 hash of this buffer.
    Returns the 512-bit SHA-512 hash of this buffer.
    final long
    Returns the number of bytes currently in this buffer.
    void
    skip(long byteCount)
    Discards byteCount bytes from the head of this buffer.
    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 the timeout for this source.
    Returns a human-readable string that describes the contents of this buffer.
    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
    Write the contents of this to out.
    final Buffer
    writeTo(OutputStream out, long byteCount)
    Write byteCount bytes from this to 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
  • Constructor Details

    • Buffer

      public Buffer()
  • Method Details

    • size

      public final long size()
      Returns the number of bytes currently in this buffer.
    • buffer

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

      public Buffer getBuffer()
      Description copied from interface: BufferedSource
      This source's internal buffer.
      Specified by:
      getBuffer in interface BufferedSource
    • outputStream

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

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

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

      public boolean exhausted()
      Description copied from interface: BufferedSource
      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.
      Specified by:
      exhausted in interface BufferedSource
    • require

      public void require(long byteCount) throws EOFException
      Description copied from interface: BufferedSource
      Returns when the buffer contains at least byteCount bytes. Throws an EOFException if the source is exhausted before the required bytes can be read.
      Specified by:
      require in interface BufferedSource
      Throws:
      EOFException
    • request

      public boolean request(long byteCount)
      Description copied from interface: BufferedSource
      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.
      Specified by:
      request in interface BufferedSource
    • peek

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

      public InputStream inputStream()
      Description copied from interface: BufferedSource
      Returns an input stream that reads from this source.
      Specified by:
      inputStream in interface BufferedSource
    • copyTo

      public final Buffer copyTo(OutputStream out) throws IOException
      Copy the contents of this to out.
      Throws:
      IOException
    • copyTo

      public final Buffer copyTo(OutputStream out, long offset, long byteCount) throws IOException
      Copy byteCount bytes from this, starting at offset, to out.
      Throws:
      IOException
    • copyTo

      public final Buffer copyTo(Buffer out, long offset, long byteCount)
      Copy byteCount bytes from this, starting at offset, to out.
    • writeTo

      public final Buffer writeTo(OutputStream out) throws IOException
      Write the contents of this to out.
      Throws:
      IOException
    • writeTo

      public final Buffer writeTo(OutputStream out, long byteCount) throws IOException
      Write byteCount bytes from this to out.
      Throws:
      IOException
    • readFrom

      public final Buffer readFrom(InputStream in) throws IOException
      Read and exhaust bytes from in to this.
      Throws:
      IOException
    • readFrom

      public final Buffer readFrom(InputStream in, long byteCount) throws IOException
      Read byteCount bytes from in to this.
      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: BufferedSource
      Removes a byte from this source and returns it.
      Specified by:
      readByte in interface BufferedSource
    • getByte

      public final byte getByte(long pos)
      Returns the byte at pos.
    • readShort

      public short readShort()
      Description copied from interface: BufferedSource
      Removes 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());
       
      Specified by:
      readShort in interface BufferedSource
    • readInt

      public int readInt()
      Description copied from interface: BufferedSource
      Removes 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());
       
      Specified by:
      readInt in interface BufferedSource
    • readLong

      public long readLong()
      Description copied from interface: BufferedSource
      Removes 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());
       
      Specified by:
      readLong in interface BufferedSource
    • readShortLe

      public short readShortLe()
      Description copied from interface: BufferedSource
      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 BufferedSource
    • readIntLe

      public int readIntLe()
      Description copied from interface: BufferedSource
      Removes 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());
       
      Specified by:
      readIntLe in interface BufferedSource
    • readLongLe

      public long readLongLe()
      Description copied from interface: BufferedSource
      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 BufferedSource
    • readDecimalLong

      public long readDecimalLong()
      Description copied from interface: BufferedSource
      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 BufferedSource
    • readHexadecimalUnsignedLong

      public long readHexadecimalUnsignedLong()
      Description copied from interface: BufferedSource
      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 BufferedSource
    • readByteString

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

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

      public int select(Options options)
      Description copied from interface: BufferedSource
      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 BufferedSource.readByteString() or even BufferedSource.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 BufferedSource
    • readFully

      public void readFully(Buffer sink, long byteCount) throws EOFException
      Description copied from interface: BufferedSource
      Removes exactly byteCount bytes from this and appends them to sink. Throws an EOFException if the requested number of bytes cannot be read.
      Specified by:
      readFully in interface BufferedSource
      Throws:
      EOFException
    • readAll

      public long readAll(Sink sink) throws IOException
      Description copied from interface: BufferedSource
      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 BufferedSource
      Throws:
      IOException
    • readUtf8

      public String readUtf8()
      Description copied from interface: BufferedSource
      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 BufferedSource
    • readUtf8

      public String readUtf8(long byteCount) throws EOFException
      Description copied from interface: BufferedSource
      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 BufferedSource
      Throws:
      EOFException
    • readString

      public String readString(Charset charset)
      Description copied from interface: BufferedSource
      Removes all bytes from this, decodes them as charset, and returns the string.
      Specified by:
      readString in interface BufferedSource
    • readString

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

      @Nullable public String readUtf8Line() throws EOFException
      Description copied from interface: BufferedSource
      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 BufferedSource
      Throws:
      EOFException
    • readUtf8LineStrict

      public String readUtf8LineStrict() throws EOFException
      Description copied from interface: BufferedSource
      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 EOFException is thrown. Use this for machine-generated data where a missing line break implies truncated input.

      Specified by:
      readUtf8LineStrict in interface BufferedSource
      Throws:
      EOFException
    • readUtf8LineStrict

      public String readUtf8LineStrict(long limit) throws EOFException
      Description copied from interface: BufferedSource
      Like BufferedSource.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 EOFException 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 BufferedSource
      Throws:
      EOFException
    • readUtf8CodePoint

      public int readUtf8CodePoint() throws EOFException
      Description copied from interface: BufferedSource
      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 EOFException and consumes no input.

      If this source doesn't start with a properly-encoded UTF-8 code point, this method will remove 1 or more non-UTF-8 bytes and return the replacement character (U+FFFD). This covers encoding problems (the input is not properly-encoded UTF-8), characters out of range (beyond the 0x10ffff limit of Unicode), code points for UTF-16 surrogates (U+d800..U+dfff) and overlong encodings (such as 0xc080 for the NUL character in modified UTF-8).

      Specified by:
      readUtf8CodePoint in interface BufferedSource
      Throws:
      EOFException
    • readByteArray

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

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

      public int read(byte[] sink)
      Description copied from interface: BufferedSource
      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 BufferedSource
    • readFully

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

      public int read(byte[] sink, int offset, int byteCount)
      Description copied from interface: BufferedSource
      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 BufferedSource
    • read

      public int read(ByteBuffer sink) throws IOException
      Specified by:
      read in interface ReadableByteChannel
      Throws:
      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

      public void skip(long byteCount) throws EOFException
      Discards byteCount bytes from the head of this buffer.
      Specified by:
      skip in interface BufferedSource
      Throws:
      EOFException
    • write

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

      public Buffer writeUtf8(String string)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeUtf8

      public Buffer writeUtf8(String string, int beginIndex, int endIndex)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeUtf8CodePoint

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

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

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

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

      public Buffer write(byte[] source, int offset, int byteCount)
      Description copied from interface: BufferedSink
      Like OutputStream.write(byte[], int, int), this writes byteCount bytes of source, starting at offset.
      Specified by:
      write in interface BufferedSink
    • 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: BufferedSink
      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 BufferedSink
      Throws:
      IOException
    • write

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

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

      public Buffer writeShort(int s)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeShortLe

      public Buffer writeShortLe(int s)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeInt

      public Buffer writeInt(int i)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeIntLe

      public Buffer writeIntLe(int i)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeLong

      public Buffer writeLong(long v)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeLongLe

      public Buffer writeLongLe(long v)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeDecimalLong

      public Buffer writeDecimalLong(long v)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • writeHexadecimalUnsignedLong

      public Buffer writeHexadecimalUnsignedLong(long v)
      Description copied from interface: BufferedSink
      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 BufferedSink
    • 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
      Removes at least 1, and up to byteCount bytes from this and appends them to sink. Returns the number of bytes read, or -1 if this source is exhausted.
      Specified by:
      read in interface Source
    • indexOf

      public long indexOf(byte b)
      Description copied from interface: BufferedSource
      Equivalent to indexOf(b, 0).
      Specified by:
      indexOf in interface BufferedSource
    • 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 BufferedSource
    • indexOf

      public long indexOf(byte b, long fromIndex, long toIndex)
      Description copied from interface: BufferedSource
      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 BufferedSource
    • indexOf

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

      public long indexOf(ByteString bytes, long fromIndex) throws IOException
      Description copied from interface: BufferedSource
      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 BufferedSource
      Throws:
      IOException
    • indexOfElement

      public long indexOfElement(ByteString targetBytes)
      Description copied from interface: BufferedSource
      Specified by:
      indexOfElement in interface BufferedSource
    • indexOfElement

      public long indexOfElement(ByteString targetBytes, long fromIndex)
      Description copied from interface: BufferedSource
      Returns the first index in this buffer that is at or after fromIndex and that contains any of the bytes in targetBytes. 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'.
       
      Specified by:
      indexOfElement in interface BufferedSource
    • rangeEquals

      public boolean rangeEquals(long offset, ByteString bytes)
      Description copied from interface: BufferedSource
      Returns 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 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 BufferedSource
    • rangeEquals

      public boolean rangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount)
      Description copied from interface: BufferedSource
      Returns true 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 BufferedSource
    • flush

      public void flush()
      Description copied from interface: BufferedSink
      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 BufferedSink
      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
      Closes 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.
      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
      Returns the timeout for this source.
      Specified by:
      timeout in interface Sink
      Specified by:
      timeout in interface Source
    • 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)
      Returns the 160-bit SHA-1 HMAC of this buffer.
    • hmacSha256

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

      public final ByteString hmacSha512(ByteString key)
      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 final ByteString snapshot()
      Returns an immutable copy of this buffer as a byte string.
    • snapshot

      public final ByteString snapshot(int byteCount)
      Returns an immutable copy of the first byteCount bytes of this buffer as a byte string.
    • 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)