diff --git a/src/buffer.jl b/src/buffer.jl index 8e5d744..987ff05 100644 --- a/src/buffer.jl +++ b/src/buffer.jl @@ -203,7 +203,7 @@ end # Insert data to the current buffer. # `data` must not alias `buf` -function insertdata!(buf::Buffer, data::Union{AbstractArray{UInt8}, Memory}) +function insertdata!(buf::Buffer, data::Union{AbstractVector{UInt8}, Memory}) nbytes = Int(length(data)) makemargin!(buf, nbytes) datapos = if iszero(buf.markpos) diff --git a/src/stream.jl b/src/stream.jl index d057cf3..8114157 100644 --- a/src/stream.jl +++ b/src/stream.jl @@ -462,6 +462,7 @@ inserted. `data` must not alias any internal buffers in `stream` """ function unread(stream::TranscodingStream, data::AbstractVector{UInt8}) + ready_to_read!(stream) insertdata!(stream.buffer1, data) return nothing end diff --git a/test/codecdoubleframe.jl b/test/codecdoubleframe.jl index 7730d8a..c39fdbc 100644 --- a/test/codecdoubleframe.jl +++ b/test/codecdoubleframe.jl @@ -396,6 +396,22 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD close(stream) end + @testset "unread" begin + stream = DoubleFrameDecoderStream(IOBuffer("[ ffoooobbaarr ]")) + @test position(stream) == 0 + @test read(stream, 3) == b"foo" + @test position(stream) == 3 + @test read(stream, 3) == b"bar" + @test position(stream) == 6 + @test TranscodingStreams.unread(stream, b"baz") === nothing + @test position(stream) == 3 + @test read(stream, 3) == b"baz" + @test position(stream) == 6 + @test eof(stream) + @test position(stream) == 6 + close(stream) + end + test_roundtrip_read(DoubleFrameEncoderStream, DoubleFrameDecoderStream) test_roundtrip_write(DoubleFrameEncoderStream, DoubleFrameDecoderStream) test_roundtrip_lines(DoubleFrameEncoderStream, DoubleFrameDecoderStream) diff --git a/test/codecnoop.jl b/test/codecnoop.jl index 9db6bc1..d5a0862 100644 --- a/test/codecnoop.jl +++ b/test/codecnoop.jl @@ -325,13 +325,20 @@ using FillArrays: Zeros @testset "unread" begin stream = NoopStream(IOBuffer("")) @test TranscodingStreams.unread(stream, b"foo") === nothing + @test position(stream) == -3 @test read(stream, 3) == b"foo" + @test position(stream) == 0 + @test eof(stream) close(stream) stream = NoopStream(IOBuffer("foo")) @test read(stream, 3) == b"foo" + @test position(stream) == 3 @test TranscodingStreams.unread(stream, b"bar") === nothing + @test position(stream) == 0 @test read(stream, 3) == b"bar" + @test position(stream) == 3 + @test eof(stream) close(stream) stream = NoopStream(IOBuffer("foobar")) @@ -351,24 +358,36 @@ using FillArrays: Zeros close(stream) stream = NoopStream(IOBuffer("foobar")) + @test position(stream) == 0 @test read(stream, 3) == b"foo" + @test position(stream) == 3 @test read(stream, 3) == b"bar" + @test position(stream) == 6 @test TranscodingStreams.unread(stream, b"baz") === nothing + @test position(stream) == 3 @test read(stream, 3) == b"baz" + @test position(stream) == 6 @test eof(stream) + @test position(stream) == 6 close(stream) for bufsize in (1, 2, 3, 4, 100) for n in (1, 100) stream = NoopStream(IOBuffer("foo"^n*"bar"^n); bufsize) @test mark(stream) == 0 + @test position(stream) == 0 @test read(stream, 3n) == codeunits("foo"^n) @test read(stream, 3n) == codeunits("bar"^n) + @test position(stream) == 6n TranscodingStreams.unread(stream, codeunits("baz"^n)) + @test position(stream) == 3n @test reset(stream) == 0 + @test position(stream) == 0 @test read(stream, 3n) == codeunits("foo"^n) @test read(stream, 3n) == codeunits("baz"^n) + @test position(stream) == 6n @test eof(stream) + @test position(stream) == 6n close(stream) end end @@ -414,6 +433,11 @@ using FillArrays: Zeros end @test read(stream, 3) == b"bar" close(stream) + + stream = NoopStream(IOBuffer()) + write(stream, b"foo") + @test_throws ArgumentError TranscodingStreams.unread(stream, b"bar") + close(stream) end stream = NoopStream(IOBuffer(""))