Hi, everyone! Does anyone know how to implement something similar to the NodeJS method fs. createReadStream
for streaming a file in Crystal?
Stream a file in the context of a web application? Or?
It allows reading a file by chunks (stream), something like this:
reader = fs.createReadStream('input.txt', {
flag: 'a+',
encoding: 'UTF-8',
});
// Read and display the file data on console
reader.on('data', function (chunk) {
console.log(chunk);
});
So, I’m curious if there is something similar in Crystal (for reading files in chunks without loading the complete file in memory). I’ve been reading the Standard API docs but I couldn’t find anything similar.
When you open a file via like File.open
, you an IO instance, which is essentially a stream. It exposes various iterative methods like #each_line
, #each_byte
, etc. Or maybe more applicable in your case would be the #read_string or #read methods.
Yes, but I understand the file is loaded completely in memory, or maybe it doesn’t?
It’s not by default, unless you’re using like File.read
or File.open
then a #gets_to_end
.
Oh, nice! So I could also use read_at
or (seek
+ read
) for reading by chunks, right? Thank you so much!
You might wanna take a look at IO.copy