Skip to content

Commit

Permalink
Fix reading nested streams with stop_on_end=true set. (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 committed Mar 26, 2024
1 parent 3be4361 commit a7df41a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/noop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,18 @@ end
# These methods are overloaded for the `Noop` codec because it has only one
# buffer for efficiency.

function fillbuffer(stream::NoopStream; eager::Bool = false)
function fillbuffer(stream::NoopStream; eager::Bool = false)::Int
changemode!(stream, :read)
buffer = stream.buffer1
@assert buffer === stream.buffer2
if stream.stream isa TranscodingStream && buffer === stream.stream.buffer1
# Delegate the operation when buffers are shared.
return fillbuffer(stream.stream, eager = eager)
underlying_mode::Symbol = stream.stream.state.mode
if underlying_mode === :idle || underlying_mode === :read
return fillbuffer(stream.stream, eager = eager)
else
return 0
end
end
nfilled::Int = 0
while ((!eager && buffersize(buffer) == 0) || (eager && makemargin!(buffer, 0, eager = true) > 0)) && !eof(stream.stream)
Expand Down
9 changes: 7 additions & 2 deletions src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,15 @@ end

# Read as much data as possbile from `input` to the margin of `output`.
# This function will not block if `input` has buffered data.
function readdata!(input::IO, output::Buffer)
function readdata!(input::IO, output::Buffer)::Int
if input isa TranscodingStream && input.buffer1 === output
# Delegate the operation to the underlying stream for shared buffers.
return fillbuffer(input)
mode::Symbol = input.state.mode
if mode === :idle || mode === :read
return fillbuffer(input)
else
return 0
end
end
nread::Int = 0
navail = bytesavailable(input)
Expand Down
6 changes: 3 additions & 3 deletions test/codecdoubleframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
stop_on_end=true,
)
))
@test_broken read(s1) == b""
@test_broken eof(s1)
@test read(s1) == b""
@test eof(s1)

s2 = NoopStream(
DoubleFrameDecoderStream(
Expand All @@ -281,7 +281,7 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
)
)
@test read(s2) == b""
@test_broken eof(s2)
@test eof(s2)
end

test_roundtrip_read(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
Expand Down

0 comments on commit a7df41a

Please sign in to comment.