diff --git a/src/stream.jl b/src/stream.jl index 0313bd4c..57210b4f 100644 --- a/src/stream.jl +++ b/src/stream.jl @@ -328,12 +328,16 @@ end function Base.read(stream::TranscodingStream, ::Type{UInt8}) if eof(stream) - throw(EOFError()) + ready_to_read!(stream) + if eof(stream) + throw(EOFError()) + end end return readbyte!(stream.buffer1) end function Base.readuntil(stream::TranscodingStream, delim::UInt8; keep::Bool=false) + ready_to_read!(stream) buffer1 = stream.buffer1 # delay initialization so as to reduce the number of buffer resizes local ret::Vector{UInt8} @@ -374,6 +378,7 @@ function Base.readuntil(stream::TranscodingStream, delim::UInt8; keep::Bool=fals end function Base.unsafe_read(stream::TranscodingStream, output::Ptr{UInt8}, nbytes::UInt) + ready_to_read!(stream) buffer = stream.buffer1 p = output p_end = output + nbytes @@ -392,6 +397,7 @@ function Base.unsafe_read(stream::TranscodingStream, output::Ptr{UInt8}, nbytes: end function Base.readbytes!(stream::TranscodingStream, b::DenseArray{UInt8}, nb=length(b)) + ready_to_read!(stream) filled = 0 resized = false while filled < nb && !eof(stream) @@ -407,12 +413,9 @@ function Base.readbytes!(stream::TranscodingStream, b::DenseArray{UInt8}, nb=len return filled end -function Base.bytesavailable(stream::TranscodingStream)::Int - if stream.state.mode ∉ (:read, :stop) - 0 - else - buffersize(stream.buffer1) - end +function Base.bytesavailable(stream::TranscodingStream) + ready_to_read!(stream) + return buffersize(stream.buffer1) end function Base.readavailable(stream::TranscodingStream) @@ -446,12 +449,18 @@ function unsafe_unread(stream::TranscodingStream, data::Ptr, nbytes::Integer) if nbytes < 0 throw(ArgumentError("negative nbytes")) end + ready_to_read!(stream) + insertdata!(stream.buffer1, convert(Ptr{UInt8}, data), nbytes) + return nothing +end + +# Ready to read data from the stream. +function ready_to_read!(stream::TranscodingStream) mode = stream.state.mode if mode ∉ (:read, :stop) changemode!(stream, :read) end - insertdata!(stream.buffer1, convert(Ptr{UInt8}, data), nbytes) - return nothing + return end diff --git a/test/codecdoubleframe.jl b/test/codecdoubleframe.jl index 9c5546d9..a0f81020 100644 --- a/test/codecdoubleframe.jl +++ b/test/codecdoubleframe.jl @@ -208,7 +208,7 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD stream = TranscodingStream(DoubleFrameDecoder(), sink, stop_on_end=true) @test_broken write(stream, "[ yy ]sdfsadfasdfdf") == 4 @test eof(stream) - @test_throws EOFError read(stream, UInt8) + @test_throws ArgumentError read(stream, UInt8) flush(stream) @test take!(sink) == b"y" close(stream) diff --git a/test/codecnoop.jl b/test/codecnoop.jl index 3e4efc77..82e61ac0 100644 --- a/test/codecnoop.jl +++ b/test/codecnoop.jl @@ -229,9 +229,9 @@ # switch write => read stream = NoopStream(IOBuffer(b"foobar", read=true, write=true)) - begin + @test_throws ArgumentError begin write(stream, b"xyz") - @test isempty(read(stream, 3)) + read(stream, 3) end # switch read => write diff --git a/test/codecquadruple.jl b/test/codecquadruple.jl index 336d4d6d..d0b3bd39 100644 --- a/test/codecquadruple.jl +++ b/test/codecquadruple.jl @@ -140,7 +140,7 @@ end stream = TranscodingStream(QuadrupleCodec(), sink, bufsize=16) write(stream, "x") @test eof(stream) - @test_throws EOFError read(stream, UInt8) + @test_throws ArgumentError read(stream, UInt8) @test eof(stream) write(stream, "y") @test eof(stream) @@ -151,6 +151,5 @@ end @test take!(sink) == b"xxxxyyyy" close(stream) @test eof(stream) - @test_throws EOFError read(stream, UInt8) end end