OLD | NEW |
---|---|
(Empty) | |
1 // 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.
| |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 part of sync.http; | |
16 | |
17 // '\n' character | |
18 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.
| |
19 | |
20 typedef void _LineDecoderCallback( | |
21 String line, int bytesRead, _LineDecoder decoder); | |
22 | |
23 class _LineDecoder { | |
24 BytesBuilder _unprocessedBytes = new BytesBuilder(); | |
25 | |
26 int expectedByteCount = -1; | |
27 | |
28 final _LineDecoderCallback _callback; | |
29 | |
30 _LineDecoder.withCallback(this._callback); | |
31 | |
32 void add(List<int> chunk) { | |
33 while (chunk.isNotEmpty) { | |
34 int splitIndex = -1; | |
35 | |
36 if (expectedByteCount > 0) { | |
37 splitIndex = expectedByteCount - _unprocessedBytes.length; | |
38 } else { | |
39 splitIndex = chunk.indexOf(_LINE_TERMINATOR) + 1; | |
40 } | |
41 | |
42 if (splitIndex > 0 && splitIndex <= chunk.length) { | |
43 _unprocessedBytes.add(chunk.sublist(0, splitIndex)); | |
44 chunk = chunk.sublist(splitIndex); | |
45 expectedByteCount = -1; | |
46 _process(_unprocessedBytes.takeBytes()); | |
47 } else { | |
48 _unprocessedBytes.add(chunk); | |
49 chunk = []; | |
50 } | |
51 } | |
52 } | |
53 | |
54 void _process(List<int> line) => | |
55 _callback(UTF8.decoder.convert(line), line.length, this); | |
56 | |
57 int get bufferedBytes => _unprocessedBytes.length; | |
58 | |
59 void close() => _process(_unprocessedBytes.takeBytes()); | |
60 } | |
OLD | NEW |