22# =========
33
44"""
5- transcode(::Type{C}, data::Vector{UInt8})::Vector{UInt8} where C<:Codec
5+ transcode(
6+ ::Type{C},
7+ data::Union{Vector{UInt8},Base.CodeUnits{UInt8}},
8+ )::Vector{UInt8} where {C<:Codec}
69
710Transcode `data` by applying a codec `C()`.
811
@@ -27,21 +30,34 @@ julia> String(decompressed)
2730
2831```
2932"""
30- function Base. transcode (:: Type{C} , data :: ByteData ) where C<: Codec
33+ function Base. transcode (:: Type{C} , args ... ) where { C<: Codec }
3134 codec = C ()
3235 initialize (codec)
3336 try
34- return transcode (codec, data )
37+ return transcode (codec, args ... )
3538 finally
3639 finalize (codec)
3740 end
3841end
3942
43+ _default_output_buffer (codec, input) = Buffer (
44+ initial_output_size (
45+ codec,
46+ buffermem (input)
47+ )
48+ )
49+
4050"""
41- transcode(codec::Codec, data::Vector{UInt8})::Vector{UInt8}
51+ transcode(
52+ codec::Codec,
53+ data::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer},
54+ [output::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer}],
55+ )::Vector{UInt8}
4256
4357Transcode `data` by applying `codec`.
4458
59+ If `output` is unspecified, then this method will allocate it.
60+
4561Note that this method does not initialize or finalize `codec`. This is
4662efficient when you transcode a number of pieces of data, but you need to call
4763[`TranscodingStreams.initialize`](@ref) and
@@ -59,7 +75,9 @@ julia> codec = ZlibCompressor();
5975
6076julia> TranscodingStreams.initialize(codec)
6177
62- julia> compressed = transcode(codec, data);
78+ julia> compressed = Vector{UInt8}()
79+
80+ julia> transcode(codec, data, compressed);
6381
6482julia> TranscodingStreams.finalize(codec)
6583
@@ -76,9 +94,29 @@ julia> String(decompressed)
7694
7795```
7896"""
79- function Base. transcode (codec:: Codec , data:: ByteData )
80- input = Buffer (data)
81- output = Buffer (initial_output_size (codec, buffermem (input)))
97+ function Base. transcode (
98+ codec:: Codec ,
99+ input:: Buffer ,
100+ output:: Union{Buffer,Nothing} = nothing ,
101+ )
102+ output = (output === nothing ? _default_output_buffer (codec, input) : initbuffer! (output))
103+ transcode! (output, codec, input)
104+ end
105+
106+ """
107+ transcode!(output::Buffer, codec::Codec, input::Buffer)
108+
109+ Transcode `input` by applying `codec` and storing the results in `output`.
110+ Note that this method does not initialize or finalize `codec`. This is
111+ efficient when you transcode a number of pieces of data, but you need to call
112+ [`TranscodingStreams.initialize`](@ref) and
113+ [`TranscodingStreams.finalize`](@ref) explicitly.
114+ """
115+ function transcode! (
116+ output:: Buffer ,
117+ codec:: Codec ,
118+ input:: Buffer ,
119+ )
82120 error = Error ()
83121 code = startproc (codec, :write , error)
84122 if code === :error
@@ -121,6 +159,12 @@ function Base.transcode(codec::Codec, data::ByteData)
121159 throw (error[])
122160end
123161
162+ Base. transcode (codec:: Codec , data:: Buffer , output:: ByteData ) =
163+ transcode (codec, data, Buffer (output))
164+
165+ Base. transcode (codec:: Codec , data:: ByteData , args... ) =
166+ transcode (codec, Buffer (data), args... )
167+
124168# Return the initial output buffer size.
125169function initial_output_size (codec:: Codec , input:: Memory )
126170 return max (
0 commit comments