Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: sdk/lib/io/data_transformer.dart

Issue 22867033: Update ZLib/GZip in dart:io to be a Codec/Converter from dart:convert. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/io_natives.cc ('k') | sdk/lib/io/http_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
8 const ZLIB = const ZLibCodec();
floitsch 2013/08/22 15:04:05 add comments.
Søren Gjesse 2013/08/22 15:10:37 Documentation for the ZLIB object?
Anders Johnsen 2013/08/22 15:58:01 Done.
Anders Johnsen 2013/08/22 15:58:01 Done.
9
10
11 class ZLibCodec extends Codec<List<int>, List<int>> {
floitsch 2013/08/22 15:04:05 add comments and for all other classes / methods.
Søren Gjesse 2013/08/22 15:10:37 And for the codec.
Anders Johnsen 2013/08/22 15:58:01 Done.
Anders Johnsen 2013/08/22 15:58:01 Done.
12 final Converter<List<int>, List<int>> encoder =
13 const ZLibEncoder(gzip: false);
14 final Converter<List<int>, List<int>> decoder = const ZLibDecoder();
15
16 const ZLibCodec();
17 }
18
19
20 const GZIP = const GZipCodec();
Søren Gjesse 2013/08/22 15:10:37 Ditto.
Anders Johnsen 2013/08/22 15:58:01 Done.
21
22
23 class GZipCodec extends Codec<List<int>, List<int>> {
Søren Gjesse 2013/08/22 15:10:37 Ditto.
Anders Johnsen 2013/08/22 15:58:01 Done.
24 final Converter<List<int>, List<int>> encoder = const ZLibEncoder(gzip: true);
25 final Converter<List<int>, List<int>> decoder = const ZLibDecoder();
26
27 const GZipCodec();
28 }
29
30
31 class ZLibEncoder extends Converter<List<int>, List<int>> {
floitsch 2013/08/22 15:04:05 Still misses the synchronous convert function. You
Anders Johnsen 2013/08/22 15:58:01 Done.
32 final bool gzip;
33 final int level;
34
35 /**
36 * Create a new [ZLibEncoder] converter. If the [gzip] flag is set, the
37 * encoder will wrap the encoded ZLib data in GZip frames.
38 */
39 const ZLibEncoder({this.gzip: false, this.level: 6});
40
41 ByteConversionSink startChunkedConversion(
floitsch 2013/08/22 15:04:05 Comment explaining that the function is more effic
Anders Johnsen 2013/08/22 15:58:01 Done.
42 ChunkedConversionSink<List<int>> sink) {
43 if (sink is! ByteConversionSink) {
44 sink = new ByteConversionSink.from(sink);
45 }
46 return new _ZLibEncoderSink(sink, gzip, level);
47 }
48 }
49
50
51 class ZLibDecoder extends Converter<List<int>, List<int>> {
Søren Gjesse 2013/08/22 15:10:37 Add comment for this constructor as well. Currentl
Anders Johnsen 2013/08/22 15:58:01 Done.
52 const ZLibDecoder();
53
54 ByteConversionSink startChunkedConversion(
55 ChunkedConversionSink<List<int>> sink) {
56 if (sink is! ByteConversionSink) {
57 sink = new ByteConversionSink.from(sink);
58 }
59 return new _ZLibDecoderSink(sink);
60 }
61 }
62
63
64 class _ZLibEncoderSink extends _FilterSink {
65 _ZLibEncoderSink(ByteConversionSink sink, bool gzip, int level)
66 : super(sink, _Filter.newZLibDeflateFilter(gzip, level));
67 }
68
69
70 class _ZLibDecoderSink extends _FilterSink {
71 _ZLibDecoderSink(ByteConversionSink sink)
72 : super(sink, _Filter.newZLibInflateFilter());
73 }
74
75
76 class _FilterSink extends ByteConversionSink {
77 final _Filter _filter;
78 final ByteConversionSink _sink;
79 bool _closed = false;
80 bool _empty = true;
81
82 _FilterSink(ByteConversionSink this._sink, _Filter this._filter);
83
84 void add(List<int> data) {
85 addSlice(data, 0, data.length, false);
86 }
87
88 void addSlice(List<int> data, int start, int end, bool isLast) {
89 if (_closed) return;
90 if (start < 0 || start > data.length) {
91 throw ArgumentError("Invalid start position");
92 }
93 if (end < 0 || end > data.length || end < start) {
94 throw ArgumentError("Invalid end position");
95 }
96 try {
97 _empty = false;
98 _filter.process(data, start, end);
99 var out;
100 while ((out = _filter.processed(flush: false)) != null) {
101 _sink.add(out);
102 }
103 } catch (e) {
104 _closed = true;
105 throw e;
106 }
107
108 if (isLast) close();
109 }
110
111 void close() {
112 if (_closed) return;
113 if (_empty) _filter.process(const [], 0, 0);
floitsch 2013/08/22 15:04:05 can't you replace this and the following lines wit
Anders Johnsen 2013/08/22 15:58:01 Added comment. And no, since we don't want to writ
114 try {
115 var out;
116 while ((out = _filter.processed(end: true)) != null) {
117 _sink.add(out);
118 }
119 } catch (e) {
120 _closed = true;
121 throw e;
122 }
123 if (!_closed) _filter.end();
124 _closed = true;
125 _sink.close();
126 }
127 }
128
129
130
7 /** 131 /**
8 * Private helper-class to handle native filters. 132 * Private helper-class to handle native filters.
9 */ 133 */
10 abstract class _Filter { 134 abstract class _Filter {
11 /** 135 /**
12 * Call to process a chunk of data. A call to [process] should only be made 136 * Call to process a chunk of data. A call to [process] should only be made
13 * when [processed] returns [null]. 137 * when [processed] returns [null].
14 */ 138 */
15 void process(List<int> data); 139 void process(List<int> data, int start, int end);
16 140
17 /** 141 /**
18 * Get a chunk of processed data. When there are no more data available, 142 * Get a chunk of processed data. When there are no more data available,
19 * [processed] will return [null]. Set [flush] to [false] for non-final 143 * [processed] will return [null]. Set [flush] to [false] for non-final
20 * calls to improve performance of some filters. 144 * calls to improve performance of some filters.
21 * 145 *
22 * The last call to [processed] should have [end] set to [true]. This will mak e 146 * The last call to [processed] should have [end] set to [true]. This will mak e
23 * sure a 'end' packet is written on the stream. 147 * sure a 'end' packet is written on the stream.
24 */ 148 */
25 List<int> processed({bool flush: true, bool end: false}); 149 List<int> processed({bool flush: true, bool end: false});
26 150
27 /** 151 /**
28 * Mark the filter as closed. Always call this method for any filter created 152 * Mark the filter as closed. Always call this method for any filter created
29 * to avoid leaking resources. [end] can be called at any time, but any 153 * to avoid leaking resources. [end] can be called at any time, but any
30 * successive calls to [process] or [processed] will fail. 154 * successive calls to [process] or [processed] will fail.
31 */ 155 */
32 void end(); 156 void end();
33 157
34 external static _Filter newZLibDeflateFilter(bool gzip, int level); 158 external static _Filter newZLibDeflateFilter(bool gzip, int level);
35 external static _Filter newZLibInflateFilter(); 159 external static _Filter newZLibInflateFilter();
36 } 160 }
37
38
39 class _FilterTransformer extends StreamEventTransformer<List<int>, List<int>> {
40 final _Filter _filter;
41 bool _closed = false;
42 bool _empty = true;
43
44 _FilterTransformer(_Filter this._filter);
45
46 void handleData(List<int> data, EventSink<List<int>> sink) {
47 if (_closed) return;
48 try {
49 _empty = false;
50 _filter.process(data);
51 var out;
52 while ((out = _filter.processed(flush: false)) != null) {
53 sink.add(out);
54 }
55 } catch (e, s) {
56 _closed = true;
57 // TODO(floitsch): we are losing the stack trace.
58 sink.addError(e);
59 sink.close();
60 }
61 }
62
63 void handleDone(EventSink<List<int>> sink) {
64 if (_closed) return;
65 if (_empty) _filter.process(const []);
66 try {
67 var out;
68 while ((out = _filter.processed(end: true)) != null) {
69 sink.add(out);
70 }
71 } catch (e, s) {
72 // TODO(floitsch): we are losing the stack trace.
73 sink.addError(e);
74 _closed = true;
75 }
76 if (!_closed) _filter.end();
77 _closed = true;
78 sink.close();
79 }
80 }
81
82
83 /**
84 * ZLibDeflater class used to deflate a stream of bytes, using zlib.
85 */
86 class ZLibDeflater extends _FilterTransformer {
87 ZLibDeflater({bool gzip: true, int level: 6})
88 : super(_Filter.newZLibDeflateFilter(gzip, level));
89 }
90
91
92 /**
93 * ZLibInflater class used to inflate a stream of bytes, using zlib.
94 */
95 class ZLibInflater extends _FilterTransformer {
96 ZLibInflater() : super(_Filter.newZLibInflateFilter());
97 }
98
OLDNEW
« no previous file with comments | « runtime/bin/io_natives.cc ('k') | sdk/lib/io/http_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698