Qore::StringOutputStream Class Reference

This class implements the OutputStream interface for writing bytes to a String buffer. More...

Inheritance diagram for Qore::StringOutputStream:
[legend]

## Public Member Functions

nothing | close ()
| Closes the output stream and releases any resources. More...
| constructor ()
| Creates the StringOutputStream. More...
| constructor (string encoding)
| Creates the StringOutputStream. More...
string | getData ()
| Returns the data written to the stream, clearing the internal buffer. More...
string | getEncoding ()
| Returns the character encoding for the StringOutputStream. More...
bool | hasData ()
| Returns True if the stream already has data. More...
nothing | write (binary data)
| Writes bytes to the output stream. More...
Public Member Functions inherited from Qore::OutputStream
| constructor ()
| Constructor. More...
Public Member Functions inherited from Qore::StreamBase
| constructor ()
| Throws an exception if called directly; this class can only be instantiated by builtin subclasses. More...
int | getThreadId ()
| Get the currently assigned thread id or -1 if not assigned to any thread.
nothing | reassignThread ()
| Reassigns current thread as thread used for stream manipulation. More...
nothing | unassignThread ()
| Unassigns current thread as thread used for stream manipulation. More...

## Detailed Description

This class implements the OutputStream interface for writing bytes to a String buffer.

Example: StringOutputStream basic usage
  StringOutputStream sos();
  sos.write(<41>);
  sos.write(<42>);
  sos.write(<43>);
  sos.getData(); # returns "ABC"

Note
  stream classes are not designed to be accessed from multiple threads; they have been implemented without locking for fast and efficient use when used from a single thread. For methods that would be unsafe to use in another thread, any use of such methods in threads other than the thread where the constructor was called will cause a `STREAM-THREAD-ERROR` to be thrown, unless the stream is handed off to another thread using the StreamBase::unassignThread() method in the thread that currently owns the stream, and the StreamBase::reassignThread() method in the new thread.

See also
  StreamWriter for a class that can be used to write various kinds of data to an OutputStream

Since
  Qore 0.8.13

## Member Function Documentation

## close()

nothing Qore::StringOutputStream::close | ( |  | ) |

virtual

Closes the output stream and releases any resources.

Any methods called on a closed output stream will throw an exception.

Exceptions

  OUTPUT-STREAM-CLOSED-ERROR | the output stream has already been closed
  STREAM-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object

Implements Qore::OutputStream.

## constructor() [1/2]

Qore::StringOutputStream::constructor | ( |  | ) |

Creates the StringOutputStream.

This variant assumes the Default Character Encoding for the string

Note
  raw binary data is written to the output stream, so it's up to the caller to ensure that only valid character data for the given encoding is written to the StringOutputStream object, otherwise a string with invalid character data will be returned by getData()

## constructor() [2/2]

Qore::StringOutputStream::constructor | ( | string | encoding | ) |

Creates the StringOutputStream.

Parameters

  encoding | the encoding to tag the string data with

Note
  raw binary data is written to the output stream, so it's up to the caller to ensure that only valid character data for the given encoding is written to the StringOutputStream object, otherwise a string with invalid character data will be returned by getData()

## getData()

string Qore::StringOutputStream::getData | ( |  | ) |

Returns the data written to the stream, clearing the internal buffer.

Returns
  the data written to the StringOutputStream

Example:
  StringOutputStream sos();
  sos.write(<41>);
  sos.write(<42>);
  sos.write(<43>);
  sos.getData(); # returns "ABC"

Exceptions

  OUTPUT-STREAM-CLOSED-ERROR | the output stream has already been closed
  STREAM-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object

## getEncoding()

string Qore::StringOutputStream::getEncoding | ( |  | ) |

Returns the character encoding for the StringOutputStream.

Code Flags:
  CONSTANT

Example:
  StringOutputStream sos("latin1");
  string encoding = sos.getEncoding();

Returns
  the character encoding for the StringOutputStream

Note
  raw binary data is written to the output stream, so it's up to the caller to ensure that only valid character data for the given encoding is written to the StringOutputStream object, otherwise a string with invalid character data will be returned by getData()

## hasData()

bool Qore::StringOutputStream::hasData | ( |  | ) |

Returns True if the stream already has data.

Returns
  True if the stream already has data

Code Flags:
  RET_VALUE_ONLY

Example:
  bool has_data = sos.hasData();

Exceptions

  OUTPUT-STREAM-CLOSED-ERROR | the output stream has already been closed
  STREAM-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object

Since
  Qore 1.1

## write()

nothing Qore::StringOutputStream::write | ( | binary | data | ) |

virtual

Writes bytes to the output stream.

Parameters

  data | the data to write

Example:
  StringOutputStream sos();
  sos.write(<41>);
  sos.write(<42>);
  sos.write(<43>);
  sos.getData(); # returns "ABC"

Exceptions

  OUTPUT-STREAM-CLOSED-ERROR | the output stream has already been closed
  STREAM-THREAD-ERROR | this exception is thrown if this method is called from any thread other than the thread that created the object

Implements Qore::OutputStream.