OutputStream Manual

Table of Contents


An output stream is used to write data to a resource defined with a maxon::Url. maxon::OutputStreamInterface is based on maxon::BaseStreamInterface.


maxon::BaseStreamInterface provides these simple functions:

To append to the end of a file use the flags maxon::OPENSTREAMFLAGS::WRITE_DONT_TRUNCATE and maxon::OPENSTREAMFLAGS::SEEK_TO_END.
// This example writes a maxon::String into a local file.
// construct file URL
const maxon::Url url = (targetFolder + maxon::Url("sometext.txt"_s))iferr_return;
// only proceed if file does not exist yet
if (url.IoDetect() == maxon::IODETECT::NONEXISTENT)
// define text
const maxon::String data { "Hello World!" };
const maxon::BaseArray<maxon::Char> memory = data.GetCString() iferr_return;
// write to file
const maxon::OutputStreamRef stream = url.OpenOutputStream() iferr_return;
stream.Write(memory) iferr_return;
Definition: basearray.h:412
Definition: string.h:1235
Definition: url.h:942
PyObject * stream
Definition: codecs.h:186
Url doesn't exist.
#define iferr_return
Definition: resultbase.h:1465
// This example loads a file from the web and saves it to the local file system.
// check if the given URL references a file on the web
const maxon::UrlScheme scheme = webFile.GetScheme();
const maxon::Bool isHTTP = scheme == maxon::URLSCHEME_HTTP;
const maxon::Bool isHTTPS = scheme == maxon::URLSCHEME_HTTPS;
if (isHTTP || isHTTPS)
// read data
// input stream
const maxon::InputStreamRef inputStream = webFile.OpenInputStream() iferr_return;
const maxon::Int64 length = inputStream.GetStreamLength() iferr_return;
inputStream.Read(data) iferr_return;
inputStream.Close() iferr_return;
// save data to file
// prepare file name
const maxon::Url localFile = (targetFolder + webFile.GetName())iferr_return;
// output stream
const maxon::OutputStreamRef outputStream = localFile.OpenOutputStream() iferr_return;
// write to file
outputStream.Write(data) iferr_return;
outputStream.Close() iferr_return;
Definition: basearray.h:1369
Definition: apibaseid.h:253
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:181
int64_t Int64
64 bit signed integer datatype.
Definition: apibase.h:178
static const Id URLSCHEME_HTTPS
Scheme identifier for Hypertext Transfer Protocol Secure (HTTPS) connections.
Definition: url.h:775
static const Id URLSCHEME_HTTP
Scheme identifier for Hypertext Transfer Protocol (HTTP) connections.
Definition: url.h:769
PyWideStringList Py_ssize_t length
Definition: initconfig.h:448

Further Reading