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

Unified Diff: lib/src/line_decoder.dart

Issue 2827083002: Created a new synchronous http client using RawSynchronousSockets. (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
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..5a50a8c69f2281b82c54cce3a8861bc7eef4adc3
--- /dev/null
+++ b/lib/src/line_decoder.dart
@@ -0,0 +1,60 @@
+// Copyright 2017 Google Inc. All Rights Reserved.
zra 2017/04/19 17:46:13 Please use the usual copyright header.
bkonyi 2017/04/19 21:29:50 Done.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+part of sync.http;
+
+// '\n' character
+const int _LINE_TERMINATOR = 10;
zra 2017/04/19 17:46:13 See the Dart style guide for the correct formattin
bkonyi 2017/04/19 21:29:50 Done.
+
+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(_LINE_TERMINATOR) + 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());
+}

Powered by Google App Engine
This is Rietveld 408576698