Chromium Code Reviews| 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 final bool raw; | |
| 33 | |
| 34 /** | |
| 25 * Get a [Converter] for encoding to `ZLib` compressed data. | 35 * Get a [Converter] for encoding to `ZLib` compressed data. |
| 26 */ | 36 */ |
| 27 Converter<List<int>, List<int>> get encoder => | 37 Converter<List<int>, List<int>> get encoder => |
| 28 new ZLibEncoder(gzip: false, level: level); | 38 new ZLibEncoder(gzip: false, level: level, windowBits: windowBits, |
| 39 raw: raw); | |
| 29 | 40 |
| 30 /** | 41 /** |
| 31 * Get a [Converter] for decoding `ZLib` compressed data. | 42 * Get a [Converter] for decoding `ZLib` compressed data. |
| 32 */ | 43 */ |
| 33 Converter<List<int>, List<int>> get decoder => const ZLibDecoder(); | 44 Converter<List<int>, List<int>> get decoder => new ZLibDecoder( |
| 45 windowBits: windowBits, raw: raw); | |
| 34 | 46 |
| 35 /** | 47 /** |
| 36 * The compression-[level] can be set in the range of `1..10`, with `6` being | 48 * 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 | 49 * 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 | 50 * 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. | 51 * less CPU and memory, but at the cost of lower compression rates. |
| 40 */ | 52 */ |
| 41 const ZLibCodec({this.level: 6}); | 53 const ZLibCodec({this.level: 6, this.windowBits: 15, this.raw: false}); |
|
vicb
2014/01/09 09:48:27
Adding some checks here would imply removing the '
| |
| 42 } | 54 } |
| 43 | 55 |
| 44 | 56 |
| 45 /** | 57 /** |
| 46 * An instance of the default implementation of the [GZipCodec]. | 58 * An instance of the default implementation of the [GZipCodec]. |
| 47 */ | 59 */ |
| 48 const GZipCodec GZIP = const GZipCodec(); | 60 const GZipCodec GZIP = const GZipCodec(); |
| 49 | 61 |
| 50 | 62 |
| 51 /** | 63 /** |
| 52 * The [GZipCodec] encodes raw bytes to GZip compressed bytes and decodes GZip | 64 * The [GZipCodec] encodes raw bytes to GZip compressed bytes and decodes GZip |
| 53 * compressed bytes to raw bytes. | 65 * compressed bytes to raw bytes. |
| 54 * | 66 * |
| 55 * The difference between [ZLibCodec] and [GZipCodec] is that the [GZipCodec] | 67 * The difference between [ZLibCodec] and [GZipCodec] is that the [GZipCodec] |
| 56 * wraps the `ZLib` compressed bytes in `GZip` frames. | 68 * wraps the `ZLib` compressed bytes in `GZip` frames. |
| 57 */ | 69 */ |
| 58 class GZipCodec extends Codec<List<int>, List<int>> { | 70 class GZipCodec extends Codec<List<int>, List<int>> { |
| 59 /** | 71 /** |
| 60 * The compression level of the [ZLibCodec]. | 72 * The compression level of the [ZLibCodec]. |
| 61 */ | 73 */ |
| 62 final int level; | 74 final int level; |
| 63 | 75 |
| 64 /** | 76 /** |
| 77 * The windowBits parameter is the base two logarithm of the window size (the | |
| 78 * size of the history buffer). It should be in the range 8..15. Larger values | |
| 79 * of this parameter result in better compression at the expense of memory | |
| 80 * usage. The default value is 15 | |
| 81 */ | |
| 82 final int windowBits; | |
| 83 | |
| 84 final bool raw; | |
| 85 | |
| 86 /** | |
| 65 * Get a [Converter] for encoding to `GZip` compressed data. | 87 * Get a [Converter] for encoding to `GZip` compressed data. |
| 66 */ | 88 */ |
| 67 Converter<List<int>, List<int>> get encoder => | 89 Converter<List<int>, List<int>> get encoder => |
| 68 new ZLibEncoder(gzip: true, level: level); | 90 new ZLibEncoder(gzip: true, level: level, windowBits: windowBits, |
| 91 raw: raw); | |
| 69 | 92 |
| 70 /** | 93 /** |
| 71 * Get a [Converter] for decoding `GZip` compressed data. | 94 * Get a [Converter] for decoding `GZip` compressed data. |
| 72 */ | 95 */ |
| 73 Converter<List<int>, List<int>> get decoder => const ZLibDecoder(); | 96 Converter<List<int>, List<int>> get decoder => new ZLibDecoder( |
| 97 windowBits: windowBits, raw: raw); | |
| 74 | 98 |
| 75 /** | 99 /** |
| 76 * The compression-[level] can be set in the range of `1..10`, with `6` being | 100 * 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 | 101 * 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 | 102 * 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. | 103 * `6` will use less CPU and memory, but at the cost of lower compression |
| 104 * rates. | |
| 80 */ | 105 */ |
| 81 const GZipCodec({this.level: 6}); | 106 const GZipCodec({this.level: 6, this.windowBits: 15, this.raw: false}); |
| 82 } | 107 } |
| 83 | 108 |
| 84 | 109 |
| 85 /** | 110 /** |
| 86 * The [ZLibEncoder] is the encoder used by [ZLibCodec] and [GZipCodec] to | 111 * The [ZLibEncoder] is the encoder used by [ZLibCodec] and [GZipCodec] to |
| 87 * compress data. | 112 * compress data. |
| 88 */ | 113 */ |
| 89 class ZLibEncoder extends Converter<List<int>, List<int>> { | 114 class ZLibEncoder extends Converter<List<int>, List<int>> { |
| 90 /** | 115 /** |
| 91 * If [gzip] is true, `GZip` frames will be added to the compressed data. | 116 * If [gzip] is true, `GZip` frames will be added to the compressed data. |
| 92 */ | 117 */ |
| 93 final bool gzip; | 118 final bool gzip; |
| 94 | 119 |
| 95 /** | 120 /** |
| 96 * The compression level used by the encoder. | 121 * The compression level used by the encoder. |
| 97 */ | 122 */ |
| 98 final int level; | 123 final int level; |
| 99 | 124 |
| 100 /** | 125 /** |
| 126 * The windowBits parameter is the base two logarithm of the window size (the | |
| 127 * size of the history buffer). It should be in the range 8..15. Larger values | |
| 128 * of this parameter result in better compression at the expense of memory | |
| 129 * usage. The default value is 15 | |
| 130 */ | |
| 131 final int windowBits; | |
| 132 | |
| 133 final bool raw; | |
| 134 | |
| 135 /** | |
| 101 * Create a new [ZLibEncoder] converter. If the [gzip] flag is set, the | 136 * Create a new [ZLibEncoder] converter. If the [gzip] flag is set, the |
| 102 * encoder will wrap the encoded ZLib data in GZip frames. | 137 * encoder will wrap the encoded ZLib data in GZip frames. |
| 103 */ | 138 */ |
| 104 const ZLibEncoder({this.gzip: false, this.level: 6}); | 139 const ZLibEncoder({this.gzip: false, this.level: 6, this.windowBits: 15, |
| 105 | 140 this.raw: false}); |
| 106 | 141 |
| 107 /** | 142 /** |
| 108 * Convert a list of bytes using the options given to the [ZLibEncoder] | 143 * Convert a list of bytes using the options given to the [ZLibEncoder] |
| 109 * constructor. | 144 * constructor. |
| 110 */ | 145 */ |
| 111 List<int> convert(List<int> bytes) { | 146 List<int> convert(List<int> bytes) { |
| 112 _BufferSink sink = new _BufferSink(); | 147 _BufferSink sink = new _BufferSink(); |
| 113 startChunkedConversion(sink) | 148 startChunkedConversion(sink) |
| 114 ..add(bytes) | 149 ..add(bytes) |
| 115 ..close(); | 150 ..close(); |
| 116 return sink.builder.takeBytes(); | 151 return sink.builder.takeBytes(); |
| 117 } | 152 } |
| 118 | 153 |
| 119 /** | 154 /** |
| 120 * Start a chunked conversion using the options given to the [ZLibEncoder] | 155 * Start a chunked conversion using the options given to the [ZLibEncoder] |
| 121 * constructor. While it accepts any [ChunkedConversionSink] taking | 156 * constructor. While it accepts any [ChunkedConversionSink] taking |
| 122 * [List<int>]'s, the optimal sink to be passed as [sink] is a | 157 * [List<int>]'s, the optimal sink to be passed as [sink] is a |
| 123 * [ByteConversionSink]. | 158 * [ByteConversionSink]. |
| 124 */ | 159 */ |
| 125 ByteConversionSink startChunkedConversion( | 160 ByteConversionSink startChunkedConversion( |
| 126 ChunkedConversionSink<List<int>> sink) { | 161 ChunkedConversionSink<List<int>> sink) { |
| 127 if (sink is! ByteConversionSink) { | 162 if (sink is! ByteConversionSink) { |
| 128 sink = new ByteConversionSink.from(sink); | 163 sink = new ByteConversionSink.from(sink); |
| 129 } | 164 } |
| 130 return new _ZLibEncoderSink(sink, gzip, level); | 165 return new _ZLibEncoderSink(sink, gzip, level, windowBits, raw); |
| 131 } | 166 } |
| 132 } | 167 } |
| 133 | 168 |
| 134 | 169 |
| 135 /** | 170 /** |
| 136 * The [ZLibDecoder] is the decoder used by [ZLibCodec] and [GZipCodec] to | 171 * The [ZLibDecoder] is the decoder used by [ZLibCodec] and [GZipCodec] to |
| 137 * decompress data. | 172 * decompress data. |
| 138 */ | 173 */ |
| 139 class ZLibDecoder extends Converter<List<int>, List<int>> { | 174 class ZLibDecoder extends Converter<List<int>, List<int>> { |
| 175 /** | |
| 176 * The windowBits parameter is the base two logarithm of the window size (the | |
| 177 * size of the history buffer). It should be in the range 8..15. Larger values | |
| 178 * of this parameter result in better compression at the expense of memory | |
| 179 * usage. The default value is 15 | |
| 180 */ | |
| 181 final int windowBits; | |
| 182 | |
| 183 final bool raw; | |
| 140 | 184 |
| 141 /** | 185 /** |
| 142 * Create a new [ZLibEncoder] converter. | 186 * Create a new [ZLibDecoder] converter. |
| 143 */ | 187 */ |
| 144 const ZLibDecoder(); | 188 const ZLibDecoder({this.windowBits: 15, this.raw: false}); |
| 145 | 189 |
| 146 /** | 190 /** |
| 147 * Convert a list of bytes using the options given to the [ZLibDecoder] | 191 * Convert a list of bytes using the options given to the [ZLibDecoder] |
| 148 * constructor. | 192 * constructor. |
| 149 */ | 193 */ |
| 150 List<int> convert(List<int> bytes) { | 194 List<int> convert(List<int> bytes) { |
| 151 _BufferSink sink = new _BufferSink(); | 195 _BufferSink sink = new _BufferSink(); |
| 152 startChunkedConversion(sink) | 196 startChunkedConversion(sink) |
| 153 ..add(bytes) | 197 ..add(bytes) |
| 154 ..close(); | 198 ..close(); |
| 155 return sink.builder.takeBytes(); | 199 return sink.builder.takeBytes(); |
| 156 } | 200 } |
| 157 | 201 |
| 158 /** | 202 /** |
| 159 * Start a chunked conversion. While it accepts any [ChunkedConversionSink] | 203 * Start a chunked conversion. While it accepts any [ChunkedConversionSink] |
| 160 * taking [List<int>]'s, the optimal sink to be passed as [sink] is a | 204 * taking [List<int>]'s, the optimal sink to be passed as [sink] is a |
| 161 * [ByteConversionSink]. | 205 * [ByteConversionSink]. |
| 162 */ | 206 */ |
| 163 ByteConversionSink startChunkedConversion( | 207 ByteConversionSink startChunkedConversion( |
| 164 ChunkedConversionSink<List<int>> sink) { | 208 ChunkedConversionSink<List<int>> sink) { |
| 165 if (sink is! ByteConversionSink) { | 209 if (sink is! ByteConversionSink) { |
| 166 sink = new ByteConversionSink.from(sink); | 210 sink = new ByteConversionSink.from(sink); |
| 167 } | 211 } |
| 168 return new _ZLibDecoderSink(sink); | 212 return new _ZLibDecoderSink(sink, windowBits, raw); |
| 169 } | 213 } |
| 170 } | 214 } |
| 171 | 215 |
| 172 | 216 |
| 173 class _BufferSink extends ByteConversionSink { | 217 class _BufferSink extends ByteConversionSink { |
| 174 final BytesBuilder builder = new BytesBuilder(); | 218 final BytesBuilder builder = new BytesBuilder(); |
| 175 | 219 |
| 176 void add(List<int> chunk) { | 220 void add(List<int> chunk) { |
| 177 builder.add(chunk); | 221 builder.add(chunk); |
| 178 } | 222 } |
| 179 | 223 |
| 180 void addSlice(List<int> chunk, int start, int end, bool isLast) { | 224 void addSlice(List<int> chunk, int start, int end, bool isLast) { |
| 181 if (chunk is Uint8List) { | 225 if (chunk is Uint8List) { |
| 182 Uint8List list = chunk; | 226 Uint8List list = chunk; |
| 183 builder.add(new Uint8List.view(list.buffer, start, end - start)); | 227 builder.add(new Uint8List.view(list.buffer, start, end - start)); |
| 184 } else { | 228 } else { |
| 185 builder.add(chunk.sublist(start, end)); | 229 builder.add(chunk.sublist(start, end)); |
| 186 } | 230 } |
| 187 } | 231 } |
| 188 | 232 |
| 189 void close() {} | 233 void close() {} |
| 190 } | 234 } |
| 191 | 235 |
| 192 | 236 |
| 193 class _ZLibEncoderSink extends _FilterSink { | 237 class _ZLibEncoderSink extends _FilterSink { |
| 194 _ZLibEncoderSink(ByteConversionSink sink, bool gzip, int level) | 238 _ZLibEncoderSink(ByteConversionSink sink, bool gzip, int level, |
| 195 : super(sink, _Filter.newZLibDeflateFilter(gzip, level)); | 239 int windowBits, bool raw) |
| 240 : super(sink, _Filter.newZLibDeflateFilter(gzip, level, windowBits, raw)); | |
| 196 } | 241 } |
| 197 | 242 |
| 198 | 243 |
| 199 class _ZLibDecoderSink extends _FilterSink { | 244 class _ZLibDecoderSink extends _FilterSink { |
| 200 _ZLibDecoderSink(ByteConversionSink sink) | 245 _ZLibDecoderSink(ByteConversionSink sink, int windowBits, bool raw) |
| 201 : super(sink, _Filter.newZLibInflateFilter()); | 246 : super(sink, _Filter.newZLibInflateFilter(windowBits, raw)); |
| 202 } | 247 } |
| 203 | 248 |
| 204 | 249 |
| 205 class _FilterSink extends ByteConversionSink { | 250 class _FilterSink extends ByteConversionSink { |
| 206 final _Filter _filter; | 251 final _Filter _filter; |
| 207 final ByteConversionSink _sink; | 252 final ByteConversionSink _sink; |
| 208 bool _closed = false; | 253 bool _closed = false; |
| 209 bool _empty = true; | 254 bool _empty = true; |
| 210 | 255 |
| 211 _FilterSink(ByteConversionSink this._sink, _Filter this._filter); | 256 _FilterSink(this._sink, this._filter); |
| 212 | 257 |
| 213 void add(List<int> data) { | 258 void add(List<int> data) { |
| 214 addSlice(data, 0, data.length, false); | 259 addSlice(data, 0, data.length, false); |
| 215 } | 260 } |
| 216 | 261 |
| 217 void addSlice(List<int> data, int start, int end, bool isLast) { | 262 void addSlice(List<int> data, int start, int end, bool isLast) { |
| 218 if (_closed) return; | 263 if (_closed) return; |
| 219 if (start < 0 || start > data.length) { | 264 if (start < 0 || start > data.length) { |
| 220 throw new ArgumentError("Invalid start position"); | 265 throw new ArgumentError("Invalid start position"); |
| 221 } | 266 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 */ | 324 */ |
| 280 List<int> processed({bool flush: true, bool end: false}); | 325 List<int> processed({bool flush: true, bool end: false}); |
| 281 | 326 |
| 282 /** | 327 /** |
| 283 * Mark the filter as closed. Always call this method for any filter created | 328 * 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 | 329 * to avoid leaking resources. [end] can be called at any time, but any |
| 285 * successive calls to [process] or [processed] will fail. | 330 * successive calls to [process] or [processed] will fail. |
| 286 */ | 331 */ |
| 287 void end(); | 332 void end(); |
| 288 | 333 |
| 289 external static _Filter newZLibDeflateFilter(bool gzip, int level); | 334 external static _Filter newZLibDeflateFilter(bool gzip, int level, |
| 290 external static _Filter newZLibInflateFilter(); | 335 int windowBits, bool raw); |
| 336 external static _Filter newZLibInflateFilter(int windowBits, bool raw); | |
| 291 } | 337 } |
| OLD | NEW |