Qore Programming Language Reference Manual  0.9.16
Qore::StreamWriter Class Reference

This class defines a stream writer for output streams. More...

Public Member Methods

 constructor (Qore::OutputStream os, *string encoding)
 Creates the StreamWriter for writing data to the given OutputStream. More...
 
nothing f_printf (string fmt,...)
 Writes a formatted string with hard field widths to an output stream; string data is converted to the StreamWriter's character encoding if necessary before writing. More...
 
nothing f_vprintf (string fmt, any fmt_args)
 Writes a formatted string with hard field widths to an output stream, where the second argument is the formatting argument list; string data is converted to the StreamWriter's character encoding if necessary before writing. More...
 
string getEncoding ()
 Returns the character encoding for the StreamWriter. More...
 
OutputStream getOutputStream ()
 Returns the OutputStream for the StreamWriter. More...
 
nothing print (string str)
 Writes string data to an output stream; string data is converted to the StreamWriter's character encoding if necessary before writing. More...
 
nothing printf (string fmt,...)
 Writes a formatted string with soft field widths to an output stream; string data is converted to the StreamWriter's character encoding if necessary before writing. More...
 
nothing vprintf (string fmt, any fmt_args)
 Writes a formatted string with soft field widths to an output stream, where the second argument is the formatting argument list; string data is converted to the StreamWriter's character encoding if necessary before writing. More...
 
nothing write (string str)
 Writes a String to the output stream. More...
 
nothing write (binary b)
 Writes a Binary to the output stream. More...
 
nothing writei1 (int c)
 Writes a 1-byte signed integer to the output stream. More...
 
nothing writei2 (int s)
 Writes a 2-byte (16 bit) integer to the output stream in binary big-endian format. More...
 
nothing writei2LSB (int s)
 Writes a 2-byte (16 bit) integer to the output stream in binary little-endian format. More...
 
nothing writei4 (int i)
 Writes a 4-byte (32 bit) integer to the output stream in binary big-endian format. More...
 
nothing writei4LSB (int i)
 Writes a 4-byte (32 bit) integer to the output stream in binary little-endian format. More...
 
nothing writei8 (int i)
 Writes an 8-byte (64 bit) integer to the output stream in binary big-endian format. More...
 
nothing writei8LSB (int i)
 Writes an 8-byte (64 bit) integer to the output stream in binary little-endian format. More...
 

Detailed Description

This class defines a stream writer for output streams.

Since
Qore 0.8.13
Example: StreamWriter basic usage
FileOutputStream os("log.txt");
StreamWriter sw(os, "UTF-8");
sw.print("Lorem ipsum.\n");
sw.printf("%s: %d\n", "The answer", 42);
sw.print("32-bit int values:\n");
sw.writei4(1337);
sw.writei4(0xf4f4);
See also
OutputStream

Member Function Documentation

◆ constructor()

Qore::StreamWriter::constructor ( Qore::OutputStream  os,
*string  encoding 
)

Creates the StreamWriter for writing data to the given OutputStream.

Parameters
osthe OutputStream for writing data
encodingcharacter encoding of the StreamWriter; all passed string data will be converted to this encoding before being written to the output stream; if not present, the default character encoding is assumed

◆ f_printf()

nothing Qore::StreamWriter::f_printf ( string  fmt,
  ... 
)

Writes a formatted string with hard field widths to an output stream; string data is converted to the StreamWriter's character encoding if necessary before writing.

This method does not allow arguments to overrun field width specifiers in the format string.

Example:
sw.f_printf("%5s\n", "hello there"); # outputs "hello\n", enforcing field width
Parameters
fmtthe format string; see String Formatting for more information about the format string
Exceptions
ENCODING-CONVERSION-ERRORerror converting from the string's character encoding to the StreamWriter's character encoding
See also
StreamWriter::printf() for a similar method that does not enforce field widths

◆ f_vprintf()

nothing Qore::StreamWriter::f_vprintf ( string  fmt,
any  fmt_args 
)

Writes a formatted string with hard field widths to an output stream, where the second argument is the formatting argument list; string data is converted to the StreamWriter's character encoding if necessary before writing.

This method does not allow arguments to overrun field width specifiers in the format string.

Example:
sw.f_vprintf("%5s: %d\n", ("hello there", 2)); # outputs "hello: 2\n", enforcing field width
Parameters
fmtthe format string; see String Formatting for more information about the format string
fmt_argsthe single argument or list of arguments that will be used as the argument list for the format string. If a single argument is passed instead of a list, it will be used as the first argument as if a list were passed
Exceptions
ENCODING-CONVERSION-ERRORerror converting from the string's character encoding to the StreamWriter's character encoding
See also
StreamWriter::printf() for a similar method that does not enforce field widths

◆ getEncoding()

string Qore::StreamWriter::getEncoding ( )

Returns the character encoding for the StreamWriter.

Code Flags:
CONSTANT

