Package okio

Interface BufferedSource

  • All Superinterfaces:
    AutoCloseable, Closeable, Source
    All Known Implementing Classes:
    Buffer

    public interface BufferedSource
    extends Source
    A source that keeps a buffer internally so that callers can do small reads without a performance penalty. It also allows clients to read ahead, buffering as much as necessary before consuming input.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      Buffer buffer()
      Returns this source's internal buffer.
      boolean exhausted()
      Returns true if there are no more bytes in this source.
      long indexOf​(byte b)
      Returns the index of the first b in the buffer.
      long indexOf​(byte b, long fromIndex)
      Returns the index of the first b in the buffer at or after fromIndex.
      long indexOf​(ByteString bytes)
      Returns the index of the first match for bytes in the buffer.
      long indexOf​(ByteString bytes, long fromIndex)
      Returns the index of the first match for bytes in the buffer at or after fromIndex.
      long indexOfElement​(ByteString targetBytes)
      Returns the index of the first byte in targetBytes in the buffer.
      long indexOfElement​(ByteString targetBytes, long fromIndex)
      Returns the index of the first byte in targetBytes in the buffer at or after fromIndex.
      InputStream inputStream()
      Returns an input stream that reads from this source.
      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.
      long readAll​(Sink sink)
      Removes all bytes from this and appends them to sink.
      byte readByte()
      Removes a byte from this source and returns it.
      byte[] readByteArray()
      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.
      ByteString readByteString()
      Removes all bytes bytes from this and returns them as a byte string.
      ByteString readByteString​(long byteCount)
      Removes byteCount bytes from this and returns them as a byte string.
      long readDecimalLong()
      Reads a long from this source in signed decimal form (i.e., as a string in base 10 with optional leading '-').
      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 readHexadecimalUnsignedLong()
      Reads a long form this source in hexadecimal form (i.e., as a string in base 16).
      int readInt()
      Removes four bytes from this source and returns a big-endian int.
      int readIntLe()
      Removes four bytes from this source and returns a little-endian int.
      long readLong()
      Removes eight bytes from this source and returns a big-endian long.
      long readLongLe()
      Removes eight bytes from this source and returns a little-endian long.
      short readShort()
      Removes two bytes from this source and returns a big-endian short.
      short readShortLe()
      Removes two bytes from this source and returns a little-endian short.
      String readString​(long byteCount, Charset charset)
      Removes byteCount bytes from this, decodes them as charset, and returns the string.
      String readString​(Charset charset)
      Removes all bytes from this, decodes them as charset, and returns the string.
      String readUtf8()
      Removes all bytes from this, decodes them as UTF-8, and returns the string.
      String readUtf8​(long byteCount)
      Removes byteCount bytes from this, decodes them as UTF-8, and returns the string.
      int readUtf8CodePoint()
      Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary.
      String readUtf8Line()
      Removes and returns characters up to but not including the next line break.
      String readUtf8LineStrict()
      Removes and returns characters up to but not including the next line break.
      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.
      void skip​(long byteCount)
      Reads and discards byteCount bytes from this source.
    • Method Detail

      • buffer

        Buffer buffer()
        Returns this source's internal buffer.
      • exhausted

        boolean exhausted()
                   throws IOException
        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.
        Throws:
        IOException
      • require

        void require​(long byteCount)
              throws IOException
        Returns when the buffer contains at least byteCount bytes. Throws an EOFException if the source is exhausted before the required bytes can be read.
        Throws:
        IOException
      • request

        boolean request​(long byteCount)
                 throws IOException
        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.
        Throws:
        IOException
      • readByte

        byte readByte()
               throws IOException
        Removes a byte from this source and returns it.
        Throws:
        IOException
      • readShort

        short readShort()
                 throws IOException
        Removes two bytes from this source and returns a big-endian short.
        Throws:
        IOException
      • readShortLe

        short readShortLe()
                   throws IOException
        Removes two bytes from this source and returns a little-endian short.
        Throws:
        IOException
      • readInt

        int readInt()
             throws IOException
        Removes four bytes from this source and returns a big-endian int.
        Throws:
        IOException
      • readIntLe

        int readIntLe()
               throws IOException
        Removes four bytes from this source and returns a little-endian int.
        Throws:
        IOException
      • readLong

        long readLong()
               throws IOException
        Removes eight bytes from this source and returns a big-endian long.
        Throws:
        IOException
      • readLongLe

        long readLongLe()
                 throws IOException
        Removes eight bytes from this source and returns a little-endian long.
        Throws:
        IOException
      • readDecimalLong

        long readDecimalLong()
                      throws IOException
        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.
        Throws:
        NumberFormatException - if the found digits do not fit into a long or a decimal number was not present.
        IOException
      • readHexadecimalUnsignedLong

        long readHexadecimalUnsignedLong()
                                  throws IOException
        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.
        Throws:
        NumberFormatException - if the found hexadecimal does not fit into a long or hexadecimal was not found.
        IOException
      • skip

        void skip​(long byteCount)
           throws IOException
        Reads and discards byteCount bytes from this source. Throws an EOFException if the source is exhausted before the requested bytes can be skipped.
        Throws:
        IOException
      • readByteString

        ByteString readByteString​(long byteCount)
                           throws IOException
        Removes byteCount bytes from this and returns them as a byte string.
        Throws:
        IOException
      • readByteArray

        byte[] readByteArray()
                      throws IOException
        Removes all bytes from this and returns them as a byte array.
        Throws:
        IOException
      • readByteArray

        byte[] readByteArray​(long byteCount)
                      throws IOException
        Removes byteCount bytes from this and returns them as a byte array.
        Throws:
        IOException
      • read

        int read​(byte[] sink)
          throws IOException
        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.
        Throws:
        IOException
      • readFully

        void readFully​(byte[] sink)
                throws IOException
        Removes exactly sink.length bytes from this and copies them into sink. Throws an EOFException if the requested number of bytes cannot be read.
        Throws:
        IOException
      • read

        int read​(byte[] sink,
                 int offset,
                 int byteCount)
          throws IOException
        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.
        Throws:
        IOException
      • readFully

        void readFully​(Buffer sink,
                       long byteCount)
                throws IOException
        Removes exactly byteCount bytes from this and appends them to sink. Throws an EOFException if the requested number of bytes cannot be read.
        Throws:
        IOException
      • readAll

        long readAll​(Sink sink)
              throws IOException
        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.
        Throws:
        IOException
      • readUtf8

        String readUtf8()
                 throws IOException
        Removes all bytes from this, decodes them as UTF-8, and returns the string.
        Throws:
        IOException
      • readUtf8

        String readUtf8​(long byteCount)
                 throws IOException
        Removes byteCount bytes from this, decodes them as UTF-8, and returns the string.
        Throws:
        IOException
      • readUtf8Line

        String readUtf8Line()
                     throws IOException
        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 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.

        Throws:
        IOException
      • readUtf8LineStrict

        String readUtf8LineStrict()
                           throws IOException
        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.

        Throws:
        IOException
      • readUtf8CodePoint

        int readUtf8CodePoint()
                       throws IOException
        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).

        Throws:
        IOException
      • readString

        String readString​(long byteCount,
                          Charset charset)
                   throws IOException
        Removes byteCount bytes from this, decodes them as charset, and returns the string.
        Throws:
        IOException
      • indexOf

        long indexOf​(byte b)
              throws IOException
        Returns the index of the first b in the buffer. 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.
        Throws:
        IOException
      • indexOf

        long indexOf​(byte b,
                     long fromIndex)
              throws IOException
        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.
        Throws:
        IOException
      • indexOf

        long indexOf​(ByteString bytes)
              throws IOException
        Returns the index of the first match for bytes in the buffer. 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.
        Throws:
        IOException
      • indexOf

        long indexOf​(ByteString bytes,
                     long fromIndex)
              throws IOException
        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.
        Throws:
        IOException
      • indexOfElement

        long indexOfElement​(ByteString targetBytes)
                     throws IOException
        Returns the index of the first byte in targetBytes in the buffer. 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.
        Throws:
        IOException
      • indexOfElement

        long indexOfElement​(ByteString targetBytes,
                            long fromIndex)
                     throws IOException
        Returns the index of the first byte in targetBytes in the buffer at or after fromIndex. 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.
        Throws:
        IOException
      • inputStream

        InputStream inputStream()
        Returns an input stream that reads from this source.