Skip to content

Commit

Permalink
expand output buffer when no data are processed (#10)
Browse files Browse the repository at this point in the history
When no data are consumed and processed, the codec would expect the
output buffer will be expanded in the next call. This is necessary when
the codec want to write a epilogue to the output but it has not enough
size to store the data.
  • Loading branch information
bicycle1885 authored Aug 20, 2017
1 parent 433596f commit 2ac7394
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ function fillbuffer(stream::TranscodingStream)
if stream.state.code == :error
handle_error(stream)
end
if stream.state.code == :ok && Δin == Δout == 0
makemargin!(buffer1, max(16, marginsize(buffer1) * 2))
end
end
return nfilled
end
Expand Down Expand Up @@ -443,6 +446,8 @@ function process_to_write(stream::TranscodingStream)
buffer2.total += Δout
if stream.state.code == :error
handle_error(stream)
elseif stream.state.code == :ok && Δin == Δout == 0
makemargin!(buffer2, max(16, marginsize(buffer2) * 2))
end
makemargin!(buffer1, 0)
return Δin
Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,29 @@ struct InvalidCodec <: TranscodingStreams.Codec end
@test_throws MethodError read(TranscodingStream(InvalidCodec(), IOBuffer()))
end

struct QuadrupleCodec <: TranscodingStreams.Codec end

function TranscodingStreams.process(
codec :: QuadrupleCodec,
input :: TranscodingStreams.Memory,
output :: TranscodingStreams.Memory,
error :: TranscodingStreams.Error)
i = j = 0
while i + 1 endof(input) && j + 4 endof(output)
b = input[i+1]
i += 1
output[j+1] = output[j+2] = output[j+3] = output[j+4] = b
j += 4
end
return i, j, input.size == 0 ? (:end) : (:ok)
end

@testset "QuadrupleCodec" begin
@test transcode(QuadrupleCodec(), b"") == b""
@test transcode(QuadrupleCodec(), b"a") == b"aaaa"
@test transcode(QuadrupleCodec(), b"ab") == b"aaaabbbb"
end

for pkg in ["CodecZlib", "CodecBzip2", "CodecXz", "CodecZstd"]
Pkg.test(pkg)
end

0 comments on commit 2ac7394

Please sign in to comment.