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;
data.Resize(length) iferr_return;
inputStream.Read(data) iferr_return;
// convert to string
const maxon::String text { data };
DiagnosticOutput("File Content: @", text);
}
// 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::Int length = inputStream.GetStreamLength() iferr_return;
data.Resize(length) 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;
}

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;
data.Resize(length) iferr_return;
inputStream.Read(data) iferr_return;
// convert to string
const maxon::String text { data };
DiagnosticOutput("Stream Content: @", text);

Further Reading

maxon::String
Definition: string.h:1197
maxon::BaseArray::Resize
ResultMem Resize(Int newCnt, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::DEFAULT)
Definition: basearray.h:1077
maxon::Bool
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:172
maxon::Id
Definition: apibaseid.h:273
iferr_return
#define iferr_return
Definition: resultbase.h:1434
maxon::URLSCHEME_HTTPS
static const Id URLSCHEME_HTTPS
Definition: url.h:663
maxon::BaseArray
Definition: basearray.h:366
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:168
maxon::Url
Definition: url.h:819
maxon::URLSCHEME_HTTP
static const Id URLSCHEME_HTTP
Definition: url.h:658
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:179
maxon::IODETECT::FILE
@ FILE
Url is a file.
maxon::CharToBlock
Block< const Char > CharToBlock(const Char *str)
Definition: block.h:893