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

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/lib/convert/byte_conversion.dart

Issue 2698353003: unfork DDC's copy of most SDK libraries (Closed)
Patch Set: revert core_patch Created 3 years, 9 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013, 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 dart.convert;
6
7 /**
8 * The [ByteConversionSink] provides an interface for converters to
9 * efficiently transmit byte data.
10 *
11 * Instead of limiting the interface to one non-chunked list of bytes it
12 * accepts its input in chunks (themselves being lists of bytes).
13 *
14 * This abstract class will likely get more methods over time. Implementers are
15 * urged to extend or mix in [ByteConversionSinkBase] to ensure that their
16 * class covers the newly added methods.
17 */
18 abstract class ByteConversionSink extends ChunkedConversionSink<List<int>> {
19 ByteConversionSink();
20 factory ByteConversionSink.withCallback(void callback(List<int> accumulated))
21 = _ByteCallbackSink;
22 factory ByteConversionSink.from(Sink<List<int>> sink)
23 = _ByteAdapterSink;
24
25 /**
26 * Adds the next [chunk] to `this`.
27 *
28 * Adds the bytes defined by [start] and [end]-exclusive to `this`.
29 *
30 * If [isLast] is `true` closes `this`.
31 *
32 * Contrary to `add` the given [chunk] must not be held onto. Once the method
33 * returns, it is safe to overwrite the data in it.
34 */
35 void addSlice(List<int> chunk, int start, int end, bool isLast);
36
37 // TODO(floitsch): add more methods:
38 // - iterateBytes.
39 }
40
41 /**
42 * This class provides a base-class for converters that need to accept byte
43 * inputs.
44 */
45 abstract class ByteConversionSinkBase extends ByteConversionSink {
46
47 void add(List<int> chunk);
48 void close();
49
50 void addSlice(List<int> chunk, int start, int end, bool isLast) {
51 add(chunk.sublist(start, end));
52 if (isLast) close();
53 }
54 }
55
56 /**
57 * This class adapts a simple [Sink] to a [ByteConversionSink].
58 *
59 * All additional methods of the [ByteConversionSink] (compared to the
60 * ChunkedConversionSink) are redirected to the `add` method.
61 */
62 class _ByteAdapterSink extends ByteConversionSinkBase {
63 final Sink<List<int>> _sink;
64
65 _ByteAdapterSink(this._sink);
66
67 void add(List<int> chunk) { _sink.add(chunk); }
68 void close() { _sink.close(); }
69 }
70
71 /**
72 * This class accumulates all chunks into one list of bytes
73 * and invokes a callback when the sink is closed.
74 *
75 * This class can be used to terminate a chunked conversion.
76 */
77 class _ByteCallbackSink extends ByteConversionSinkBase {
78 static const _INITIAL_BUFFER_SIZE = 1024;
79
80 final _ChunkedConversionCallback<List<int>> _callback;
81 List<int> _buffer = new Uint8List(_INITIAL_BUFFER_SIZE);
82 int _bufferIndex = 0;
83
84 _ByteCallbackSink(void callback(List<int> accumulated))
85 : this._callback = callback;
86
87 void add(Iterable<int> chunk) {
88 int freeCount = _buffer.length - _bufferIndex;
89 if (chunk.length > freeCount) {
90 // Grow the buffer.
91 int oldLength = _buffer.length;
92 int newLength = _roundToPowerOf2(chunk.length + oldLength) * 2;
93 List<int> grown = new Uint8List(newLength);
94 grown.setRange(0, _buffer.length, _buffer);
95 _buffer = grown;
96 }
97 _buffer.setRange(_bufferIndex, _bufferIndex + chunk.length, chunk);
98 _bufferIndex += chunk.length;
99 }
100
101 static int _roundToPowerOf2(int v) {
102 assert(v > 0);
103 v--;
104 v |= v >> 1;
105 v |= v >> 2;
106 v |= v >> 4;
107 v |= v >> 8;
108 v |= v >> 16;
109 v++;
110 return v;
111 }
112
113 void close() {
114 _callback(_buffer.sublist(0, _bufferIndex));
115 }
116 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698