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

Unified Diff: sdk/lib/convert/byte_converter.dart

Issue 17580014: dart:convert library. Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/codec/json.dart ('k') | sdk/lib/convert/convert.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/convert/byte_converter.dart
diff --git a/sdk/lib/convert/byte_converter.dart b/sdk/lib/convert/byte_converter.dart
new file mode 100644
index 0000000000000000000000000000000000000000..a68daed9d5f79379833aa43cca9637650b88bcb8
--- /dev/null
+++ b/sdk/lib/convert/byte_converter.dart
@@ -0,0 +1,128 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of dart.convert;
+
+class _ByteInterface extends ChunkedConversionInterface {
+ const _ByteInterface();
+
+ ByteConversionSink adapt(ChunkedConversionSink sink) {
+ if (sink.interface == _ByteInterface) return sink;
+ // Common denominator is ChunkedConversionSink.
+ return new _ByteAdapterSink(sink);
+ }
+
+ ByteConversionSink adaptEventSink(EventSink sink) {
+ return new _EventSinkByteAdapterSink(sink);
+ }
+}
+
+abstract class ByteConversionSink extends ChunkedConversionSink<List<int>> {
+ ByteConversionSink();
+ factory ByteConversionSink.withCallback(void callback(List<int> list)) =
+ _ByteConversionCallbackSink;
+
+ void addChunk(List<int> chunk, int start, int end, bool isLast);
+ void addIsoLatin1(String string, bool isLast);
+ void addAscii(String string, bool isLast);
+ void iterateBytes(ByteIterator iterator, bool isLast);
+ void close(); // Equivalent to addChunk([], true);
+
+ static ChunkedConversionInterface get INTERFACE => const _ByteInterface();
+}
+
+abstract class ByteConversionSinkBase implements ByteConversionSink {
+
+ void add(List<int> chunk);
+ void close();
+
+ void addNonChunked(List<int> list) {
+ add(list);
+ close();
+ }
+
+ void addChunk(List<int> chunk, int start, int end, bool isLast) {
+ if (start != 0 || end != chunk.length) {
+ add(chunk.sublist(start, end));
+ } else {
+ add(chunk);
+ }
+ if (isLast) close();
+ }
+
+
+ void addIsoLatin1(String string, bool isLast) {
+ addChunk(string.codeUnits, 0, string.length, isLast);
+ }
+ void addAscii(String string, bool isLast) {
+ addChunk(string.codeUnits, 0, string.length, isLast);
+ }
+ void iterateBytes(ByteIterator iterator, bool isLast) {
+ List<int> list = iterator.convertToByteList();
+ addChunk(list, 0, list.length, isLast);
+ }
+
+ _ByteInterface get interface => ByteConversionSink.INTERFACE;
+}
+
+class _ByteAdapterSink extends ByteConversionSinkBase {
+ final ChunkedConversionSink _otherSink;
+ final List<int> _accumulated = <int>[];
+
+ _ByteAdapterSink(this._otherSink);
+
+ void addChunk(List<int> chunk, int start, int end, bool isLast) {
+ if (start != 0 || end != chunk.length) {
+ add(chunk.getRange(start, end));
+ } else {
+ add(chunk);
+ }
+ if (isLast) close();
+ }
+
+ void add(Iterable<int> chunk) {
+ _accumulated.addAll(chunk);
+ }
+
+ void close() {
+ _otherSink.addNonChunked(_accumulated);
+ }
+
+ void addNonChunked(List<int> list) => _otherSink.addNonChunked(list);
+}
+
+class _EventSinkByteAdapterSink extends ByteConversionSinkBase {
+ final EventSink _sink;
+ final List<int> _accumulated = <int>[];
+
+ _EventSinkByteAdapterSink(this._sink);
+
+ void add(List<int> chunk) => _sink.add(chunk);
+ void close() => _sink.close();
+}
+
+class _ByteConversionCallbackSink extends ByteConversionSinkBase {
+ Function _callback;
+ List<int> _accumulated = <int>[];
+
+ _ByteConversionCallbackSink(this._callback);
+
+ void add(Iterable<int> chunk) => _accumulated.addAll(chunk);
+ void close() => _callback(_accumulated);
+
+ void addNonChunked(List<int> list) => _callback(list);
+}
+
+abstract class ByteIterator extends Iterator<int> {
+ /**
+ * Fills [list] at position [startIndex] up to the [endIndex]. If
+ * not enough elements were available returns the next free index.
+ */
+ int fill(List<int> list, int startIndex, int endIndex);
+
+ /**
+ * Returns the string representation of the full content of this iterator.
+ */
+ List<int> convertToByteList();
+}
« no previous file with comments | « sdk/lib/codec/json.dart ('k') | sdk/lib/convert/convert.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698