OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 | 7 |
8 /** | 8 /** |
9 * An instance of the default implementation of the [ZLibCodec]. | 9 * An instance of the default implementation of the [ZLibCodec]. |
10 */ | 10 */ |
11 const ZLibCodec ZLIB = const ZLibCodec(); | 11 const ZLibCodec ZLIB = const ZLibCodec(); |
12 | 12 |
13 | 13 |
14 /** | 14 /** |
15 * The [ZLibCodec] encodes raw bytes to ZLib compressed bytes and decodes ZLib | 15 * The [ZLibCodec] encodes raw bytes to ZLib compressed bytes and decodes ZLib |
16 * compressed bytes to raw bytes. | 16 * compressed bytes to raw bytes. |
17 */ | 17 */ |
18 class ZLibCodec extends Codec<List<int>, List<int>> { | 18 class ZLibCodec extends Codec<List<int>, List<int>> { |
19 /** | 19 /** |
20 * The compression level of the [ZLibCodec]. | 20 * The compression level of the [ZLibCodec]. |
21 */ | 21 */ |
22 final int level; | 22 final int level; |
23 | 23 |
24 /** | 24 /** |
25 * The windowBits parameter is the base two logarithm of the window size (the | |
26 * size of the history buffer). It should be in the range 8..15. Larger values | |
27 * of this parameter result in better compression at the expense of memory | |
28 * usage. The default value is 15 | |
29 */ | |
30 final int windowBits; | |
31 | |
32 /** | |
33 * The [memLevel] parameter specifies how much memory should be allocated for | |
34 * the internal compression state. [memLevel]=1 uses minimum memory but is slow | |
35 * and reduces compression ratio; [memLevel]=9 uses maximum memory for optimal | |
36 * speed. The default value is 8 | |
37 * The memory requirements for deflate are (in bytes): | |
38 * (1 << (windowBits + 2)) + (1 << (memLevel + 9)) | |
39 * that is: 128K for windowBits = 15 + 128K for memLevel = 8 (default values) | |
40 */ | |
41 final int memLevel; | |
42 | |
43 /** | |
44 * When [raw] is true, deflate generates raw data with no zlib header or | |
45 * trailer, and will not compute an adler32 check value | |
46 */ | |
47 final bool raw; | |
48 | |
49 /** | |
25 * Get a [Converter] for encoding to `ZLib` compressed data. | 50 * Get a [Converter] for encoding to `ZLib` compressed data. |
26 */ | 51 */ |
27 Converter<List<int>, List<int>> get encoder => | 52 Converter<List<int>, List<int>> get encoder => |
28 new ZLibEncoder(gzip: false, level: level); | 53 new ZLibEncoder(gzip: false, level: level, windowBits: windowBits, |
54 raw: raw, memLevel: memLevel); | |
29 | 55 |
30 /** | 56 /** |
31 * Get a [Converter] for decoding `ZLib` compressed data. | 57 * Get a [Converter] for decoding `ZLib` compressed data. |
32 */ | 58 */ |
33 Converter<List<int>, List<int>> get decoder => const ZLibDecoder(); | 59 Converter<List<int>, List<int>> get decoder => new ZLibDecoder( |
60 windowBits: windowBits, raw: raw); | |
34 | 61 |
35 /** | 62 /** |
36 * The compression-[level] can be set in the range of `1..10`, with `6` being | 63 * The compression-[level] can be set in the range of `-1..9`, with `6` being |
37 * the default compression level. Levels above 6 will have higher compression | 64 * the default compression level. Levels above 6 will have higher compression |
38 * rates at the cost of more CPU and memory usage. Levels below 6 will use | 65 * rates at the cost of more CPU and memory usage. Levels below 6 will use |
39 * less CPU and memory, but at the cost of lower compression rates. | 66 * less CPU and memory, but at the cost of lower compression rates. |
40 */ | 67 */ |
41 const ZLibCodec({this.level: 6}); | 68 const ZLibCodec({this.level: 6, this.windowBits: 15, this.raw: false, |
Anders Johnsen
2014/01/15 10:52:08
Please add some constants to ZLibCodec, ZLIB_LEVEL
Anders Johnsen
2014/01/15 10:52:08
Make raw the last argument.
vicb
2014/01/15 11:08:04
That's in the pipe
vicb
2014/01/15 11:08:04
Could I ask why ? (is order important for named pa
Anders Johnsen
2014/01/15 11:34:17
Matter of taste I suppose :) `raw` is kind of diff
vicb
2014/01/23 09:14:04
I've moved raw to be the last parameter everywhere
| |
69 this.memLevel: 8}); | |
42 } | 70 } |
43 | 71 |
44 | 72 |
45 /** | 73 /** |
46 * An instance of the default implementation of the [GZipCodec]. | 74 * An instance of the default implementation of the [GZipCodec]. |
47 */ | 75 */ |
48 const GZipCodec GZIP = const GZipCodec(); | 76 const GZipCodec GZIP = const GZipCodec(); |
49 | 77 |
50 | 78 |
51 /** | 79 /** |
52 * The [GZipCodec] encodes raw bytes to GZip compressed bytes and decodes GZip | 80 * The [GZipCodec] encodes raw bytes to GZip compressed bytes and decodes GZip |
53 * compressed bytes to raw bytes. | 81 * compressed bytes to raw bytes. |
54 * | 82 * |
55 * The difference between [ZLibCodec] and [GZipCodec] is that the [GZipCodec] | 83 * The difference between [ZLibCodec] and [GZipCodec] is that the [GZipCodec] |
56 * wraps the `ZLib` compressed bytes in `GZip` frames. | 84 * wraps the `ZLib` compressed bytes in `GZip` frames. |
57 */ | 85 */ |
58 class GZipCodec extends Codec<List<int>, List<int>> { | 86 class GZipCodec extends Codec<List<int>, List<int>> { |
59 /** | 87 /** |
60 * The compression level of the [ZLibCodec]. | 88 * The compression level of the [ZLibCodec]. |
61 */ | 89 */ |
62 final int level; | 90 final int level; |
63 | 91 |
64 /** | 92 /** |
93 * The windowBits parameter is the base two logarithm of the window size (the | |
94 * size of the history buffer). It should be in the range 8..15. Larger values | |
95 * of this parameter result in better compression at the expense of memory | |
96 * usage. The default value is 15 | |
97 */ | |
98 final int windowBits; | |
99 | |
100 /** | |
101 * The [memLevel] parameter specifies how much memory should be allocated for | |
102 * the internal compression state. [memLevel]=1 uses minimum memory but is slow | |
103 * and reduces compression ratio; [memLevel]=9 uses maximum memory for optimal | |
104 * speed. The default value is 8 | |
105 * The memory requirements for deflate are (in bytes): | |
106 * (1 << (windowBits + 2)) + (1 << (memLevel + 9)) | |
107 * that is: 128K for windowBits = 15 + 128K for memLevel = 8 (default values) | |
108 */ | |
109 final int memLevel; | |
110 | |
111 /** | |
112 * When [raw] is true, deflate generates raw data with no zlib header or | |
113 * trailer, and will not compute an adler32 check value | |
114 */ | |
115 final bool raw; | |
116 | |
117 /** | |
65 * Get a [Converter] for encoding to `GZip` compressed data. | 118 * Get a [Converter] for encoding to `GZip` compressed data. |
66 */ | 119 */ |
67 Converter<List<int>, List<int>> get encoder => | 120 Converter<List<int>, List<int>> get encoder => |
68 new ZLibEncoder(gzip: true, level: level); | 121 new ZLibEncoder(gzip: true, level: level, windowBits: windowBits, |
122 raw: raw, memLevel: memLevel); | |
69 | 123 |
70 /** | 124 /** |
71 * Get a [Converter] for decoding `GZip` compressed data. | 125 * Get a [Converter] for decoding `GZip` compressed data. |
72 */ | 126 */ |
73 Converter<List<int>, List<int>> get decoder => const ZLibDecoder(); | 127 Converter<List<int>, List<int>> get decoder => new ZLibDecoder( |
128 windowBits: windowBits, raw: raw); | |
74 | 129 |
75 /** | 130 /** |
76 * The compression-[level] can be set in the range of `1..10`, with `6` being | 131 * The compression-[level] can be set in the range of `-1..9`, with `6` being |
77 * the default compression level. Levels above 6 will have higher compression | 132 * the default compression level. Levels above `6` will have higher |
78 * rates at the cost of more CPU and memory usage. Levels below 6 will use | 133 * compression rates at the cost of more CPU and memory usage. Levels below |
79 * less CPU and memory, but at the cost of lower compression rates. | 134 * `6` will use less CPU and memory, but at the cost of lower compression |
135 * rates. | |
80 */ | 136 */ |
81 const GZipCodec({this.level: 6}); | 137 const GZipCodec({this.level: 6, this.windowBits: 15, this.raw: false, |
138 this.memLevel: 8}); | |
82 } | 139 } |
83 | 140 |
84 | 141 |
85 /** | 142 /** |
86 * The [ZLibEncoder] is the encoder used by [ZLibCodec] and [GZipCodec] to | 143 * The [ZLibEncoder] is the encoder used by [ZLibCodec] and [GZipCodec] to |
87 * compress data. | 144 * compress data. |
88 */ | 145 */ |
89 class ZLibEncoder extends Converter<List<int>, List<int>> { | 146 class ZLibEncoder extends Converter<List<int>, List<int>> { |
90 /** | 147 /** |
91 * If [gzip] is true, `GZip` frames will be added to the compressed data. | 148 * If [gzip] is true, `GZip` frames will be added to the compressed data. |
92 */ | 149 */ |
93 final bool gzip; | 150 final bool gzip; |
94 | 151 |
95 /** | 152 /** |
96 * The compression level used by the encoder. | 153 * The compression level used by the encoder. |
97 */ | 154 */ |
98 final int level; | 155 final int level; |
99 | 156 |
100 /** | 157 /** |
158 * The windowBits parameter is the base two logarithm of the window size (the | |
159 * size of the history buffer). It should be in the range 8..15. Larger values | |
160 * of this parameter result in better compression at the expense of memory | |
161 * usage. The default value is 15 | |
162 */ | |
163 final int windowBits; | |
164 | |
165 /** | |
166 * The [memLevel] parameter specifies how much memory should be allocated for | |
167 * the internal compression state. [memLevel]=1 uses minimum memory but is slow | |
168 * and reduces compression ratio; [memLevel]=9 uses maximum memory for optimal | |
169 * speed. The default value is 8 | |
170 * The memory requirements for deflate are (in bytes): | |
171 * (1 << (windowBits + 2)) + (1 << (memLevel + 9)) | |
172 * that is: 128K for windowBits = 15 + 128K for memLevel = 8 (default values) | |
173 */ | |
174 final int memLevel; | |
175 | |
176 /** | |
177 * When [raw] is true, deflate generates raw data with no zlib header or | |
178 * trailer, and will not compute an adler32 check value | |
179 */ | |
180 final bool raw; | |
181 | |
182 /** | |
101 * Create a new [ZLibEncoder] converter. If the [gzip] flag is set, the | 183 * Create a new [ZLibEncoder] converter. If the [gzip] flag is set, the |
102 * encoder will wrap the encoded ZLib data in GZip frames. | 184 * encoder will wrap the encoded ZLib data in GZip frames. |
103 */ | 185 */ |
104 const ZLibEncoder({this.gzip: false, this.level: 6}); | 186 const ZLibEncoder({this.gzip: false, this.level: 6, this.windowBits: 15, |
105 | 187 this.raw: false, this.memLevel: 8}); |
106 | 188 |
107 /** | 189 /** |
108 * Convert a list of bytes using the options given to the [ZLibEncoder] | 190 * Convert a list of bytes using the options given to the [ZLibEncoder] |
109 * constructor. | 191 * constructor. |
110 */ | 192 */ |
111 List<int> convert(List<int> bytes) { | 193 List<int> convert(List<int> bytes) { |
112 _BufferSink sink = new _BufferSink(); | 194 _BufferSink sink = new _BufferSink(); |
113 startChunkedConversion(sink) | 195 startChunkedConversion(sink) |
114 ..add(bytes) | 196 ..add(bytes) |
115 ..close(); | 197 ..close(); |
116 return sink.builder.takeBytes(); | 198 return sink.builder.takeBytes(); |
117 } | 199 } |
118 | 200 |
119 /** | 201 /** |
120 * Start a chunked conversion using the options given to the [ZLibEncoder] | 202 * Start a chunked conversion using the options given to the [ZLibEncoder] |
121 * constructor. While it accepts any [ChunkedConversionSink] taking | 203 * constructor. While it accepts any [ChunkedConversionSink] taking |
122 * [List<int>]'s, the optimal sink to be passed as [sink] is a | 204 * [List<int>]'s, the optimal sink to be passed as [sink] is a |
123 * [ByteConversionSink]. | 205 * [ByteConversionSink]. |
124 */ | 206 */ |
125 ByteConversionSink startChunkedConversion( | 207 ByteConversionSink startChunkedConversion( |
126 ChunkedConversionSink<List<int>> sink) { | 208 ChunkedConversionSink<List<int>> sink) { |
127 if (sink is! ByteConversionSink) { | 209 if (sink is! ByteConversionSink) { |
128 sink = new ByteConversionSink.from(sink); | 210 sink = new ByteConversionSink.from(sink); |
129 } | 211 } |
130 return new _ZLibEncoderSink(sink, gzip, level); | 212 return new _ZLibEncoderSink(sink, gzip, level, windowBits, raw, memLevel); |
131 } | 213 } |
132 } | 214 } |
133 | 215 |
134 | 216 |
135 /** | 217 /** |
136 * The [ZLibDecoder] is the decoder used by [ZLibCodec] and [GZipCodec] to | 218 * The [ZLibDecoder] is the decoder used by [ZLibCodec] and [GZipCodec] to |
137 * decompress data. | 219 * decompress data. |
138 */ | 220 */ |
139 class ZLibDecoder extends Converter<List<int>, List<int>> { | 221 class ZLibDecoder extends Converter<List<int>, List<int>> { |
222 /** | |
223 * The windowBits parameter is the base two logarithm of the window size (the | |
224 * size of the history buffer). It should be in the range 8..15. Larger values | |
225 * of this parameter result in better compression at the expense of memory | |
226 * usage. The default value is 15 | |
227 */ | |
228 final int windowBits; | |
140 | 229 |
141 /** | 230 /** |
142 * Create a new [ZLibEncoder] converter. | 231 * When [raw] is true, deflate generates raw data with no zlib header or |
232 * trailer, and will not compute an adler32 check value | |
233 */ | |
234 final bool raw; | |
235 | |
236 /** | |
237 * Create a new [ZLibDecoder] converter. | |
143 */ | 238 */ |
144 const ZLibDecoder(); | 239 const ZLibDecoder({this.windowBits: 15, this.raw: false}); |
145 | 240 |
146 /** | 241 /** |
147 * Convert a list of bytes using the options given to the [ZLibDecoder] | 242 * Convert a list of bytes using the options given to the [ZLibDecoder] |
148 * constructor. | 243 * constructor. |
149 */ | 244 */ |
150 List<int> convert(List<int> bytes) { | 245 List<int> convert(List<int> bytes) { |
151 _BufferSink sink = new _BufferSink(); | 246 _BufferSink sink = new _BufferSink(); |
152 startChunkedConversion(sink) | 247 startChunkedConversion(sink) |
153 ..add(bytes) | 248 ..add(bytes) |
154 ..close(); | 249 ..close(); |
155 return sink.builder.takeBytes(); | 250 return sink.builder.takeBytes(); |
156 } | 251 } |
157 | 252 |
158 /** | 253 /** |
159 * Start a chunked conversion. While it accepts any [ChunkedConversionSink] | 254 * Start a chunked conversion. While it accepts any [ChunkedConversionSink] |
160 * taking [List<int>]'s, the optimal sink to be passed as [sink] is a | 255 * taking [List<int>]'s, the optimal sink to be passed as [sink] is a |
161 * [ByteConversionSink]. | 256 * [ByteConversionSink]. |
162 */ | 257 */ |
163 ByteConversionSink startChunkedConversion( | 258 ByteConversionSink startChunkedConversion( |
164 ChunkedConversionSink<List<int>> sink) { | 259 ChunkedConversionSink<List<int>> sink) { |
165 if (sink is! ByteConversionSink) { | 260 if (sink is! ByteConversionSink) { |
166 sink = new ByteConversionSink.from(sink); | 261 sink = new ByteConversionSink.from(sink); |
167 } | 262 } |
168 return new _ZLibDecoderSink(sink); | 263 return new _ZLibDecoderSink(sink, windowBits, raw); |
169 } | 264 } |
170 } | 265 } |
171 | 266 |
172 | 267 |
173 class _BufferSink extends ByteConversionSink { | 268 class _BufferSink extends ByteConversionSink { |
174 final BytesBuilder builder = new BytesBuilder(); | 269 final BytesBuilder builder = new BytesBuilder(); |
175 | 270 |
176 void add(List<int> chunk) { | 271 void add(List<int> chunk) { |
177 builder.add(chunk); | 272 builder.add(chunk); |
178 } | 273 } |
179 | 274 |
180 void addSlice(List<int> chunk, int start, int end, bool isLast) { | 275 void addSlice(List<int> chunk, int start, int end, bool isLast) { |
181 if (chunk is Uint8List) { | 276 if (chunk is Uint8List) { |
182 Uint8List list = chunk; | 277 Uint8List list = chunk; |
183 builder.add(new Uint8List.view(list.buffer, start, end - start)); | 278 builder.add(new Uint8List.view(list.buffer, start, end - start)); |
184 } else { | 279 } else { |
185 builder.add(chunk.sublist(start, end)); | 280 builder.add(chunk.sublist(start, end)); |
186 } | 281 } |
187 } | 282 } |
188 | 283 |
189 void close() {} | 284 void close() {} |
190 } | 285 } |
191 | 286 |
192 | 287 |
193 class _ZLibEncoderSink extends _FilterSink { | 288 class _ZLibEncoderSink extends _FilterSink { |
194 _ZLibEncoderSink(ByteConversionSink sink, bool gzip, int level) | 289 _ZLibEncoderSink(ByteConversionSink sink, bool gzip, int level, |
195 : super(sink, _Filter.newZLibDeflateFilter(gzip, level)); | 290 int windowBits, bool raw, int memLevel) |
291 : super(sink, _Filter.newZLibDeflateFilter(gzip, level, windowBits, raw, | |
292 memLevel)); | |
196 } | 293 } |
197 | 294 |
198 | 295 |
199 class _ZLibDecoderSink extends _FilterSink { | 296 class _ZLibDecoderSink extends _FilterSink { |
200 _ZLibDecoderSink(ByteConversionSink sink) | 297 _ZLibDecoderSink(ByteConversionSink sink, int windowBits, bool raw) |
201 : super(sink, _Filter.newZLibInflateFilter()); | 298 : super(sink, _Filter.newZLibInflateFilter(windowBits, raw)); |
202 } | 299 } |
203 | 300 |
204 | 301 |
205 class _FilterSink extends ByteConversionSink { | 302 class _FilterSink extends ByteConversionSink { |
206 final _Filter _filter; | 303 final _Filter _filter; |
207 final ByteConversionSink _sink; | 304 final ByteConversionSink _sink; |
208 bool _closed = false; | 305 bool _closed = false; |
209 bool _empty = true; | 306 bool _empty = true; |
210 | 307 |
211 _FilterSink(ByteConversionSink this._sink, _Filter this._filter); | 308 _FilterSink(this._sink, this._filter); |
212 | 309 |
213 void add(List<int> data) { | 310 void add(List<int> data) { |
214 addSlice(data, 0, data.length, false); | 311 addSlice(data, 0, data.length, false); |
215 } | 312 } |
216 | 313 |
217 void addSlice(List<int> data, int start, int end, bool isLast) { | 314 void addSlice(List<int> data, int start, int end, bool isLast) { |
218 if (_closed) return; | 315 if (_closed) return; |
219 if (start < 0 || start > data.length) { | 316 if (start < 0 || start > data.length) { |
220 throw new ArgumentError("Invalid start position"); | 317 throw new ArgumentError("Invalid start position"); |
221 } | 318 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 */ | 376 */ |
280 List<int> processed({bool flush: true, bool end: false}); | 377 List<int> processed({bool flush: true, bool end: false}); |
281 | 378 |
282 /** | 379 /** |
283 * Mark the filter as closed. Always call this method for any filter created | 380 * Mark the filter as closed. Always call this method for any filter created |
284 * to avoid leaking resources. [end] can be called at any time, but any | 381 * to avoid leaking resources. [end] can be called at any time, but any |
285 * successive calls to [process] or [processed] will fail. | 382 * successive calls to [process] or [processed] will fail. |
286 */ | 383 */ |
287 void end(); | 384 void end(); |
288 | 385 |
289 external static _Filter newZLibDeflateFilter(bool gzip, int level); | 386 external static _Filter newZLibDeflateFilter(bool gzip, int level, |
290 external static _Filter newZLibInflateFilter(); | 387 int windowBits, bool raw, |
388 int memLevel); | |
389 external static _Filter newZLibInflateFilter(int windowBits, bool raw); | |
291 } | 390 } |
OLD | NEW |