Class BufferedReader

extends Object implements Reader
A reader wrapper that buffers the content offered by a reader.
Deprecated. Use io.Reader instead.

Class summary


Constructors

Methods

Whether n bytes are available in the internal buffer.
This function will not call read on the underlying reader, so it only tells you the bytes that can be read without a read operation that might block.

buffer-all -> none
Reads and buffers until the end of the reader.

Amount of buffered data.
This function will not call read on the underlying reader, so it only tells you the bytes that can be read without a read operation that might block.

byte n/any -> int
Reads the n'th byte from the current position.
This operation does not consume any bytes. Use skip or read to advance this reader.
Errors
At least n + 1 bytes must be available.

bytes n/any -> ByteArray
Reads the first n bytes from the reader.
This operation does not consume any bytes. Use skip or read to advance this reader.
Errors
At least n bytes must be available.

Whether n bytes can be ensured (see ensure).
Tries to buffer n bytes, and returns whether it was able to.

clear -> none
Clears any buffered data.

The number of bytes that have been consumed from the BufferedReader.

ensure n/int -> none
Ensures that at least n bytes are available.
Errors
At least n bytes must be available in the underlying reader. Use can-ensure if a non-throwing version is necessary.

index-of byte/any -> int
Searches forwards for the byte.
Consumes no bytes.
Returns the index of the first occurrence of the byte.
Returns null otherwise.

index-of byte/any --to/int -> int
Searches forwards for the byte.
Consumes no bytes.
Returns the index of the first occurrence of the byte.
Returns -1 otherwise.

index-of-or-throw byte/any -> any
Searches forwards for the index of the byte.
Consumes no bytes.
Returns the index of the first occurrence of the byte.
Throws if the byte is not found.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

Peeks the first n bytes and converts them to a string.
Errors
The peeked bytes must be convertible to a UTF8 string.
At least n bytes must be available.
Examples

class MyReader implements Reader:
  read -> ByteArray?: return "hellø".to_byte_array

main:
  reader := BufferedReader MyReader
  print
    reader.peek_string 6  // >> Hellø
  print
    reader.peek_string 5  // >> Error!

read --max-size/int=null -> ByteArray
Reads from the reader.
The read bytes are consumed.
If max-size is specified the returned byte array will never be larger than that size, but it may be smaller, even if there is more data available from the underlying reader. Use read-bytes to read exactly n bytes.
Returns null if the reader is at the end.

Reads the first byte.
The read byte is consumed.
Errors
At least 1 byte must be available.

Reads the first n bytes from the reader.
The read bytes are consumed.
At least n bytes must be available. That is, a call to can-ensure n must return true.
If you want to read either n bytes, if they are available, or the maximum number of available bytes otherwise, use the following code:

read_exactly_or_drain reader/BufferedReader n/int -> ByteArray?:
  if can_ensure n: return reader.read_bytes n
  reader.buffer_all
  if reader.buffered == 0: return null
  return reader.read_bytes reader.buffered

read-bytes-until delimiter/any -> ByteArray
Reads the bytes before the delimiter.
The read bytes and the delimiter are consumed.
Errors
The delimiter must be available.

read-line keep-newlines/any=false -> string
Reads a line as a string.
Lines are terminated by a newline character ('\n') except for the final line.
Carriage returns ('\r') are removed from lines terminated by '\r\n'.

Reads the first n bytes as a string.
The read bytes are consumed.
Errors
The read bytes must be convertible to a UTF8 string.
At least n bytes must be available.
Examples

class MyReader implements Reader:
  read -> ByteArray?: return "hellø".to_byte_array

main:
  reader := BufferedReader MyReader
  print
    reader.read_string 6  // >> Hellø
  print
    reader.read_string 5  // >> Error!

read-string --max-size/int=null -> string
Reads at most max-size bytes as a string.
The read bytes are consumed.
Note that this method is different from read followed by to_string as it ensures that the data is split into valid UTF-8 chunks.
If max-size is specified the returned string will never be larger than that size (in bytes), but it may be smaller, even if there is more data available from the underlying reader.
Returns null if the stream has ended.
Errors
The read bytes must be convertible to a legal UTF8 string, but this method will read a number of bytes such that legal UTF-8 characters are not chopped up.
May throw an end-of-stream exception if the stream ends in the middle of a malformed UTF-8 character.
Instead of returning a zero length string it throws an exception. This can happen if max-size is less than 4 bytes and the next thing is a UTF-8 character that is coded in more bytes than were requested. This also means max-size should never be zero.

read-until delimiter/any -> string
Reads the string before the delimiter.
The read bytes and the delimiter are consumed.
Errors
The delimiter must be available.

Reads up to the max-size amount of bytes from the reader.
The read bytes are consumed.
Errors
At least 1 byte must be available.
Deprecated. Use read --max-size instead.

Deprecated.

skip n/any -> none
Skips n bytes.
Errors
At least n bytes must be available.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

unget value/ByteArray -> none
The bytes in value are prepended to the BufferedReader.
These will be the first bytes to be read in subsequent read operations. This takes ownership of value so it is kept alive and its contents should not be modified after being given to the BufferedReader.
This causes the consumed count to go backwards.