| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.convert; |
| 6 |
| 7 class _ByteInterface extends ChunkedConversionInterface { |
| 8 const _ByteInterface(); |
| 9 |
| 10 ByteConversionSink adapt(ChunkedConversionSink sink) { |
| 11 if (sink.interface == _ByteInterface) return sink; |
| 12 // Common denominator is ChunkedConversionSink. |
| 13 return new _ByteAdapterSink(sink); |
| 14 } |
| 15 |
| 16 ByteConversionSink adaptEventSink(EventSink sink) { |
| 17 return new _EventSinkByteAdapterSink(sink); |
| 18 } |
| 19 } |
| 20 |
| 21 abstract class ByteConversionSink extends ChunkedConversionSink<List<int>> { |
| 22 ByteConversionSink(); |
| 23 factory ByteConversionSink.withCallback(void callback(List<int> list)) = |
| 24 _ByteConversionCallbackSink; |
| 25 |
| 26 void addChunk(List<int> chunk, int start, int end, bool isLast); |
| 27 void addIsoLatin1(String string, bool isLast); |
| 28 void addAscii(String string, bool isLast); |
| 29 void iterateBytes(ByteIterator iterator, bool isLast); |
| 30 void close(); // Equivalent to addChunk([], true); |
| 31 |
| 32 static ChunkedConversionInterface get INTERFACE => const _ByteInterface(); |
| 33 } |
| 34 |
| 35 abstract class ByteConversionSinkBase implements ByteConversionSink { |
| 36 |
| 37 void add(List<int> chunk); |
| 38 void close(); |
| 39 |
| 40 void addNonChunked(List<int> list) { |
| 41 add(list); |
| 42 close(); |
| 43 } |
| 44 |
| 45 void addChunk(List<int> chunk, int start, int end, bool isLast) { |
| 46 if (start != 0 || end != chunk.length) { |
| 47 add(chunk.sublist(start, end)); |
| 48 } else { |
| 49 add(chunk); |
| 50 } |
| 51 if (isLast) close(); |
| 52 } |
| 53 |
| 54 |
| 55 void addIsoLatin1(String string, bool isLast) { |
| 56 addChunk(string.codeUnits, 0, string.length, isLast); |
| 57 } |
| 58 void addAscii(String string, bool isLast) { |
| 59 addChunk(string.codeUnits, 0, string.length, isLast); |
| 60 } |
| 61 void iterateBytes(ByteIterator iterator, bool isLast) { |
| 62 List<int> list = iterator.convertToByteList(); |
| 63 addChunk(list, 0, list.length, isLast); |
| 64 } |
| 65 |
| 66 _ByteInterface get interface => ByteConversionSink.INTERFACE; |
| 67 } |
| 68 |
| 69 class _ByteAdapterSink extends ByteConversionSinkBase { |
| 70 final ChunkedConversionSink _otherSink; |
| 71 final List<int> _accumulated = <int>[]; |
| 72 |
| 73 _ByteAdapterSink(this._otherSink); |
| 74 |
| 75 void addChunk(List<int> chunk, int start, int end, bool isLast) { |
| 76 if (start != 0 || end != chunk.length) { |
| 77 add(chunk.getRange(start, end)); |
| 78 } else { |
| 79 add(chunk); |
| 80 } |
| 81 if (isLast) close(); |
| 82 } |
| 83 |
| 84 void add(Iterable<int> chunk) { |
| 85 _accumulated.addAll(chunk); |
| 86 } |
| 87 |
| 88 void close() { |
| 89 _otherSink.addNonChunked(_accumulated); |
| 90 } |
| 91 |
| 92 void addNonChunked(List<int> list) => _otherSink.addNonChunked(list); |
| 93 } |
| 94 |
| 95 class _EventSinkByteAdapterSink extends ByteConversionSinkBase { |
| 96 final EventSink _sink; |
| 97 final List<int> _accumulated = <int>[]; |
| 98 |
| 99 _EventSinkByteAdapterSink(this._sink); |
| 100 |
| 101 void add(List<int> chunk) => _sink.add(chunk); |
| 102 void close() => _sink.close(); |
| 103 } |
| 104 |
| 105 class _ByteConversionCallbackSink extends ByteConversionSinkBase { |
| 106 Function _callback; |
| 107 List<int> _accumulated = <int>[]; |
| 108 |
| 109 _ByteConversionCallbackSink(this._callback); |
| 110 |
| 111 void add(Iterable<int> chunk) => _accumulated.addAll(chunk); |
| 112 void close() => _callback(_accumulated); |
| 113 |
| 114 void addNonChunked(List<int> list) => _callback(list); |
| 115 } |
| 116 |
| 117 abstract class ByteIterator extends Iterator<int> { |
| 118 /** |
| 119 * Fills [list] at position [startIndex] up to the [endIndex]. If |
| 120 * not enough elements were available returns the next free index. |
| 121 */ |
| 122 int fill(List<int> list, int startIndex, int endIndex); |
| 123 |
| 124 /** |
| 125 * Returns the string representation of the full content of this iterator. |
| 126 */ |
| 127 List<int> convertToByteList(); |
| 128 } |
| OLD | NEW |