| 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 * The [ByteConversionSink] provides an interface for converters to | 8 * The [ByteConversionSink] provides an interface for converters to |
| 9 * efficiently transmit byte data. | 9 * efficiently transmit byte data. |
| 10 * | 10 * |
| 11 * Instead of limiting the interface to one non-chunked list of bytes it | 11 * Instead of limiting the interface to one non-chunked list of bytes it |
| 12 * accepts its input in chunks (themselves being lists of bytes). | 12 * accepts its input in chunks (themselves being lists of bytes). |
| 13 */ | 13 */ |
| 14 abstract class ByteConversionSink extends ChunkedConversionSink<List<int>> { | 14 abstract class ByteConversionSink extends ChunkedConversionSink<List<int>> { |
| 15 ByteConversionSink(); | 15 ByteConversionSink(); |
| 16 factory ByteConversionSink.withCallback(void callback(List<int> accumulated)) | 16 factory ByteConversionSink.withCallback(void callback(List<int> accumulated)) |
| 17 = _ByteCallbackSink; | 17 = _ByteCallbackSink; |
| 18 factory ByteConversionSink.from(ChunkedConversionSink<List<int>> sink) | 18 factory ByteConversionSink.from(ChunkedConversionSink<List<int>> sink) |
| 19 = _ByteAdapterSink; | 19 = _ByteAdapterSink; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * Adds the next [chunk] to `this`. | 22 * Adds the next [chunk] to `this`. |
| 23 * | 23 * |
| 24 * Adds the bytes defined by [start] and [end]-exclusive to `this`. | 24 * Adds the bytes defined by [start] and [end]-exclusive to `this`. |
| 25 * | 25 * |
| 26 * If [isLast] is `true` closes `this`. | 26 * If [isLast] is `true` closes `this`. |
| 27 * | 27 * |
| 28 * The given [chunk] is not held onto. Once the method returns, it is safe to | 28 * Contrary to `add` the given [chunk] must not be held onto. Once the method |
| 29 * overwrite the data in it. | 29 * returns, it is safe to overwrite the data in it. |
| 30 */ | 30 */ |
| 31 void addSlice(List<int> chunk, int start, int end, bool isLast); | 31 void addSlice(List<int> chunk, int start, int end, bool isLast); |
| 32 | 32 |
| 33 // TODO(floitsch): add more methods: | 33 // TODO(floitsch): add more methods: |
| 34 // - iterateBytes. | 34 // - iterateBytes. |
| 35 } | 35 } |
| 36 | 36 |
| 37 /** | 37 /** |
| 38 * This class provides a base-class for converters that need to accept byte | 38 * This class provides a base-class for converters that need to accept byte |
| 39 * inputs. | 39 * inputs. |
| 40 */ | 40 */ |
| 41 abstract class ByteConversionSinkBase extends ByteConversionSink { | 41 abstract class ByteConversionSinkBase extends ByteConversionSink { |
| 42 | 42 |
| 43 void add(List<int> chunk); | 43 void add(List<int> chunk); |
| 44 void close(); | 44 void close(); |
| 45 | 45 |
| 46 void addSlice(List<int> chunk, int start, int end, bool isLast) { | 46 void addSlice(List<int> chunk, int start, int end, bool isLast) { |
| 47 if (start != 0 || end != chunk.length) { | 47 add(chunk.sublist(start, end)); |
| 48 add(chunk.sublist(start, end)); | |
| 49 } else { | |
| 50 add(chunk); | |
| 51 } | |
| 52 if (isLast) close(); | 48 if (isLast) close(); |
| 53 } | 49 } |
| 54 } | 50 } |
| 55 | 51 |
| 56 /** | 52 /** |
| 57 * This class adapts a simple [ChunkedConversionSink] to a [ByteConversionSink]. | 53 * This class adapts a simple [ChunkedConversionSink] to a [ByteConversionSink]. |
| 58 * | 54 * |
| 59 * All additional methods of the [ByteConversionSink] (compared to the | 55 * All additional methods of the [ByteConversionSink] (compared to the |
| 60 * ChunkedConversionSink) are redirected to the `add` method. | 56 * ChunkedConversionSink) are redirected to the `add` method. |
| 61 */ | 57 */ |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 v |= v >> 8; | 105 v |= v >> 8; |
| 110 v |= v >> 16; | 106 v |= v >> 16; |
| 111 v++; | 107 v++; |
| 112 return v; | 108 return v; |
| 113 } | 109 } |
| 114 | 110 |
| 115 void close() { | 111 void close() { |
| 116 _callback(_buffer.sublist(0, _bufferIndex)); | 112 _callback(_buffer.sublist(0, _bufferIndex)); |
| 117 } | 113 } |
| 118 } | 114 } |
| OLD | NEW |