From 2ac7394576932d1cf6d30f788a5afcfb612f8208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kenta=20Sato=20=28=E4=BD=90=E8=97=A4=20=E5=BB=BA=E5=A4=AA?= =?UTF-8?q?=29?= Date: Sun, 20 Aug 2017 12:29:19 +0900 Subject: [PATCH] expand output buffer when no data are processed (#10) 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. --- src/stream.jl | 5 +++++ test/runtests.jl | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/stream.jl b/src/stream.jl index dd347c56..454eb4e0 100644 --- a/src/stream.jl +++ b/src/stream.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 4a2e0cea..04795aae 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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