Class Buffer


  • public class Buffer
    extends Object
    Compatibility layer for OkHttp.
    • Constructor Detail

      • Buffer

        public Buffer()
      • Buffer

        public Buffer​(byte[] bytes)
    • Method Detail

      • getBytes

        public final byte[] getBytes()
        Returns the bytes in this source as a byte array.
        Returns:
        the bytes in this source as a byte array.
      • size

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

        public final byte[] readByteArray()
        Removes all bytes from this and returns them as a byte array.
        Returns:
        the byte array containing the bytes in this buffer.
      • readString

        public final String readString​(Charset charset)
        Removes all bytes from this, decodes them as charset, and returns the string.
        Parameters:
        charset - the charset to use to decode the bytes.
        Returns:
        the string containing the bytes in this buffer.
      • readUtf8

        public final String readUtf8()
        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());
         
      • write

        public final Buffer write​(byte[] bytes)
        Like OutputStream.write(byte[]), this writes a complete byte array to this sink.
        Parameters:
        bytes - the bytes to write.
      • write

        public Buffer write​(byte[] bytes,
                            int off,
                            int len)
        Like OutputStream.write(byte[], int, int), this writes byteCount bytes of source, starting at offset.
        Parameters:
        bytes - the bytes to write.
        off - the index of the first byte to write.
        len - the number of bytes to write.
      • writeString

        public final Buffer writeString​(String string,
                                        Charset charset)
        Encodes string in charset and writes it to this buffer.
        Parameters:
        string - the string to write.
        charset - the charset to use to encode the string.
      • writeUtf8

        public final Buffer writeUtf8​(String string)
        Encodes string in UTF-8 and writes it to this buffer.
        
        
           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());
         
        Parameters:
        string - the string to write.
      • inputStream

        public final InputStream inputStream()
        Returns an input stream that reads from bytes cloned from this buffer.
        Returns:
        an input stream that reads from bytes cloned from this buffer.
      • flush

        public final void flush()
        No operation is performed, added for compatibility.