Skip to content

Commit f84d7cf

Browse files
authored
implement Base.seekstart and Base.seekend (#28)
1 parent 0cec52c commit f84d7cf

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/stream.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ function splitkwargs(kwargs, keys)
119119
return hits, others
120120
end
121121

122+
# Check that mode is valid.
123+
macro checkmode(validmodes)
124+
mode = esc(:mode)
125+
quote
126+
if !$(foldr((x, y) -> :($(mode) == $(QuoteNode(x)) || $(y)), false, eval(validmodes)))
127+
throw(ArgumentError(string("invalid mode :", $(mode))))
128+
end
129+
end
130+
end
131+
122132

123133
# Base IO Functions
124134
# -----------------
@@ -213,6 +223,34 @@ function Base.skip(stream::TranscodingStream, offset::Integer)
213223
end
214224

215225

226+
# Seek Operations
227+
# ---------------
228+
229+
function Base.seekstart(stream::TranscodingStream)
230+
mode = stream.state.mode
231+
@checkmode (:idle, :read, :write)
232+
if mode == :read || mode == :write
233+
callstartproc(stream, mode)
234+
emptybuffer!(stream.state.buffer1)
235+
emptybuffer!(stream.state.buffer2)
236+
end
237+
seekstart(stream.stream)
238+
return
239+
end
240+
241+
function Base.seekend(stream::TranscodingStream)
242+
mode = stream.state.mode
243+
@checkmode (:idle, :read, :write)
244+
if mode == :read || mode == :write
245+
callstartproc(stream, mode)
246+
emptybuffer!(stream.state.buffer1)
247+
emptybuffer!(stream.state.buffer2)
248+
end
249+
seekend(stream.stream)
250+
return
251+
end
252+
253+
216254
# Read Functions
217255
# --------------
218256

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,14 @@ end
392392
import CodecZlib: GzipCompression, GzipDecompression
393393
TranscodingStreams.test_chunked_read(GzipCompression, GzipDecompression)
394394
TranscodingStreams.test_chunked_write(GzipCompression, GzipDecompression)
395+
396+
@testset "seek" begin
397+
data = transcode(GzipCompression, b"abracadabra")
398+
stream = TranscodingStream(GzipDecompression(), IOBuffer(data))
399+
seekstart(stream)
400+
@test read(stream, 3) == b"abr"
401+
seekstart(stream)
402+
@test read(stream, 3) == b"abr"
403+
seekend(stream)
404+
#@test eof(stream)
405+
end

0 commit comments

Comments
 (0)