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

Unified Diff: lib/src/line_decoder.dart

Issue 2833903002: Updated README to notify users of Dart SDK version requirements. (Closed)
Patch Set: Created 3 years, 8 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 | « codereview.settings ('k') | lib/src/sync_http.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/line_decoder.dart
diff --git a/lib/src/line_decoder.dart b/lib/src/line_decoder.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ced5499e0b180bad6bdef76560dda1ae1e7c77a1
--- /dev/null
+++ b/lib/src/line_decoder.dart
@@ -0,0 +1,50 @@
+// Copyright (c) 2017, 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 sync.http;
+
+// '\n' character
+const int _lineTerminator = 10;
+
+typedef void _LineDecoderCallback(
+ String line, int bytesRead, _LineDecoder decoder);
+
+class _LineDecoder {
+ BytesBuilder _unprocessedBytes = new BytesBuilder();
+
+ int expectedByteCount = -1;
+
+ final _LineDecoderCallback _callback;
+
+ _LineDecoder.withCallback(this._callback);
+
+ void add(List<int> chunk) {
+ while (chunk.isNotEmpty) {
+ int splitIndex = -1;
+
+ if (expectedByteCount > 0) {
+ splitIndex = expectedByteCount - _unprocessedBytes.length;
+ } else {
+ splitIndex = chunk.indexOf(_lineTerminator) + 1;
+ }
+
+ if (splitIndex > 0 && splitIndex <= chunk.length) {
+ _unprocessedBytes.add(chunk.sublist(0, splitIndex));
+ chunk = chunk.sublist(splitIndex);
+ expectedByteCount = -1;
+ _process(_unprocessedBytes.takeBytes());
+ } else {
+ _unprocessedBytes.add(chunk);
+ chunk = [];
+ }
+ }
+ }
+
+ void _process(List<int> line) =>
+ _callback(UTF8.decoder.convert(line), line.length, this);
+
+ int get bufferedBytes => _unprocessedBytes.length;
+
+ void close() => _process(_unprocessedBytes.takeBytes());
+}
« no previous file with comments | « codereview.settings ('k') | lib/src/sync_http.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698