You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+74Lines changed: 74 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,40 @@ s = query(io) # io is a stream
37
37
will return a `File` or `Stream` object that also encodes the detected
38
38
file format.
39
39
40
+
Sometimes you want to read or write files that are larger than your available
41
+
memory, or might be an unknown or infinite length (e.g. reading an audio or
42
+
video stream from a socket). In these cases it might not make sense to process
43
+
the whole file at once, but instead process it a chunk at a time. For these situations FileIO provides the `loadstreaming` and `savestreaming` functions, which return an object that you can `read` or `write`, rather than the file data itself.
44
+
45
+
This would look something like:
46
+
47
+
```jl
48
+
using FileIO
49
+
audio =loadstreaming("bigfile.wav")
50
+
try
51
+
while!eof(audio)
52
+
chunk =read(audio, 4096) # read 4096 frames
53
+
# process the chunk
54
+
end
55
+
finally
56
+
close(stream)
57
+
end
58
+
```
59
+
60
+
or use `do` syntax to auto-close the stream:
61
+
62
+
```jl
63
+
using FileIO
64
+
doloadstreaming("bigfile.wav") audio
65
+
while!eof(audio)
66
+
chunk =read(audio, 4096) # read 4096 frames
67
+
# process the chunk
68
+
end
69
+
end
70
+
```
71
+
72
+
Note that in these cases you may want to use `read!` with a pre-allocated buffer for maximum efficiency.
73
+
40
74
## Adding new formats
41
75
42
76
You register a new format by adding `add_format(fmt, magic,
@@ -139,6 +173,46 @@ automatically even if the code inside the `do` scope throws an error.)
139
173
Conversely, `load(::Stream)` and `save(::Stream)` should not close the
140
174
input stream.
141
175
176
+
`loadstreaming` and `savestreaming` use the same query mechanism, but return a decoded stream that users can `read` or `write`. You should also implement a `close` method on your reader or writer type. Just like with `load` and `save`, if the user provided a filename, your `close` method should be responsible for closing any streams you opened in order to read or write the file. If you are given a `Stream`, your `close` method should only do the clean up for your reader or writer type, not close the stream.
0 commit comments