| Qore Programming Language Reference Manual 2.2.0

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Qore::StreamWriter Class Reference

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

`#include <QC_StreamWriter.dox.h>`

## Public Member Functions
| constructor (Qore::OutputStream os, *string encoding)
| Creates the StreamWriter for writing data to the given OutputStream.
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.
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.
string | getEncoding ()
| Returns the character encoding for the StreamWriter.
OutputStream | getOutputStream ()
| Returns the OutputStream for the StreamWriter.
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.
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.
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.
nothing | write (string str)
| Writes a String to the output stream.
nothing | write (binary b)
| Writes a Binary to the output stream.
nothing | writei1 (int c)
| Writes a 1-byte signed integer to the output stream.
nothing | writei2 (int s)
| Writes a 2-byte (16 bit) integer to the output stream in binary big-endian format.
nothing | writei2LSB (int s)
| Writes a 2-byte (16 bit) integer to the output stream in binary little-endian format.
nothing | writei4 (int i)
| Writes a 4-byte (32 bit) integer to the output stream in binary big-endian format.
nothing | writei4LSB (int i)
| Writes a 4-byte (32 bit) integer to the output stream in binary little-endian format.
nothing | writei8 (int i)
| Writes an 8-byte (64 bit) integer to the output stream in binary big-endian format.
nothing | writei8LSB (int i)
| Writes an 8-byte (64 bit) integer to the output stream in binary little-endian format.

## 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

  os | the OutputStream for writing data
  encoding | character 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

  fmt | the format string; see String Formatting for more information about the format string

Exceptions

  ENCODING-CONVERSION-ERROR | error 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

  fmt | the format string; see String Formatting for more information about the format string
  fmt_args | the 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-ERROR | error 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

  str | the 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-ERROR | error 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

  fmt | the format string; see String Formatting for more information about the format string

Exceptions

  ENCODING-CONVERSION-ERROR | error 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

  fmt | the format string; see String Formatting for more information about the format string
  fmt_args | the 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-ERROR | error 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

  b | the binary to write

## ◆ write() [2/2]

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

Writes a String to the output stream.

Example:
  sw.write("string");

Parameters

  str | the 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

  c | the 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

  s | the 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

  s | the 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

  i | the 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

  i | the 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

  i | the 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

  i | the integer to write in binary little-endian format; only the least-significant 64 bits of the integer will be written to the file