First of all, I clearly know what node.js stream is.
Node.js stream is used when I need a way to serve data in chunks, piece by piece, to the memory, instead of loading the entire content of a file into the memory and crashing the process. Node.js stream works like a flowing stream.
Then later, I came across the term "byte stream", and intuitively thought that it is also this "flowing" stream concept where data is served chunk by chunk.
Please correct me if i'm wrong, but I byte stream
is actually not "flowing" at all, is it?
When I access a byte stream
, all the data is loaded into the memory at once, right?
I am using this dicom-parser library in nodejs, which reads a dicom file and extract the dicom tag information. I am able to use it like this:
const filedata = fs.readFileSync(fileFullPath); // todo: will load entire file into memory, not good for large files;
const byteArray = new Uint8Array(filedata);
const dicomDataSet = dicomParser.parseDicom(byteArray);
but this is loading entire file in memory and might crash my app. I need to load the file in a "streaming" fashion. Diving into the dicom-parser
library's source code:
/**
* Parses a DICOM P10 byte array and returns a DataSet object with the parsed elements.
* If the options argument is supplied and it contains the untilTag property, parsing
* will stop once that tag is encoutered. This can be used to parse partial byte streams.
*
* @param byteArray the byte array
* @param options object to control parsing behavior (optional)
* @returns {DataSet}
* @throws error if an error occurs while parsing. The exception object will contain a
* property dataSet with the elements successfully parsed before the error.
*/
export default function parseDicom(byteArray, options = {}) {
....
parseDicom
accepts a byteArray
as input. This byteArray for sure cannot be a nodejs stream, right?
There is also some code:
var reader = new FileReader();
reader.onload = function(file) {
var arrayBuffer = reader.result;
// Here we have the file data as an ArrayBuffer. dicomParser requires as input a
// Uint8Array so we create that here
var byteArray = new Uint8Array(arrayBuffer);
What is an ArrayBuffer? What is a Uint8Array ?
How are they related to byte stream?