@@ -12,23 +12,40 @@ Transcoding protocol
12
12
Transcoding proceeds by calling some functions in a specific way. We call this
13
13
"transcoding protocol" and any codec must implement it as described below.
14
14
15
- There are four functions for a codec to implement:
15
+ There are six functions for a codec to implement:
16
+ - `expectedsize`: return the expected size of transcoded data
17
+ - `minoutsize`: return the minimum output size of `process`
16
18
- `initialize`: initialize the codec
17
19
- `finalize`: finalize the codec
18
20
- `startproc`: start processing with the codec
19
21
- `process`: process data with the codec.
20
22
21
23
These are defined in the `TranscodingStreams` and a new codec type must extend
22
24
these methods if necessary. Implementing a `process` method is mandatory but
23
- other three are optional. `initialize `, `finalize `, and `startproc` have a
24
- default implementation that does nothing .
25
+ others are optional. `expectedsize `, `minoutsize `, `initialize`, `finalize`,
26
+ and `startproc` have a default implementation .
25
27
26
28
Your codec type is denoted by `C` and its object by `codec`.
27
29
28
30
Errors that occur in these methods are supposed to be unrecoverable and the
29
31
stream will go to the panic state. Only `Base.isopen` and `Base.close` are
30
32
available in that state.
31
33
34
+ ### `expectedsize`
35
+
36
+ The `expectedsize(codec::C, input::Memory)::Int` method takes `codec` and
37
+ `input`, and returns the expected size of transcoded data. This method will be
38
+ used as a hint to determine the size of a data buffer when `transcode` is
39
+ called. A good hint will reduce the number of buffer resizing and hence result
40
+ in better performance.
41
+
42
+ ### `minoutsize`
43
+
44
+ The `minoutsize(codec::C, input::Memory)::Int` method takes `codec` and `input`,
45
+ and returns the minimum required size of the output memory when `process` is
46
+ called. For example, an encoder of base64 will write at least four bytes to the
47
+ output and hence it is reasonable to return 4 with this method.
48
+
32
49
### `initialize`
33
50
34
51
The `initialize(codec::C)::Void` method takes `codec` and returns `nothing`.
@@ -84,10 +101,34 @@ abstract type Codec end
84
101
# Methods
85
102
# -------
86
103
104
+ """
105
+ expectedsize(codec::Codec, input::Memory)::Int
106
+
107
+ Return the expected size of the transcoded `input` with `codec`.
108
+
109
+ The default method returns `input.size`.
110
+ """
111
+ function expectedsize (codec:: Codec , input:: Memory ):: Int
112
+ return input. size
113
+ end
114
+
115
+ """
116
+ minoutsize(codec::Codec, input::Memory)::Int
117
+
118
+ Return the minimum output size to be ensured when calling `process`.
119
+
120
+ The default method returns `max(1, div(input.size, 4))`.
121
+ """
122
+ function minoutsize (codec:: Codec , input:: Memory ):: Int
123
+ return max (1 , div (input. size, 4 ))
124
+ end
125
+
87
126
"""
88
127
initialize(codec::Codec)::Void
89
128
90
129
Initialize `codec`.
130
+
131
+ The default method does nothing.
91
132
"""
92
133
function initialize (codec:: Codec )
93
134
return nothing
97
138
finalize(codec::Codec)::Void
98
139
99
140
Finalize `codec`.
141
+
142
+ The default method does nothing.
100
143
"""
101
144
function finalize (codec:: Codec ):: Void
102
145
return nothing
106
149
startproc(codec::Codec, state::Symbol, error::Error)::Symbol
107
150
108
151
Start data processing with `codec` of `state`.
152
+
153
+ The default method does nothing and returns `:ok`.
109
154
"""
110
155
function startproc (codec:: Codec , state:: Symbol , error:: Error ):: Symbol
111
156
return :ok
115
160
process(codec::Codec, input::Memory, output::Memory, error::Error)::Tuple{Int,Int,Symbol}
116
161
117
162
Do data processing with `codec`.
163
+
164
+ There is no default method.
118
165
"""
119
166
function process (codec:: Codec , input:: Memory , output:: Memory , error:: Error ):: Tuple{Int,Int,Symbol}
120
167
# no default method
0 commit comments