Qore Programming Language Reference Manual  0.8.12.3
Qore::StreamReader Class Reference

This class defines a stream reader for input streams. More...

Public Member Functions

 constructor (Qore::InputStream is, *string encoding)
 Creates the StreamReader for reading data from the given InputStream. More...
 
string getEncoding ()
 Returns the character encoding for the StreamReader. More...
 
*binary readBinary (int limit=-1)
 Reads binary data from the input stream up to a given limit. More...
 
*string readLine (*string eol, bool trim=True)
 Reads a text line from the input stream. More...
 
int readi1 ()
 Reads a 1-byte signed integer from the input stream. More...
 
int readi2 ()
 Reads a 2-byte (16 bit) signed integer from the input stream in binary big-endian format. More...
 
int readi2LSB ()
 Reads a 2-byte (16 bit) signed integer from the input stream in binary little-endian format. More...
 
int readi4 ()
 Reads a 4-byte (32 bit) signed integer from the input stream in binary big-endian format. More...
 
int readi4LSB ()
 Reads a 4-byte (32 bit) signed integer from the input stream in binary little-endian format. More...
 
int readi8 ()
 Reads a 8-byte (64 bit) signed integer from the input stream in binary big-endian format. More...
 
int readi8LSB ()
 Reads a 8-byte (64 bit) signed integer from the input stream in binary little-endian format. More...
 
int readu1 ()
 Reads a 1-byte unsigned integer from the input stream. More...
 
int readu2 ()
 Reads a 2-byte (16 bit) unsigned integer from the input stream in binary big-endian format. More...
 
int readu2LSB ()
 Reads a 2-byte (16 bit) unsigned integer from the input stream in binary little-endian format. More...
 
int readu4 ()
 Reads a 4-byte (32 bit) unsigned integer from the input stream in binary big-endian format. More...
 
int readu4LSB ()
 Reads a 4-byte (32 bit) unsigned integer from the input stream in binary little-endian format. More...
 

Detailed Description

This class defines a stream reader for input streams.

Since
Qore 0.8.13
Example: StreamReader basic usage
1 FileInputStream is("data.txt");
2 StreamReader sr(is, "UTF-8");
3 
4 *string line = sr.readLine();
5 *binary b = sr.readBinary(256);
6 int i = sr.readi4();
See also
Qore::InputStream

Member Function Documentation

Qore::StreamReader::constructor ( Qore::InputStream  is,
*string  encoding 
)

Creates the StreamReader for reading data from the given InputStream.

Parameters
isthe InputStream for reading data
encodingcharacter encoding of the data from the InputStream; the encoding has to be ASCII-compatible, otherwise an exception will be thrown; if not present, the default character encoding is assumed
Exceptions
UNSUPPORTED-ENCODING-ERRORASCII-incompatible encoding has been passed
string Qore::StreamReader::getEncoding ( )

Returns the character encoding for the StreamReader.

Code Flags:
CONSTANT
Returns
the character encoding for the StreamReader
*binary Qore::StreamReader::readBinary ( int  limit = -1)

Reads binary data from the input stream up to a given limit.

Example:
1 *binary b = sr.readBinary(16);
Parameters
limitmaximum amount of binary data to read; if not given or equal to -1, all the data from the InputStream will be read; if equal to 0, NOTHING will be read
Returns
a Binary value or NOTHING if there is no more data available in the stream
int Qore::StreamReader::readi1 ( )

Reads a 1-byte signed integer from the input stream.

Example:
1 int i = sr.readi1();
Returns
a 1-byte signed integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readi2 ( )

Reads a 2-byte (16 bit) signed integer from the input stream in binary big-endian format.

Example:
1 int i = sr.readi2();
Returns
a 2-byte signed integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readi2LSB ( )

Reads a 2-byte (16 bit) signed integer from the input stream in binary little-endian format.

Example:
1 int i = sr.readi2LSB();
Returns
a 2-byte signed integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readi4 ( )

Reads a 4-byte (32 bit) signed integer from the input stream in binary big-endian format.

Example:
1 int i = sr.readi4();
Returns
a 4-byte signed integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readi4LSB ( )

Reads a 4-byte (32 bit) signed integer from the input stream in binary little-endian format.

Example:
1 int i = sr.readi4LSB();
Returns
a 4-byte signed integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readi8 ( )

Reads a 8-byte (64 bit) signed integer from the input stream in binary big-endian format.

Example:
1 int i = sr.readi8();
Returns
a 8-byte signed integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readi8LSB ( )

Reads a 8-byte (64 bit) signed integer from the input stream in binary little-endian format.

Example:
1 int i = sr.readi8LSB();
Returns
a 8-byte signed integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
*string Qore::StreamReader::readLine ( *string  eol,
bool  trim = True 
)

Reads a text line from the input stream.

Example:
1 string line = sr.readLine("\n");
Parameters
eolthe optional end of line character(s) to use to detect lines in the data; if this string is not passed, then the end of line character(s) are detected automatically, and can be either "\n", "\r", or "\r\n"
trimif True the returned lines will be trimmed of the eol bytes
Returns
a text line read from the stream or NOTHING if there is no more data available in the stream
Exceptions
ENCODING-CONVERSION-ERRORthis exception could be thrown if the eol argument has a different character encoding from the data's and an error occurs during encoding conversion
int Qore::StreamReader::readu1 ( )

Reads a 1-byte unsigned integer from the input stream.

Example:
1 int i = sr.readu1();
Returns
a 1-byte unsigned integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readu2 ( )

Reads a 2-byte (16 bit) unsigned integer from the input stream in binary big-endian format.

Example:
1 int i = sr.readu2();
Returns
a 2-byte unsigned integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readu2LSB ( )

Reads a 2-byte (16 bit) unsigned integer from the input stream in binary little-endian format.

Example:
1 int i = sr.readu2LSB();
Returns
a 2-byte unsigned integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readu4 ( )

Reads a 4-byte (32 bit) unsigned integer from the input stream in binary big-endian format.

Example:
1 int i = sr.readu4();
Returns
a 4-byte unsigned integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream
int Qore::StreamReader::readu4LSB ( )

Reads a 4-byte (32 bit) unsigned integer from the input stream in binary little-endian format.

Example:
1 int i = sr.readu4LSB();
Returns
a 4-byte unsigned integer
Exceptions
END-OF-STREAM-ERRORthere is not enough data available in the stream