@@ -245,43 +245,42 @@ written in known `Format`. For example, `Stream{PNG}(io)` would
245
245
indicate PNG format. If known, the optional `filename` argument can
246
246
be used to improve error messages, etc.
247
247
"""
248
- struct Stream{F<: DataFormat ,IOtype<: IO } <: Formatted{F}
248
+ struct Stream{F <: DataFormat , IOtype <: IO } <: Formatted{F}
249
249
io:: IOtype
250
- filename:: Nullable {String}
250
+ filename:: Union {String, Nothing }
251
251
end
252
252
253
- Stream {F<:DataFormat} (:: Type{F} , io:: IO ) = Stream {F,typeof(io)} (io, Nullable {String} () )
254
- Stream {F<:DataFormat} (:: Type{F} , io:: IO , filename:: AbstractString ) = Stream {F,typeof(io)} (io,String (filename))
255
- Stream {F<:DataFormat} (:: Type{F} , io:: IO , filename) = Stream {F,typeof(io)} (io,filename)
256
- Stream {F} (file:: File{F} , io:: IO ) = Stream {F,typeof(io)} (io,filename (file))
253
+ Stream {F<:DataFormat} (:: Type{F} , io:: IO ) = Stream {F,typeof(io)} (io, nothing )
254
+ Stream {F<:DataFormat} (:: Type{F} , io:: IO , filename:: AbstractString ) = Stream {F, typeof(io)} (io, String (filename))
255
+ Stream {F<:DataFormat} (:: Type{F} , io:: IO , filename) = Stream {F, typeof(io)} (io, filename)
256
+ Stream {F} (file:: File{F} , io:: IO ) = Stream {F, typeof(io)} (io, filename (file))
257
257
258
258
" `stream(s)` returns the stream associated with `Stream` `s`"
259
259
stream (s:: Stream ) = s. io
260
260
261
261
"""
262
- `filename(stream)` returns a nullable- string of the filename
263
- associated with `Stream` `stream`.
262
+ `filename(stream)` returns a string of the filename
263
+ associated with `Stream` `stream`, or nothing if there is no file associated .
264
264
"""
265
265
filename (s:: Stream ) = s. filename
266
266
267
267
"""
268
268
`file_extension(file)` returns a nullable-string for the file extension associated with `Stream` `stream`.
269
269
"""
270
270
function file_extension (f:: Stream )
271
- isnull (filename (f)) && return filename (f)
272
- splitext (get (filename (f)))[2 ]
271
+ fname = filename (f)
272
+ (fname == nothing ) && return nothing
273
+ splitext (fname)[2 ]
273
274
end
274
275
275
276
# Note this closes the stream. It's useful when you've opened
276
277
# the file to check the magic bytes, but don't want to leave
277
278
# a dangling stream.
278
279
function file! {F} (strm:: Stream{F} )
279
280
f = filename (strm)
280
- if isnull (f)
281
- error (" filename unknown" )
282
- end
281
+ f == nothing && error (" filename unknown" )
283
282
close (strm. io)
284
- File {F} (get (f) )
283
+ File {F} (f )
285
284
end
286
285
287
286
# Implement standard I/O operations for File and Stream
@@ -329,9 +328,9 @@ skipmagic(io, magic::Function) = nothing
329
328
skipmagic {N} (io, magic:: NTuple{N,UInt8} ) = seek (io, length (magic))
330
329
function skipmagic (io, magic:: Tuple )
331
330
lengths = map (length, magic)
332
- all (x-> lengths[1 ] == x, lengths) && return seek (io, lengths[1 ]) # it doesn't matter what magic bytes get skipped as they all have the same length
331
+ all (x-> lengths[1 ] == x, lengths) && return seek (io, lengths[1 ]) # it doesn't matter what magic bytes get skipped as they all have the same length
333
332
magic = [magic... ]
334
- sort! (magic, lt= (a,b)-> length (a)>= length (b)) # start with longest first, to avoid overlapping magic bytes
333
+ sort! (magic, lt = (a,b)-> length (a) >= length (b)) # start with longest first, to avoid overlapping magic bytes
335
334
seekend (io)
336
335
len = position (io)
337
336
seekstart (io)
@@ -397,9 +396,9 @@ hasfunction(s::Tuple) = false #has magic
397
396
`query(io, [filename])` returns a `Stream` object with information about the
398
397
format inferred from the magic bytes.
399
398
"""
400
- query (io:: IO , filename) = query (io, Nullable ( String (filename) ))
399
+ query (io:: IO , filename) = query (io, String (filename))
401
400
402
- function query (io:: IO , filename:: Nullable{ String}= Nullable {String} () )
401
+ function query (io:: IO , filename:: Union{Nothing, String} = nothing )
403
402
magic = Vector {UInt8} (0 )
404
403
pos = position (io)
405
404
for p in magic_list
@@ -408,7 +407,7 @@ function query(io::IO, filename::Nullable{String}=Nullable{String}())
408
407
while length (m) > length (magic)
409
408
if eof (io)
410
409
seek (io, pos)
411
- return Stream {unknown_df,typeof(io)} (io, filename)
410
+ return Stream {unknown_df, typeof(io)} (io, filename)
412
411
end
413
412
push! (magic, read (io, UInt8))
414
413
end
@@ -421,8 +420,14 @@ function query(io::IO, filename::Nullable{String}=Nullable{String}())
421
420
for p in magic_func
422
421
seek (io, pos)
423
422
f = first (p)
424
- if f (io)
425
- return Stream {DataFormat{last(p)},typeof(io)} (seek (io, pos), filename)
423
+ try
424
+ if f (io)
425
+ return Stream {DataFormat{last(p)},typeof(io)} (seek (io, pos), filename)
426
+ end
427
+ catch e
428
+ println (" There was an error in magick function $f " )
429
+ println (" Please open an issue at FileIO.jl. Error:" )
430
+ println (e)
426
431
end
427
432
end
428
433
seek (io, pos)
0 commit comments