Skip to content

Commit 2ac7394

Browse files
authored
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.
1 parent 433596f commit 2ac7394

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/stream.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ function fillbuffer(stream::TranscodingStream)
392392
if stream.state.code == :error
393393
handle_error(stream)
394394
end
395+
if stream.state.code == :ok && Δin == Δout == 0
396+
makemargin!(buffer1, max(16, marginsize(buffer1) * 2))
397+
end
395398
end
396399
return nfilled
397400
end
@@ -443,6 +446,8 @@ function process_to_write(stream::TranscodingStream)
443446
buffer2.total += Δout
444447
if stream.state.code == :error
445448
handle_error(stream)
449+
elseif stream.state.code == :ok && Δin == Δout == 0
450+
makemargin!(buffer2, max(16, marginsize(buffer2) * 2))
446451
end
447452
makemargin!(buffer1, 0)
448453
return Δin

test/runtests.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,29 @@ struct InvalidCodec <: TranscodingStreams.Codec end
187187
@test_throws MethodError read(TranscodingStream(InvalidCodec(), IOBuffer()))
188188
end
189189

190+
struct QuadrupleCodec <: TranscodingStreams.Codec end
191+
192+
function TranscodingStreams.process(
193+
codec :: QuadrupleCodec,
194+
input :: TranscodingStreams.Memory,
195+
output :: TranscodingStreams.Memory,
196+
error :: TranscodingStreams.Error)
197+
i = j = 0
198+
while i + 1 endof(input) && j + 4 endof(output)
199+
b = input[i+1]
200+
i += 1
201+
output[j+1] = output[j+2] = output[j+3] = output[j+4] = b
202+
j += 4
203+
end
204+
return i, j, input.size == 0 ? (:end) : (:ok)
205+
end
206+
207+
@testset "QuadrupleCodec" begin
208+
@test transcode(QuadrupleCodec(), b"") == b""
209+
@test transcode(QuadrupleCodec(), b"a") == b"aaaa"
210+
@test transcode(QuadrupleCodec(), b"ab") == b"aaaabbbb"
211+
end
212+
190213
for pkg in ["CodecZlib", "CodecBzip2", "CodecXz", "CodecZstd"]
191214
Pkg.test(pkg)
192215
end

0 commit comments

Comments
 (0)