@@ -61,15 +61,76 @@ module Dummy
61
61
62
62
using FileIO
63
63
64
- function load (file:: File{format"DUMMY"} )
64
+ mutable struct DummyReader{IOtype}
65
+ stream:: IOtype
66
+ ownstream:: Bool
67
+ bytesleft:: Int64
68
+ end
69
+
70
+ function DummyReader (stream, ownstream)
71
+ read (stream, 5 ) == magic (format " DUMMY" ) || error (" wrong magic bytes" )
72
+ DummyReader (stream, ownstream, read (stream, Int64))
73
+ end
74
+
75
+ function Base. read (stream:: DummyReader , n)
76
+ toread = min (n, stream. bytesleft)
77
+ buf = read (stream. stream, toread)
78
+ stream. bytesleft -= length (buf)
79
+ buf
80
+ end
81
+
82
+ Base. eof (stream:: DummyReader ) = stream. bytesleft == 0 || eof (stream. stream)
83
+ Base. close (stream:: DummyReader ) = stream. ownstream && close (stream. stream)
84
+
85
+ mutable struct DummyWriter{IOtype}
86
+ stream:: IOtype
87
+ ownstream:: Bool
88
+ headerpos:: Int
89
+ byteswritten:: Int
90
+ end
91
+
92
+ function DummyWriter (stream, ownstream)
93
+ write (stream, magic (format " DUMMY" )) # Write the magic bytes
94
+ # store the position where we'll need to write the length
95
+ pos = position (stream)
96
+ # write a dummy length value
97
+ write (stream, 0xffffffffffffffff )
98
+ DummyWriter (stream, ownstream, pos, 0 )
99
+ end
100
+
101
+ function Base. write (stream:: DummyWriter , data)
102
+ udata = convert (Vector{UInt8}, data)
103
+ n = write (stream. stream, udata)
104
+ stream. byteswritten += n
105
+
106
+ n
107
+ end
108
+
109
+ function Base. close (stream:: DummyWriter )
110
+ here = position (stream. stream)
111
+ # go back and write the header
112
+ seek (stream. stream, stream. headerpos)
113
+ write (stream. stream, convert (Int64, stream. byteswritten))
114
+ seek (stream. stream, here)
115
+ stream. ownstream && close (stream. stream)
116
+
117
+ nothing
118
+ end
119
+
120
+ loadstreaming (s:: Stream{format"DUMMY"} ) = DummyReader (s, false )
121
+ loadstreaming (file:: File{format"DUMMY"} ) = DummyReader (open (file), true )
122
+ savestreaming (s:: Stream{format"DUMMY"} ) = DummyWriter (s, false )
123
+ savestreaming (file:: File{format"DUMMY"} ) = DummyWriter (open (file, " w" ), true )
124
+
125
+ # we could implement `load` and `save` in terms of their streaming versions
126
+ function FileIO. load (file:: File{format"DUMMY"} )
65
127
open (file) do s
66
- skipmagic (s)
67
128
load (s)
68
129
end
69
130
end
70
131
71
- function load (s:: Stream{format"DUMMY"} )
72
- # We're already past the magic bytes
132
+ function FileIO . load (s:: Stream{format"DUMMY"} )
133
+ skipmagic (s)
73
134
n = read (s, Int64)
74
135
out = Vector {UInt8} (n)
75
136
read! (s, out)
@@ -133,7 +194,6 @@ add_saver(format"DUMMY", :Dummy)
133
194
@test a == b
134
195
135
196
b = open (query (fn)) do s
136
- skipmagic (s)
137
197
load (s)
138
198
end
139
199
@test a == b
0 commit comments