Stream file

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) { 

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, 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 or 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!

1 Like

You might wanna take a look at IO.copy

1 Like