forked from JuliaIO/TranscodingStreams.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscode.jl
More file actions
171 lines (144 loc) · 4.22 KB
/
transcode.jl
File metadata and controls
171 lines (144 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Transcode
# =========
"""
transcode(::Type{C}, data::Vector{UInt8})::Vector{UInt8} where C<:Codec
Transcode `data` by applying a codec `C()`.
Note that this method does allocation and deallocation of `C()` in every call,
which is handy but less efficient when transcoding a number of objects.
`transcode(codec, data)` is a recommended method in terms of performance.
Examples
--------
```julia
julia> using CodecZlib
julia> data = b"abracadabra";
julia> compressed = transcode(ZlibCompressor, data);
julia> decompressed = transcode(ZlibDecompressor, compressed);
julia> String(decompressed)
"abracadabra"
```
"""
function Base.transcode(::Type{C}, data::ByteData) where C<:Codec
codec = C()
initialize(codec)
try
return transcode(codec, data)
finally
finalize(codec)
end
end
"""
transcode(codec::Codec, data::Vector{UInt8})::Vector{UInt8}
Transcode `data` by applying `codec`.
Note that this method does not initialize or finalize `codec`. This is
efficient when you transcode a number of pieces of data, but you need to call
[`TranscodingStreams.initialize`](@ref) and
[`TranscodingStreams.finalize`](@ref) explicitly.
Examples
--------
```julia
julia> using CodecZlib
julia> data = b"abracadabra";
julia> codec = ZlibCompressor();
julia> TranscodingStreams.initialize(codec)
julia> compressed = transcode(codec, data);
julia> TranscodingStreams.finalize(codec)
julia> codec = ZlibDecompressor();
julia> TranscodingStreams.initialize(codec)
julia> decompressed = transcode(codec, compressed);
julia> TranscodingStreams.finalize(codec)
julia> String(decompressed)
"abracadabra"
```
"""
function Base.transcode(codec::Codec, data::ByteData)
input = Buffer(data)
output = Buffer(initial_output_size(codec, buffermem(input)))
error = Error()
code = startproc(codec, :write, error)
if code === :error
@goto error
end
n = minoutsize(codec, buffermem(input))
@label process
makemargin!(output, n)
Δin, Δout, code = process(codec, buffermem(input), marginmem(output), error)
@debug(
"called process()",
code = code,
input_size = buffersize(input),
output_size = marginsize(output),
input_delta = Δin,
output_delta = Δout,
)
consumed!(input, Δin)
supplied!(output, Δout)
if code === :error
@goto error
elseif code === :end
if buffersize(input) > 0
if startproc(codec, :write, error) === :error
@goto error
end
n = minoutsize(codec, buffermem(input))
@goto process
end
resize!(output.data, output.marginpos - 1)
return output.data
else
n = max(Δout, minoutsize(codec, buffermem(input)))
@goto process
end
@label error
if !haserror(error)
set_default_error!(error)
end
throw(error[])
end
# Return the initial output buffer size.
function initial_output_size(codec::Codec, input::Memory)
return max(
minoutsize(codec, input),
expectedsize(codec, input),
8, # just in case where both minoutsize and expectedsize are foolish
)
end
# Mutates the provided output buffer. Useful for cases when output size is known.
function transcode!(codec::Codec, data::ByteData,output::Buffer)
input = Buffer(data)
error = Error()
code = startproc(codec, :write, error)
if code === :error
@goto error
end
@label process
Δin, Δout, code = process(codec, buffermem(input), marginmem(output), error)
@debug(
"called process()",
code = code,
input_size = buffersize(input),
output_size = marginsize(output),
input_delta = Δin,
output_delta = Δout,
)
consumed!(input, Δin)
supplied!(output, Δout)
if code === :error
@goto error
elseif code === :end
if buffersize(input) > 0
if startproc(codec, :write, error) === :error
@goto error
end
@goto process
end
resize!(output.data, output.marginpos - 1)
return output.data
else
@goto process
end
@label error
if !haserror(error)
set_default_error!(error)
end
throw(error[])
end