Skip to content

Commit c8af8ca

Browse files
committed
add metadata(), to avoid loading the whole file
1 parent fe36768 commit c8af8ca

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/FileIO.jl

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export DataFormat,
2424
savestreaming,
2525
skipmagic,
2626
stream,
27-
unknown
27+
unknown,
28+
metadata
2829

2930
import Base.showerror
3031

src/loadsave.jl

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ savestreaming
9999

100100
# if a bare filename or IO stream are given, query for the format and dispatch
101101
# to the formatted handlers below
102-
for fn in (:load, :loadstreaming, :save, :savestreaming)
102+
for fn in (:load, :loadstreaming, :save, :savestreaming, :metadata)
103103
@eval $fn(s::Union{AbstractString,IO}, args...; options...) =
104104
$fn(query(s), args...; options...)
105105
end
@@ -152,7 +152,7 @@ end
152152

153153
# Handlers for formatted files/streams
154154

155-
for fn in (:load, :loadstreaming)
155+
for fn in (:load, :loadstreaming, :metadata)
156156
@eval function $fn{F}(q::Formatted{F}, args...; options...)
157157
if unknown(q)
158158
isfile(filename(q)) || open(filename(q)) # force systemerror

test/loadsave.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ function FileIO.load(file::File{format"DUMMY"})
129129
end
130130
end
131131

132+
function FileIO.metadata(file::File{format"DUMMY"})
133+
s = open(file)
134+
skipmagic(s)
135+
n = read(s, Int64)
136+
close(s)
137+
return n
138+
end
139+
132140
function FileIO.load(s::Stream{format"DUMMY"})
133141
skipmagic(s)
134142
n = read(s, Int64)
@@ -156,6 +164,7 @@ add_saver(format"DUMMY", :Dummy)
156164
a = [0x01,0x02,0x03]
157165
fn = string(tempname(), ".dmy")
158166
save(fn, a)
167+
@test metadata(fn) == 3
159168

160169
# Test for absolute paths
161170
cd(dirname(fn)) do

0 commit comments

Comments
 (0)