From a7df41a139ed5637f54fcaaa6cf99ea8343958a7 Mon Sep 17 00:00:00 2001 From: Nathan Zimmerberg <39104088+nhz2@users.noreply.github.com> Date: Mon, 25 Mar 2024 20:02:16 -0400 Subject: [PATCH] Fix reading nested streams with `stop_on_end=true` set. (#191) --- src/noop.jl | 9 +++++++-- src/stream.jl | 9 +++++++-- test/codecdoubleframe.jl | 6 +++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/noop.jl b/src/noop.jl index 9d5fa09..543fdf6 100644 --- a/src/noop.jl +++ b/src/noop.jl @@ -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) diff --git a/src/stream.jl b/src/stream.jl index 18ae2df..3b1a15c 100644 --- a/src/stream.jl +++ b/src/stream.jl @@ -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) diff --git a/test/codecdoubleframe.jl b/test/codecdoubleframe.jl index 9b637bf..dbe20eb 100644 --- a/test/codecdoubleframe.jl +++ b/test/codecdoubleframe.jl @@ -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( @@ -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)