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.convert; | 5 part of dart.convert; |
6 | 6 |
7 /** | 7 /** |
8 * This class provides an interface for converters to | 8 * This class provides an interface for converters to |
9 * efficiently transmit String data. | 9 * efficiently transmit String data. |
10 * | 10 * |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 /** | 86 /** |
87 * This class wraps an existing [StringSink] and invokes a | 87 * This class wraps an existing [StringSink] and invokes a |
88 * closure when [close] is invoked. | 88 * closure when [close] is invoked. |
89 */ | 89 */ |
90 class _ClosableStringSink implements ClosableStringSink { | 90 class _ClosableStringSink implements ClosableStringSink { |
91 final _StringSinkCloseCallback _callback; | 91 final _StringSinkCloseCallback _callback; |
92 final StringSink _sink; | 92 final StringSink _sink; |
93 | 93 |
94 _ClosableStringSink(this._sink, this._callback); | 94 _ClosableStringSink(this._sink, this._callback); |
95 | 95 |
96 void close() => _callback(); | 96 void close() { _callback(); } |
97 | 97 |
98 void writeCharCode(int charCode) => _sink.writeCharCode(charCode); | 98 void writeCharCode(int charCode) { _sink.writeCharCode(charCode); } |
99 void write(Object o) => _sink.write(o); | 99 void write(Object o) { _sink.write(o); } |
100 void writeln([Object o = ""]) => _sink.writeln(o); | 100 void writeln([Object o = ""]) { _sink.writeln(o); } |
101 void writeAll(Iterable objects, [String separator = ""]) | 101 void writeAll(Iterable objects, [String separator = ""]) { |
102 => _sink.writeAll(objects, separator); | 102 _sink.writeAll(objects, separator); |
| 103 } |
103 } | 104 } |
104 | 105 |
105 /** | 106 /** |
106 * This class wraps an existing [StringConversionSink] and exposes a | 107 * This class wraps an existing [StringConversionSink] and exposes a |
107 * [ClosableStringSink] interface. The wrapped sink only needs to implement | 108 * [ClosableStringSink] interface. The wrapped sink only needs to implement |
108 * `add` and `close`. | 109 * `add` and `close`. |
109 */ | 110 */ |
110 // TODO(floitsch): make this class public? | 111 // TODO(floitsch): make this class public? |
111 class _StringConversionSinkAsStringSinkAdapter implements ClosableStringSink { | 112 class _StringConversionSinkAsStringSinkAdapter implements ClosableStringSink { |
112 static const _MIN_STRING_SIZE = 16; | 113 static const _MIN_STRING_SIZE = 16; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 171 |
171 /** | 172 /** |
172 * This class provides a mixin for converters that need to accept String | 173 * This class provides a mixin for converters that need to accept String |
173 * inputs. | 174 * inputs. |
174 */ | 175 */ |
175 abstract class StringConversionSinkMixin implements StringConversionSink { | 176 abstract class StringConversionSinkMixin implements StringConversionSink { |
176 | 177 |
177 void addSlice(String str, int start, int end, bool isLast); | 178 void addSlice(String str, int start, int end, bool isLast); |
178 void close(); | 179 void close(); |
179 | 180 |
180 void add(String str) => addSlice(str, 0, str.length, false); | 181 void add(String str) { addSlice(str, 0, str.length, false); } |
181 | 182 |
182 ByteConversionSink asUtf8Sink(bool allowMalformed) { | 183 ByteConversionSink asUtf8Sink(bool allowMalformed) { |
183 return new _Utf8ConversionSink(this, allowMalformed); | 184 return new _Utf8ConversionSink(this, allowMalformed); |
184 } | 185 } |
185 | 186 |
186 ClosableStringSink asStringSink() { | 187 ClosableStringSink asStringSink() { |
187 return new _StringConversionSinkAsStringSinkAdapter(this); | 188 return new _StringConversionSinkAsStringSinkAdapter(this); |
188 } | 189 } |
189 } | 190 } |
190 | 191 |
191 /** | 192 /** |
192 * This class is a [StringConversionSink] that wraps a [StringSink]. | 193 * This class is a [StringConversionSink] that wraps a [StringSink]. |
193 */ | 194 */ |
194 class _StringSinkConversionSink extends StringConversionSinkBase { | 195 class _StringSinkConversionSink extends StringConversionSinkBase { |
195 StringSink _stringSink; | 196 StringSink _stringSink; |
196 _StringSinkConversionSink(StringSink this._stringSink); | 197 _StringSinkConversionSink(StringSink this._stringSink); |
197 | 198 |
198 void close() {} | 199 void close() {} |
199 void addSlice(String str, int start, int end, bool isLast) { | 200 void addSlice(String str, int start, int end, bool isLast) { |
200 if (start != 0 || end != str.length) { | 201 if (start != 0 || end != str.length) { |
201 for (int i = start; i < end; i++) { | 202 for (int i = start; i < end; i++) { |
202 _stringSink.writeCharCode(str.codeUnitAt(i)); | 203 _stringSink.writeCharCode(str.codeUnitAt(i)); |
203 } | 204 } |
204 } else { | 205 } else { |
205 _stringSink.write(str); | 206 _stringSink.write(str); |
206 } | 207 } |
207 if (isLast) close(); | 208 if (isLast) close(); |
208 } | 209 } |
209 | 210 |
210 void add(String str) => _stringSink.write(str); | 211 void add(String str) { _stringSink.write(str); } |
211 | 212 |
212 ByteConversionSink asUtf8Sink(bool allowMalformed) { | 213 ByteConversionSink asUtf8Sink(bool allowMalformed) { |
213 return new _Utf8StringSinkAdapter(this, _stringSink, allowMalformed); | 214 return new _Utf8StringSinkAdapter(this, _stringSink, allowMalformed); |
214 } | 215 } |
215 | 216 |
216 ClosableStringSink asStringSink() { | 217 ClosableStringSink asStringSink() { |
217 return new ClosableStringSink.fromStringSink(_stringSink, this.close); | 218 return new ClosableStringSink.fromStringSink(_stringSink, this.close); |
218 } | 219 } |
219 } | 220 } |
220 | 221 |
(...skipping 25 matching lines...) Expand all Loading... |
246 * [StringConversionSink]. | 247 * [StringConversionSink]. |
247 * | 248 * |
248 * All additional methods of the [StringConversionSink] (compared to the | 249 * All additional methods of the [StringConversionSink] (compared to the |
249 * ChunkedConversionSink) are redirected to the `add` method. | 250 * ChunkedConversionSink) are redirected to the `add` method. |
250 */ | 251 */ |
251 class _StringAdapterSink extends StringConversionSinkBase { | 252 class _StringAdapterSink extends StringConversionSinkBase { |
252 final Sink<String> _sink; | 253 final Sink<String> _sink; |
253 | 254 |
254 _StringAdapterSink(this._sink); | 255 _StringAdapterSink(this._sink); |
255 | 256 |
256 void add(String str) => _sink.add(str); | 257 void add(String str) { _sink.add(str); } |
257 | 258 |
258 void addSlice(String str, int start, int end, bool isLast) { | 259 void addSlice(String str, int start, int end, bool isLast) { |
259 if (start == 0 && end == str.length) { | 260 if (start == 0 && end == str.length) { |
260 add(str); | 261 add(str); |
261 } else { | 262 } else { |
262 add(str.substring(start, end)); | 263 add(str.substring(start, end)); |
263 } | 264 } |
264 if (isLast) close(); | 265 if (isLast) close(); |
265 } | 266 } |
266 | 267 |
267 void close() => _sink.close(); | 268 void close() { _sink.close(); } |
268 } | 269 } |
269 | 270 |
270 | 271 |
271 /** | 272 /** |
272 * Decodes UTF-8 code units and stores them in a [StringSink]. | 273 * Decodes UTF-8 code units and stores them in a [StringSink]. |
273 */ | 274 */ |
274 class _Utf8StringSinkAdapter extends ByteConversionSink { | 275 class _Utf8StringSinkAdapter extends ByteConversionSink { |
275 final _Utf8Decoder _decoder; | 276 final _Utf8Decoder _decoder; |
276 final Sink _sink; | 277 final Sink _sink; |
277 | 278 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 _decoder.convert(chunk, startIndex, endIndex); | 334 _decoder.convert(chunk, startIndex, endIndex); |
334 if (_buffer.isNotEmpty) { | 335 if (_buffer.isNotEmpty) { |
335 String accumulated = _buffer.toString(); | 336 String accumulated = _buffer.toString(); |
336 _chunkedSink.addSlice(accumulated, 0, accumulated.length, isLast); | 337 _chunkedSink.addSlice(accumulated, 0, accumulated.length, isLast); |
337 _buffer.clear(); | 338 _buffer.clear(); |
338 return; | 339 return; |
339 } | 340 } |
340 if (isLast) close(); | 341 if (isLast) close(); |
341 } | 342 } |
342 } | 343 } |
OLD | NEW |