Stream reading from a String source

This class has the same behavior as BytesReader except for its constructor accepting a String.

var reader = new StringReader("a…b")
assert reader.read_char == 'a'
assert reader.read_byte == 0xE2 # 1st byte of '…'
assert reader.read_byte == 0x80 # 2nd byte of '…'
assert reader.read_char == '�' # Reads the last byte as an invalid char
assert reader.read_all == "b"

Introduced properties

fun source: String

core :: StringReader :: source

Source data to read
protected fun source=(source: String)

core :: StringReader :: source=

Source data to read

Redefined properties

redef type SELF: StringReader

core $ StringReader :: SELF

Type of this instance, automatically specialized in every class
redef fun close

core $ StringReader :: close

close the stream
redef init init

core $ StringReader :: init

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun append_line_to(s: Buffer)

core :: Reader :: append_line_to

Read a string until the end of the line and append it to s.
fun big_endian: Bool

binary :: BinaryStream :: big_endian

Use the big-endian convention? otherwise use little-endian.
fun big_endian=(big_endian: Bool)

binary :: BinaryStream :: big_endian=

Use the big-endian convention? otherwise use little-endian.
fun bytes: Bytes

core :: BytesReader :: bytes

Source data to read
protected fun bytes=(bytes: Bytes)

core :: BytesReader :: bytes=

Source data to read
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
abstract fun close

core :: Stream :: close

close the stream
fun codec: Codec

core :: Stream :: codec

Codec used to transform raw data to text
fun codec=(c: Codec)

core :: Stream :: codec=

Change the codec for this stream.
fun deserialize_msgpack(static_type: nullable String): nullable Object

core :: Reader :: deserialize_msgpack

Deserialize full Nit nullable Object from MessagePack formated data
fun each_line: LineIterator

core :: Reader :: each_line

Return an iterator that read each line.
fun eof: Bool

core :: Reader :: eof

Is there something to read.
fun finish

core :: Stream :: finish

Post-work hook.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun last_error: nullable IOError

core :: Stream :: last_error

Error produced by the file stream
protected fun last_error=(last_error: nullable IOError)

core :: Stream :: last_error=

Error produced by the file stream
protected fun lookahead: CString

core :: Stream :: lookahead

Lookahead buffer for codecs
protected fun lookahead=(lookahead: CString)

core :: Stream :: lookahead=

Lookahead buffer for codecs
protected fun lookahead_capacity: Int

core :: Stream :: lookahead_capacity

Capacity of the lookahead
protected fun lookahead_capacity=(lookahead_capacity: Int)

core :: Stream :: lookahead_capacity=

Capacity of the lookahead
protected fun lookahead_length: Int

core :: Stream :: lookahead_length

Current occupation of the lookahead
protected fun lookahead_length=(lookahead_length: Int)

core :: Stream :: lookahead_length=

Current occupation of the lookahead
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
protected abstract fun raw_read_byte: Int

core :: Reader :: raw_read_byte

Read a byte directly from the underlying stream, without
protected fun raw_read_bytes(buf: CString, max: Int): Int

core :: Reader :: raw_read_bytes

Read at most max bytes from the underlying stream into buf,
fun read(i: Int): String

core :: Reader :: read

Reads a String of at most i length
fun read_all: String

core :: Reader :: read_all

Read all the stream until the eof.
fun read_all_bytes: Bytes

core :: Reader :: read_all_bytes

Read all the stream until the eof.
fun read_bits: Array[Bool]

core :: Reader :: read_bits

Get an Array of 8 Bool by reading a single byte
fun read_block: String

core :: Reader :: read_block

Read the length as a 64 bits integer, then the content of the block
fun read_bool: Bool

core :: Reader :: read_bool

Read a single byte and return true if its value is different than 0
fun read_byte: Int

core :: Reader :: read_byte

Reads a byte. Returns a negative value on error
fun read_bytes(max: Int): Bytes

core :: Reader :: read_bytes

Reads up to max bytes from source
fun read_bytes_to_cstring(bytes: CString, max: Int): Int

core :: Reader :: read_bytes_to_cstring

Reads up to max bytes from source and stores them in bytes
fun read_char: nullable Char

core :: Reader :: read_char

Reads a character. Returns null on EOF or timeout
fun read_double: Float

core :: Reader :: read_double

Read a floating point on 64 bits and return it as a Float
fun read_float: Float

core :: Reader :: read_float

Read a floating point on 32 bits and return it as a Float
fun read_int64: Int

core :: Reader :: read_int64

Read a signed integer on 64 bits and return is an Int
fun read_line: String

core :: Reader :: read_line

Read a string until the end of the line.
fun read_lines: Array[String]

core :: Reader :: read_lines

Read all the lines until the eof.
fun read_msgpack: nullable Serializable

core :: Reader :: read_msgpack

Read the next MessagePack object and return it as a simple Nit object
fun read_nonwhitespace: nullable Char

core :: Reader :: read_nonwhitespace

Skip whitespace characters (if any) then return the following non-whitespace character.
fun read_string: String

core :: Reader :: read_string

Read a null terminated string
fun read_word: String

core :: Reader :: read_word

Read the next sequence of non whitespace characters.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
protected fun set_codec(codec: Codec)

core :: Stream :: set_codec

Codec used to transform raw data to text
fun source: String

core :: StringReader :: source

Source data to read
protected fun source=(source: String)

core :: StringReader :: source=

Source data to read
fun start

core :: Stream :: start

Pre-work hook.
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
protected fun write_buffer: CString

core :: Stream :: write_buffer

Buffer for writing data to a stream
protected fun write_buffer=(write_buffer: CString)

core :: Stream :: write_buffer=

Buffer for writing data to a stream
package_diagram core::StringReader StringReader core::BytesReader BytesReader core::StringReader->core::BytesReader core::Reader Reader core::BytesReader->core::Reader ...core::Reader ... ...core::Reader->core::Reader

Ancestors

abstract class BinaryStream

binary :: BinaryStream

A stream of binary data
interface Object

core :: Object

The root of the class hierarchy.
abstract class Reader

core :: Reader

A Stream that can be read from
abstract class Stream

core :: Stream

Any kind of stream to read/write/both to or from a source

Parents

class BytesReader

core :: BytesReader

Read from bytes in memory

Class definitions

core $ StringReader
# `Stream` reading from a `String` source
#
# This class has the same behavior as `BytesReader`
# except for its constructor accepting a `String`.
#
# ~~~
# var reader = new StringReader("a…b")
# assert reader.read_char == 'a'
# assert reader.read_byte == 0xE2 # 1st byte of '…'
# assert reader.read_byte == 0x80 # 2nd byte of '…'
# assert reader.read_char == '�' # Reads the last byte as an invalid char
# assert reader.read_all == "b"
# ~~~
class StringReader
	super BytesReader

	autoinit source

	# Source data to read
	var source: String

	init do bytes = source.to_bytes

	redef fun close
	do
		source = ""
		super
	end
end
lib/core/stream.nit:742,1--770,3