OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 sync.http; |
| 6 |
| 7 // '\n' character |
| 8 const int _lineTerminator = 10; |
| 9 |
| 10 typedef void _LineDecoderCallback( |
| 11 String line, int bytesRead, _LineDecoder decoder); |
| 12 |
| 13 class _LineDecoder { |
| 14 BytesBuilder _unprocessedBytes = new BytesBuilder(); |
| 15 |
| 16 int expectedByteCount = -1; |
| 17 |
| 18 final _LineDecoderCallback _callback; |
| 19 |
| 20 _LineDecoder.withCallback(this._callback); |
| 21 |
| 22 void add(List<int> chunk) { |
| 23 while (chunk.isNotEmpty) { |
| 24 int splitIndex = -1; |
| 25 |
| 26 if (expectedByteCount > 0) { |
| 27 splitIndex = expectedByteCount - _unprocessedBytes.length; |
| 28 } else { |
| 29 splitIndex = chunk.indexOf(_lineTerminator) + 1; |
| 30 } |
| 31 |
| 32 if (splitIndex > 0 && splitIndex <= chunk.length) { |
| 33 _unprocessedBytes.add(chunk.sublist(0, splitIndex)); |
| 34 chunk = chunk.sublist(splitIndex); |
| 35 expectedByteCount = -1; |
| 36 _process(_unprocessedBytes.takeBytes()); |
| 37 } else { |
| 38 _unprocessedBytes.add(chunk); |
| 39 chunk = []; |
| 40 } |
| 41 } |
| 42 } |
| 43 |
| 44 void _process(List<int> line) => |
| 45 _callback(UTF8.decoder.convert(line), line.length, this); |
| 46 |
| 47 int get bufferedBytes => _unprocessedBytes.length; |
| 48 |
| 49 void close() => _process(_unprocessedBytes.takeBytes()); |
| 50 } |
OLD | NEW |