Package 

Interface BufferedSource

  • All Implemented Interfaces:
    com.mobwith.httpmodule.Source , java.io.Closeable , java.lang.AutoCloseable , java.nio.channels.Channel , java.nio.channels.ReadableByteChannel

    
    public interface BufferedSource
     implements Source, ReadableByteChannel
                        

    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

      Modifier and Type Method Description
      abstract Buffer buffer() Returns this source's internal buffer.
      abstract boolean exhausted() Returns true if there are no more bytes in this source.
      abstract void require(long byteCount) Returns when the buffer contains at least {@code byteCount} bytes.
      abstract boolean request(long byteCount) Returns true when the buffer contains at least {@code byteCount} bytes, expanding it asnecessary.
      abstract byte readByte() Removes a byte from this source and returns it.
      abstract short readShort() Removes two bytes from this source and returns a big-endian short.
      abstract short readShortLe() Removes two bytes from this source and returns a little-endian short.
      abstract int readInt() Removes four bytes from this source and returns a big-endian int.
      abstract int readIntLe() Removes four bytes from this source and returns a little-endian int.
      abstract long readLong() Removes eight bytes from this source and returns a big-endian long.
      abstract long readLongLe() Removes eight bytes from this source and returns a little-endian long.
      abstract long readDecimalLong() Reads a long from this source in signed decimal form (i.e., as a string in base 10 withoptional leading '-').
      abstract long readHexadecimalUnsignedLong() Reads a long form this source in hexadecimal form (i.e., as a string in base 16).
      abstract void skip(long byteCount) Reads and discards {@code byteCount} bytes from this source.
      abstract ByteString readByteString() Removes all bytes bytes from this and returns them as a byte string.
      abstract ByteString readByteString(long byteCount) Removes {@code byteCount} bytes from this and returns them as a byte string.
      abstract int select(Options options) Finds the first string in {@code options} that is a prefix of this buffer, consumes it fromthis buffer, and returns its index.
      abstract Array<byte> readByteArray() Removes all bytes from this and returns them as a byte array.
      abstract Array<byte> readByteArray(long byteCount) Removes {@code byteCount} bytes from this and returns them as a byte array.
      abstract int read(Array<byte> sink) Removes up to {@code sink.length} bytes from this and copies them into {@code sink}.
      abstract void readFully(Array<byte> sink) Removes exactly {@code sink.length} bytes from this and copies them into {@code sink}.
      abstract int read(Array<byte> sink, int offset, int byteCount) Removes up to {@code byteCount} bytes from this and copies them into {@code sink} at {@code * offset}.
      abstract void readFully(Buffer sink, long byteCount) Removes exactly {@code byteCount} bytes from this and appends them to {@code sink}.
      abstract long readAll(Sink sink) Removes all bytes from this and appends them to {@code sink}.
      abstract String readUtf8() Removes all bytes from this, decodes them as UTF-8, and returns the string.
      abstract String readUtf8(long byteCount) Removes {@code byteCount} bytes from this, decodes them as UTF-8, and returns the string.
      abstract String readUtf8Line() Removes and returns characters up to but not including the next line break.
      abstract String readUtf8LineStrict() Removes and returns characters up to but not including the next line break.
      abstract String readUtf8LineStrict(long limit) Like readUtf8LineStrict, except this allows the caller to specify the longestallowed match.
      abstract int readUtf8CodePoint() Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary.
      abstract String readString(Charset charset) Removes all bytes from this, decodes them as {@code charset}, and returns the string.
      abstract String readString(long byteCount, Charset charset) Removes {@code byteCount} bytes from this, decodes them as {@code charset}, and returns thestring.
      abstract long indexOf(byte b) Equivalent to indexOf(b, 0).
      abstract long indexOf(byte b, long fromIndex) Returns the index of the first {@code b} in the buffer at or after {@code fromIndex}.
      abstract long indexOf(byte b, long fromIndex, long toIndex) Returns the index of {@code b} if it is found in the range of {@code fromIndex} inclusiveto {@code toIndex} exclusive.
      abstract long indexOf(ByteString bytes) Equivalent to indexOf(bytes, 0).
      abstract long indexOf(ByteString bytes, long fromIndex) Returns the index of the first match for {@code bytes} in the buffer at or after {@code * fromIndex}.
      abstract long indexOfElement(ByteString targetBytes) Equivalent to indexOfElement(targetBytes, 0).
      abstract long indexOfElement(ByteString targetBytes, long fromIndex) Returns the first index in this buffer that is at or after {@code fromIndex} and that containsany of the bytes in {@code targetBytes}.
      abstract boolean rangeEquals(long offset, ByteString bytes) Returns true if the bytes at {@code offset} in this source equal {@code bytes}.
      abstract boolean rangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount) Returns true if {@code byteCount} bytes at {@code offset} in this source equal {@code bytes} at {@code bytesOffset}.
      abstract InputStream inputStream() Returns an input stream that reads from this source.
      • Methods inherited from class com.mobwith.httpmodule.Source

        close, read, timeout
      • Methods inherited from class java.io.Closeable

        close
      • Methods inherited from class java.nio.channels.ReadableByteChannel

        read
      • Methods inherited from class java.nio.channels.Channel

        isOpen
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • buffer

         abstract Buffer buffer()

        Returns this source's internal buffer.

      • exhausted

         abstract boolean exhausted()

        Returns true if there are no more bytes in this source. This will block until there are bytesto read or the source is definitely exhausted.

      • require

         abstract void require(long byteCount)

        Returns when the buffer contains at least {@code byteCount} bytes. Throws an java.io.EOFException if the source is exhausted before the required bytes can be read.

      • request

         abstract boolean request(long byteCount)

        Returns true when the buffer contains at least {@code byteCount} bytes, expanding it asnecessary. Returns false if the source is exhausted before the requested bytes can be read.

      • readByte

         abstract byte readByte()

        Removes a byte from this source and returns it.

      • readShort

         abstract short readShort()

        Removes two bytes from this source and returns a big-endian short.

        {@code * * 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()); * }
      • readShortLe

         abstract short readShortLe()

        Removes two bytes from this source and returns a little-endian short.

        {@code * * 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()); * }
      • readInt

         abstract int readInt()

        Removes four bytes from this source and returns a big-endian int.

        {@code * * 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()); * }
      • readIntLe

         abstract int readIntLe()

        Removes four bytes from this source and returns a little-endian int.

        {@code * * 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()); * }
      • readLong

         abstract long readLong()

        Removes eight bytes from this source and returns a big-endian long.

        {@code * * 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()); * }
      • readLongLe

         abstract long readLongLe()

        Removes eight bytes from this source and returns a little-endian long.

        {@code * * 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()); * }
      • readDecimalLong

         abstract long readDecimalLong()

        Reads a long from this source in signed decimal form (i.e., as a string in base 10 withoptional leading '-'). This will iterate until a non-digit character is found.

        {@code * * 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()); * }
      • readHexadecimalUnsignedLong

         abstract long readHexadecimalUnsignedLong()

        Reads a long form this source in hexadecimal form (i.e., as a string in base 16). This williterate until a non-hexadecimal character is found.

        {@code * * 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()); * }
      • skip

         abstract void skip(long byteCount)

        Reads and discards {@code byteCount} bytes from this source. Throws an java.io.EOFException if the source is exhausted before therequested bytes can be skipped.

      • readByteString

         abstract ByteString readByteString()

        Removes all bytes bytes from this and returns them as a byte string.

      • readByteString

         abstract ByteString readByteString(long byteCount)

        Removes {@code byteCount} bytes from this and returns them as a byte string.

      • select

         abstract int select(Options options)

        Finds the first string in {@code options} that is a prefix of this buffer, consumes it fromthis buffer, and returns its index. If no byte string in {@code options} is a prefix of thisbuffer this returns -1 and no bytes are consumed.

        This can be used as an alternative to readByteString or even readUtf8 ifthe set of expected values is known in advance.

        {@code * * 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()); * }
      • readByteArray

         abstract Array<byte> readByteArray()

        Removes all bytes from this and returns them as a byte array.

      • readByteArray

         abstract Array<byte> readByteArray(long byteCount)

        Removes {@code byteCount} bytes from this and returns them as a byte array.

      • read

         abstract int read(Array<byte> sink)

        Removes up to {@code sink.length} bytes from this and copies them into {@code sink}. Returnsthe number of bytes read, or -1 if this source is exhausted.

      • readFully

         abstract void readFully(Array<byte> sink)

        Removes exactly {@code sink.length} bytes from this and copies them into {@code sink}. Throwsan java.io.EOFException if the requested number of bytes cannot be read.

      • read

         abstract int read(Array<byte> sink, int offset, int byteCount)

        Removes up to {@code byteCount} bytes from this and copies them into {@code sink} at {@code * offset}. Returns the number of bytes read, or -1 if this source is exhausted.

      • readFully

         abstract void readFully(Buffer sink, long byteCount)

        Removes exactly {@code byteCount} bytes from this and appends them to {@code sink}. Throws an java.io.EOFException if the requested number of bytes cannot be read.

      • readAll

         abstract long readAll(Sink sink)

        Removes all bytes from this and appends them to {@code sink}. Returns the total number of byteswritten to {@code sink} which will be 0 if this is exhausted.

      • readUtf8

         abstract String readUtf8()

        Removes all bytes from this, decodes them as UTF-8, and returns the string. Returns the emptystring if this source is empty.

        {@code * * 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()); * }
      • readUtf8

         abstract String readUtf8(long byteCount)

        Removes {@code byteCount} bytes from this, decodes them as UTF-8, and returns the string.

        {@code * * 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()); * }
      • readUtf8Line

        @Nullable() abstract String readUtf8Line()

        Removes and returns characters up to but not including the next line break. A line break iseither {@code "\n"} or {@code "\r\n"}; these characters are not included in the result.

        {@code * * 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 . If the source doesn't end with a line break then an implicit linebreak is assumed. Null is returned once the source is exhausted. Use this for human-generateddata, where a trailing line break is optional.

      • readUtf8LineStrict

         abstract String readUtf8LineStrict()

        Removes and returns characters up to but not including the next line break. A line break iseither {@code "\n"} or {@code "\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 java.io.EOFException is thrown. Use this for machine-generated data where a missing line break implies truncatedinput.

      • readUtf8LineStrict

         abstract String readUtf8LineStrict(long limit)

        Like readUtf8LineStrict, except this allows the caller to specify the longestallowed match. Use this to protect against streams that may not include {@code "\n"} or {@code "\r\n"}.

        The returned string will have at most {@code limit} UTF-8 bytes, and the maximum numberof bytes scanned is {@code limit + 2}. If {@code limit == 0} this will always throwan {@code EOFException} because no bytes will be scanned.

        This method is safe. No bytes are discarded if the match fails, and the caller is freeto try another match:

        {@code * * 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)); * }
      • readUtf8CodePoint

         abstract int readUtf8CodePoint()

        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 and consumes no input.

        If this source doesn't start with a properly-encoded UTF-8 code point, this method willremove 1 or more non-UTF-8 bytes and return the replacement character ({@code U+FFFD}). Thiscovers 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) andoverlong encodings (such as {@code 0xc080} for the NUL character in modified UTF-8).

      • readString

         abstract String readString(Charset charset)

        Removes all bytes from this, decodes them as {@code charset}, and returns the string.

      • readString

         abstract String readString(long byteCount, Charset charset)

        Removes {@code byteCount} bytes from this, decodes them as {@code charset}, and returns thestring.

      • indexOf

         abstract long indexOf(byte b, long fromIndex)

        Returns the index of the first {@code b} in the buffer at or after {@code fromIndex}. Thisexpands the buffer as necessary until {@code b} is found. This reads an unbounded number ofbytes into the buffer. Returns -1 if the stream is exhausted before the requested byte isfound.

        {@code * * 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)); * }
      • indexOf

         abstract long indexOf(byte b, long fromIndex, long toIndex)

        Returns the index of {@code b} if it is found in the range of {@code fromIndex} inclusiveto {@code toIndex} exclusive. If {@code b} isn't found, or if {@code fromIndex == toIndex},then -1 is returned.

        The scan terminates at either {@code toIndex} or the end of the buffer, whichever comesfirst. The maximum number of bytes scanned is {@code toIndex-fromIndex}.

      • indexOf

         abstract long indexOf(ByteString bytes, long fromIndex)

        Returns the index of the first match for {@code bytes} in the buffer at or after {@code * fromIndex}. This expands the buffer as necessary until {@code bytes} is found. This reads anunbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before therequested bytes are found.

        {@code * * 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)); * }
      • indexOfElement

         abstract long indexOfElement(ByteString targetBytes, long fromIndex)

        Returns the first index in this buffer that is at or after {@code fromIndex} and that containsany of the bytes in {@code targetBytes}. This expands the buffer as necessary until a targetbyte is found. This reads an unbounded number of bytes into the buffer. Returns -1 if thestream is exhausted before the requested byte is found.

        {@code * * 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'. * }
      • rangeEquals

         abstract boolean rangeEquals(long offset, ByteString bytes)

        Returns true if the bytes at {@code offset} in this source equal {@code bytes}. This expandsthe buffer as necessary until a byte does not match, all bytes are matched, or if the streamis exhausted before enough bytes could determine a match.

        {@code * * 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)); * }
      • rangeEquals

         abstract boolean rangeEquals(long offset, ByteString bytes, int bytesOffset, int byteCount)

        Returns true if {@code byteCount} bytes at {@code offset} in this source equal {@code bytes} at {@code bytesOffset}. This expands the buffer as necessary until a byte does not match, allbytes are matched, or if the stream is exhausted before enough bytes could determine a match.