Skip to content

Commit 2532b91

Browse files
authored
add initialized flag (#59)
1 parent 0d26a46 commit 2532b91

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/stream.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@ struct TranscodingStream{C<:Codec,S<:IO} <: IO
2020
# mutable state of the stream
2121
state::State
2222

23-
function TranscodingStream{C,S}(codec::C, stream::S, state::State) where {C<:Codec,S<:IO}
23+
function TranscodingStream{C,S}(codec::C, stream::S, state::State, initialized::Bool) where {C<:Codec,S<:IO}
2424
if !isopen(stream)
2525
throw(ArgumentError("closed stream"))
2626
elseif state.mode != :idle
2727
throw(ArgumentError("invalid initial mode"))
2828
end
29-
initialize(codec)
29+
if !initialized
30+
initialize(codec)
31+
end
3032
return new(codec, stream, state)
3133
end
3234
end
3335

34-
function TranscodingStream(codec::C, stream::S, state::State) where {C<:Codec,S<:IO}
35-
return TranscodingStream{C,S}(codec, stream, state)
36+
function TranscodingStream(codec::C, stream::S, state::State;
37+
initialized::Bool=false) where {C<:Codec,S<:IO}
38+
return TranscodingStream{C,S}(codec, stream, state, initialized)
3639
end
3740

3841
const DEFAULT_BUFFER_SIZE = 16 * 2^10 # 16KiB

src/testtools.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ end
3232
function test_roundtrip_transcode(encode, decode)
3333
seed!(TEST_RANDOM_SEED)
3434
encoder = encode()
35+
initialize(encoder)
3536
decoder = decode()
37+
initialize(decoder)
3638
for n in vcat(0:30, sort!(rand(500:100_000, 30))), alpha in (0x00:0xff, 0x00:0x0f)
3739
data = rand(alpha, n)
3840
Test.@test hash(transcode(decode, transcode(encode, data))) == hash(data)

src/transcode.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ julia> String(decompressed)
2929
"""
3030
function Base.transcode(::Type{C}, data::Vector{UInt8}) where C<:Codec
3131
codec = C()
32+
initialize(codec)
3233
try
3334
return transcode(codec, data)
3435
finally
@@ -74,7 +75,7 @@ function Base.transcode(codec::Codec, data::Vector{UInt8})
7475
buffer2 = Buffer(
7576
expectedsize(codec, Memory(data)) + minoutsize(codec, Memory(C_NULL, 0)))
7677
mark!(buffer2)
77-
stream = TranscodingStream(codec, devnull, State(Buffer(data), buffer2))
78+
stream = TranscodingStream(codec, devnull, State(Buffer(data), buffer2); initialized=true)
7879
write(stream, TOKEN_END)
7980
return takemarked!(buffer2)
8081
end

0 commit comments

Comments
 (0)