Returns the character encoding for the StreamWriter.

Returns
the character encoding for the StreamWriter

◆ getOutputStream()

OutputStream Qore::StreamWriter::getOutputStream ( )

Returns the OutputStream for the StreamWriter.

Returns the OutputStream for the StreamWriter.

Returns
the OutputStream for the StreamWriter
Since
0.9.0

◆ print()

nothing Qore::StreamWriter::print ( string  str)

Writes string data to an output stream; string data is converted to the StreamWriter's character encoding if necessary before writing.

Example:
sw.print(str);
Parameters
strthe string to be written to the output stream; string data is converted to the StreamWriter's character encoding if necessary before writing
Exceptions
ENCODING-CONVERSION-ERRORerror converting from the string's character encoding to the StreamWriter's character encoding
Note
equivalent to write(string)

◆ printf()

nothing Qore::StreamWriter::printf ( string  fmt,
  ... 
)

Writes a formatted string with soft field widths to an output stream; string data is converted to the StreamWriter's character encoding if necessary before writing.

This method will allow arguments to overrun field width specifiers in the format string.

Example:
sw.printf("%5s\n", "hello there"); # outputs "hello there\n", exceeding field width
Parameters
fmtthe format string; see String Formatting for more information about the format string
Exceptions
ENCODING-CONVERSION-ERRORerror converting from the string's character encoding to the StreamWriter's character encoding
See also
StreamWriter::f_printf() for a similar method that enforces field widths

◆ vprintf()

nothing Qore::StreamWriter::vprintf ( string  fmt,
any  fmt_args 
)

Writes a formatted string with soft field widths to an output stream, where the second argument is the formatting argument list; string data is converted to the StreamWriter's character encoding if necessary before writing.

This method will allow arguments to overrun field width specifiers in the format string.

Example:
sw.vprintf("%5s: %d\n", ("hello there", 2)); # outputs "hello there: 2\n", exceeding field width
Parameters
fmtthe format string; see String Formatting for more information about the format string
fmt_argsthe single argument or list of arguments that will be used as the argument list for the format string. If a single argument is passed instead of a list, it will be used as the first argument as if a list were passed
Exceptions
ENCODING-CONVERSION-ERRORerror converting from the string's character encoding to the StreamWriter's character encoding
See also
StreamWriter::f_vprintf() for a similar method that enforces field widths

◆ write() [1/2]

nothing Qore::StreamWriter::write ( binary  b)

Writes a Binary to the output stream.

Example:
sw.write(<010203>);
Parameters
bthe binary to write

◆ write() [2/2]

nothing Qore::StreamWriter::write ( string  str)

Writes a String to the output stream.

Example:
sw.write("string");
Parameters
strthe string to write
Note
equivalent to print(string)

◆ writei1()

nothing Qore::StreamWriter::writei1 ( int  c)

Writes a 1-byte signed integer to the output stream.

Example:
sw.writei1(val);
Parameters
cthe integer to write; only the least-significant 8 bits will be written to the file

◆ writei2()

nothing Qore::StreamWriter::writei2 ( int  s)

Writes a 2-byte (16 bit) integer to the output stream in binary big-endian format.

Example:
sw.writei2(val);
Parameters
sthe integer to write in binary big-endian format; only the least-significant 16 bits of the integer will be written to the file

◆ writei2LSB()

nothing Qore::StreamWriter::writei2LSB ( int  s)

Writes a 2-byte (16 bit) integer to the output stream in binary little-endian format.

Example:
sw.writei2LSB(val);
Parameters
sthe integer to write in binary little-endian format; only the least-significant 16 bits of the integer will be written to the file

◆ writei4()

nothing Qore::StreamWriter::writei4 ( int  i)

Writes a 4-byte (32 bit) integer to the output stream in binary big-endian format.

Example:
sw.writei4(val);
Parameters
ithe integer to write in binary big-endian format; only the least-significant 32 bits of the integer will be written to the file

◆ writei4LSB()

nothing Qore::StreamWriter::writei4LSB ( int  i)

Writes a 4-byte (32 bit) integer to the output stream in binary little-endian format.

Example:
sw.writei4LSB(val);
Parameters
ithe integer to write in binary little-endian format; only the least-significant 32 bits of the integer will be written to the file

◆ writei8()

nothing Qore::StreamWriter::writei8 ( int  i)

Writes an 8-byte (64 bit) integer to the output stream in binary big-endian format.

Example:
sw.writei8(val);
Parameters
ithe integer to write in binary big-endian format; only the least-significant 64 bits of the integer will be written to the file

◆ writei8LSB()

nothing Qore::StreamWriter::writei8LSB ( int  i)

Writes an 8-byte (64 bit) integer to the output stream in binary little-endian format.

Example:
sw.writei8LSB(val);
Parameters
ithe integer to write in binary little-endian format; only the least-significant 64 bits of the integer will be written to the file