Stream that can read from a File

Introduced properties

init from_fd(fd: Int)

core :: FileReader :: from_fd

Creates a new File stream from a file descriptor
init open(path: String)

core :: FileReader :: open

Open the file at path for reading.
fun reopen

core :: FileReader :: reopen

Open the same file again.

Redefined properties

redef type SELF: FileReader

core $ FileReader :: SELF

Type of this instance, automatically specialized in every class
redef fun eof: Bool

core $ FileReader :: eof

Is there something to read.
redef fun poll_in: Bool

core $ FileReader :: poll_in

Is there something to read? (without blocking)
redef fun raw_read_byte: Int

core $ FileReader :: raw_read_byte

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

core $ FileReader :: raw_read_bytes

Read at most max bytes from the underlying stream into buf,

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.
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 fd: Int

core :: FileStream :: fd

File descriptor of this file
fun file_stat: nullable FileStat

core :: FileStream :: file_stat

The status of a file. see POSIX stat(2).
fun finish

core :: Stream :: finish

Post-work hook.
init from_fd(fd: Int)

core :: FileReader :: from_fd

Creates a new File stream from a file descriptor
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.
init open(path: String)

core :: FileReader :: open

Open the file at path for reading.
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).
fun path: nullable String

core :: FileStream :: path

The path of the file.
protected fun path=(path: nullable String)

core :: FileStream :: path=

The path of the file.
abstract fun poll_in: Bool

core :: PollableReader :: poll_in

Is there something to read? (without blocking)
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 reopen

core :: FileReader :: reopen

Open the same file again.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun set_buffering_mode(buf_size: Int, mode: Int)

core :: FileStream :: set_buffering_mode

Sets the buffering mode for the current FileStream
protected fun set_codec(codec: Codec)

core :: Stream :: set_codec

Codec used to transform raw data to text
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::FileReader FileReader core::FileStream FileStream core::FileReader->core::FileStream core::PollableReader PollableReader core::FileReader->core::PollableReader core::Stream Stream core::FileStream->core::Stream core::Reader Reader core::PollableReader->core::Reader ...core::Stream ... ...core::Stream->core::Stream ...core::Reader ... ...core::Reader->core::Reader core::Stdin Stdin core::Stdin->core::FileReader

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

abstract class FileStream

core :: FileStream

Stream used to interact with a File or FileDescriptor
abstract class PollableReader

core :: PollableReader

Reader capable of declaring if readable without blocking

Children

class Stdin

core :: Stdin

Standard input stream.

Class definitions

core $ FileReader
# `Stream` that can read from a File
class FileReader
	super FileStream
	super PollableReader
	# Misc

	# Open the same file again.
	# The original path is reused, therefore the reopened file can be a different file.
	#
	#     var f = new FileReader.open("/etc/issue")
	#     var l = f.read_line
	#     f.reopen
	#     assert l == f.read_line
	fun reopen
	do
		var fl = _file
		if fl != null and not fl.address_is_null then close
		last_error = null
		_file = new NativeFile.io_open_read(path.as(not null).to_cstring)
		if _file.as(not null).address_is_null then
			last_error = new IOError("Cannot open `{path.as(not null)}`: {sys.errno.strerror}")
			return
		end
	end

	redef fun raw_read_byte
	do
		var nb = _file.as(not null).io_read(write_buffer, 1)
		if last_error == null and _file.as(not null).ferror then
			last_error = new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
		end
		if nb == 0 then return -1
		return write_buffer[0].to_i
	end

	redef fun raw_read_bytes(cstr, max)
	do
		var nb = _file.as(not null).io_read(cstr, max)
		if last_error == null and _file.as(not null).ferror then
			last_error = new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
		end
		return nb
	end

	redef fun eof do
		var fl = _file
		if fl == null then return true
		if fl.address_is_null then return true
		if last_error != null then return true
		if super then
			if last_error != null then return true
			return fl.feof
		end
		return false
	end

	# Open the file at `path` for reading.
	#
	#     var f = new FileReader.open("/etc/issue")
	#     assert not f.eof
	#     f.close
	#
	# In case of error, `last_error` is set
	#
	#     f = new FileReader.open("/fail/does not/exist")
	#     assert f.eof
	#     assert f.last_error != null
	init open(path: String)
	do
		self.path = path
		_file = new NativeFile.io_open_read(path.to_cstring)
		if _file.as(not null).address_is_null then
			last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
		end
	end

	# Creates a new File stream from a file descriptor
	#
	# This is a low-level method.
	init from_fd(fd: Int) do
		self.path = ""
		_file = fd.fd_to_stream(read_only)
		if _file.as(not null).address_is_null then
			last_error = new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
		end
	end

	redef fun poll_in
	do
		var res = native_poll_in(fd)
		if res == -1 then
			last_error = new IOError(errno.to_s)
			return false
		else return res > 0
	end

	private fun native_poll_in(fd: Int): Int `{
#ifndef _WIN32
		struct pollfd fds = {(int)fd, POLLIN, 0};
		return poll(&fds, 1, 0);
#else
		return 0;
#endif
	`}
end
lib/core/file.nit:101,1--205,3