InputStream Manual
An input stream is used to read data from a resource defined with a maxon::Url . maxon::InputStreamInterface is based on maxon::BaseStreamInterface .
maxon::InputStreamInterface provides these functions:
// 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 ;
// 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 ;
// input stream const maxon::InputStreamRef inputStream = webFile.OpenInputStream() iferr_return ; const maxon::Int length = inputStream.GetStreamLength() iferr_return ; maxon::BaseArray<maxon::Char> data; 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 ; maxon::BaseArray<maxon::Char> data; data. Resize (length) iferr_return ; inputStream.Read(data) iferr_return ;
// convert to string const maxon::String text { data }; DiagnosticOutput ( "Stream Content: @" , text);