2
2
# =========
3
3
4
4
"""
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}
6
9
7
10
Transcode `data` by applying a codec `C()`.
8
11
@@ -27,21 +30,34 @@ julia> String(decompressed)
27
30
28
31
```
29
32
"""
30
- function Base. transcode (:: Type{C} , data :: ByteData ) where C<: Codec
33
+ function Base. transcode (:: Type{C} , args ... ) where { C<: Codec }
31
34
codec = C ()
32
35
initialize (codec)
33
36
try
34
- return transcode (codec, data )
37
+ return transcode (codec, args ... )
35
38
finally
36
39
finalize (codec)
37
40
end
38
41
end
39
42
43
+ _default_output_buffer (codec, input) = Buffer (
44
+ initial_output_size (
45
+ codec,
46
+ buffermem (input)
47
+ )
48
+ )
49
+
40
50
"""
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}
42
56
43
57
Transcode `data` by applying `codec`.
44
58
59
+ If `output` is unspecified, then this method will allocate it.
60
+
45
61
Note that this method does not initialize or finalize `codec`. This is
46
62
efficient when you transcode a number of pieces of data, but you need to call
47
63
[`TranscodingStreams.initialize`](@ref) and
@@ -59,7 +75,9 @@ julia> codec = ZlibCompressor();
59
75
60
76
julia> TranscodingStreams.initialize(codec)
61
77
62
- julia> compressed = transcode(codec, data);
78
+ julia> compressed = Vector{UInt8}()
79
+
80
+ julia> transcode(codec, data, compressed);
63
81
64
82
julia> TranscodingStreams.finalize(codec)
65
83
@@ -76,9 +94,29 @@ julia> String(decompressed)
76
94
77
95
```
78
96
"""
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
+ )
82
120
error = Error ()
83
121
code = startproc (codec, :write , error)
84
122
if code === :error
@@ -121,6 +159,12 @@ function Base.transcode(codec::Codec, data::ByteData)
121
159
throw (error[])
122
160
end
123
161
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
+
124
168
# Return the initial output buffer size.
125
169
function initial_output_size (codec:: Codec , input:: Memory )
126
170
return max (
0 commit comments