InputStream Manual

Table of Contents

About

An input stream is used to read data from a resource defined with a maxon::Url. maxon::InputStreamInterface is based on maxon::BaseStreamInterface.

Usage

maxon::InputStreamInterface provides these functions:

// This example reads a text file and loads the data into a maxon::String.
// construct file URL
const maxon::Url url = (targetFolder + maxon::Url("textfile.txt"_s))iferr_return;
// check if file exists
if (url.IoDetect() == maxon::IODETECT::FILE)
{
// open input stream and get file size
const maxon::InputStreamRef inputStream = url.OpenInputStream() iferr_return;
const maxon::Int length = inputStream.GetStreamLength() iferr_return;
inputStream.Read(data) iferr_return;
// convert to string
const maxon::String text { data };
DiagnosticOutput("File Content: @", text);
}
Definition: basearray.h:412
ResultMem Resize(Int newCnt, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::DEFAULT)
Definition: basearray.h:1369
Definition: string.h:1235
Definition: url.h:952
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:188
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
PyWideStringList Py_ssize_t length
Definition: initconfig.h:448
@ FILE
Url is a file.
PyObject * text
Definition: pycore_traceback.h:70
#define iferr_return
Definition: resultbase.h:1519
// 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: 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

The input stream maxon::InputStreamInterface::FromBlock can be used to read data from a memory block.

// This example shows how to access data from a memory block using an InputStreamRef.
const auto memblock = maxon::CharToBlock("Hello World");
const maxon::InputStreamRef inputStream = maxon::InputStreamInterface::FromBlock().Create(memblock, false) iferr_return;
// read into a maxon::String
// prepare memory
const maxon::Int length = inputStream.GetStreamLength() iferr_return;
inputStream.Read(data) iferr_return;
// convert to string
const maxon::String text { data };
DiagnosticOutput("Stream Content: @", text);
Block< const Char > CharToBlock(const Char *str)
Definition: block.h:1016

Further Reading