Skip to content

Commit

Permalink
undo suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 committed Feb 25, 2024
1 parent 803bf22 commit 3b1d67a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
27 changes: 18 additions & 9 deletions src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion test/codecdoubleframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions test/codecquadruple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -151,6 +151,5 @@ end
@test take!(sink) == b"xxxxyyyy"
close(stream)
@test eof(stream)
@test_throws EOFError read(stream, UInt8)
end
end

0 comments on commit 3b1d67a

Please sign in to comment.