OLD | NEW |
| (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 // This is a copy of "dart:io"'s BytesBuilder implementation, from | |
6 // sdk/lib/io/bytes_builder.dart. It's copied here to make it available to | |
7 // non-"dart:io" applications (issue 18348). | |
8 // | |
9 // Because it's copied directly, there are no modifications from the original. | |
10 // | |
11 // This is up-to-date as of sdk revision | |
12 // 86227840d75d974feb238f8b3c59c038b99c05cf. | |
13 import 'dart:math'; | |
14 import 'dart:typed_data'; | |
15 | |
16 /** | |
17 * Builds a list of bytes, allowing bytes and lists of bytes to be added at the | |
18 * end. | |
19 * | |
20 * Used to efficiently collect bytes and lists of bytes. | |
21 */ | |
22 abstract class BytesBuilder { | |
23 /** | |
24 * Construct a new empty [BytesBuilder]. | |
25 * | |
26 * If [copy] is true, the data is always copied when added to the list. If | |
27 * it [copy] is false, the data is only copied if needed. That means that if | |
28 * the lists are changed after added to the [BytesBuilder], it may effect the | |
29 * output. Default is `true`. | |
30 */ | |
31 factory BytesBuilder({bool copy: true}) { | |
32 if (copy) { | |
33 return new _CopyingBytesBuilder(); | |
34 } else { | |
35 return new _BytesBuilder(); | |
36 } | |
37 } | |
38 | |
39 /** | |
40 * Appends [bytes] to the current contents of the builder. | |
41 * | |
42 * Each value of [bytes] will be bit-representation truncated to the range | |
43 * 0 .. 255. | |
44 */ | |
45 void add(List<int> bytes); | |
46 | |
47 /** | |
48 * Append [byte] to the current contents of the builder. | |
49 * | |
50 * The [byte] will be bit-representation truncated to the range 0 .. 255. | |
51 */ | |
52 void addByte(int byte); | |
53 | |
54 /** | |
55 * Returns the contents of `this` and clears `this`. | |
56 * | |
57 * The list returned is a view of the the internal buffer, limited to the | |
58 * [length]. | |
59 */ | |
60 List<int> takeBytes(); | |
61 | |
62 /** | |
63 * Returns a copy of the current contents of the builder. | |
64 * | |
65 * Leaves the contents of the builder intact. | |
66 */ | |
67 List<int> toBytes(); | |
68 | |
69 /** | |
70 * The number of bytes in the builder. | |
71 */ | |
72 int get length; | |
73 | |
74 /** | |
75 * Returns `true` if the buffer is empty. | |
76 */ | |
77 bool get isEmpty; | |
78 | |
79 /** | |
80 * Returns `true` if the buffer is not empty. | |
81 */ | |
82 bool get isNotEmpty; | |
83 | |
84 /** | |
85 * Clear the contents of the builder. | |
86 */ | |
87 void clear(); | |
88 } | |
89 | |
90 | |
91 class _CopyingBytesBuilder implements BytesBuilder { | |
92 // Start with 1024 bytes. | |
93 static const int _INIT_SIZE = 1024; | |
94 | |
95 int _length = 0; | |
96 Uint8List _buffer; | |
97 | |
98 void add(List<int> bytes) { | |
99 int bytesLength = bytes.length; | |
100 if (bytesLength == 0) return; | |
101 int required = _length + bytesLength; | |
102 if (_buffer == null) { | |
103 int size = _pow2roundup(required); | |
104 size = max(size, _INIT_SIZE); | |
105 _buffer = new Uint8List(size); | |
106 } else if (_buffer.length < required) { | |
107 // We will create a list in the range of 2-4 times larger than | |
108 // required. | |
109 int size = _pow2roundup(required) * 2; | |
110 var newBuffer = new Uint8List(size); | |
111 newBuffer.setRange(0, _buffer.length, _buffer); | |
112 _buffer = newBuffer; | |
113 } | |
114 assert(_buffer.length >= required); | |
115 if (bytes is Uint8List) { | |
116 _buffer.setRange(_length, required, bytes); | |
117 } else { | |
118 for (int i = 0; i < bytesLength; i++) { | |
119 _buffer[_length + i] = bytes[i]; | |
120 } | |
121 } | |
122 _length = required; | |
123 } | |
124 | |
125 void addByte(int byte) => add([byte]); | |
126 | |
127 List<int> takeBytes() { | |
128 if (_buffer == null) return new Uint8List(0); | |
129 var buffer = new Uint8List.view(_buffer.buffer, 0, _length); | |
130 clear(); | |
131 return buffer; | |
132 } | |
133 | |
134 List<int> toBytes() { | |
135 if (_buffer == null) return new Uint8List(0); | |
136 return new Uint8List.fromList( | |
137 new Uint8List.view(_buffer.buffer, 0, _length)); | |
138 } | |
139 | |
140 int get length => _length; | |
141 | |
142 bool get isEmpty => _length == 0; | |
143 | |
144 bool get isNotEmpty => _length != 0; | |
145 | |
146 void clear() { | |
147 _length = 0; | |
148 _buffer = null; | |
149 } | |
150 | |
151 int _pow2roundup(int x) { | |
152 --x; | |
153 x |= x >> 1; | |
154 x |= x >> 2; | |
155 x |= x >> 4; | |
156 x |= x >> 8; | |
157 x |= x >> 16; | |
158 return x + 1; | |
159 } | |
160 } | |
161 | |
162 | |
163 class _BytesBuilder implements BytesBuilder { | |
164 int _length = 0; | |
165 final List _chunks = []; | |
166 | |
167 void add(List<int> bytes) { | |
168 if (bytes is! Uint8List) { | |
169 bytes = new Uint8List.fromList(bytes); | |
170 } | |
171 _chunks.add(bytes); | |
172 _length += bytes.length; | |
173 } | |
174 | |
175 void addByte(int byte) => add([byte]); | |
176 | |
177 List<int> takeBytes() { | |
178 if (_chunks.length == 0) return new Uint8List(0); | |
179 if (_chunks.length == 1) { | |
180 var buffer = _chunks.single; | |
181 clear(); | |
182 return buffer; | |
183 } | |
184 var buffer = new Uint8List(_length); | |
185 int offset = 0; | |
186 for (var chunk in _chunks) { | |
187 buffer.setRange(offset, offset + chunk.length, chunk); | |
188 offset += chunk.length; | |
189 } | |
190 clear(); | |
191 return buffer; | |
192 } | |
193 | |
194 List<int> toBytes() { | |
195 if (_chunks.length == 0) return new Uint8List(0); | |
196 var buffer = new Uint8List(_length); | |
197 int offset = 0; | |
198 for (var chunk in _chunks) { | |
199 buffer.setRange(offset, offset + chunk.length, chunk); | |
200 offset += chunk.length; | |
201 } | |
202 return buffer; | |
203 } | |
204 | |
205 int get length => _length; | |
206 | |
207 bool get isEmpty => _length == 0; | |
208 | |
209 bool get isNotEmpty => _length != 0; | |
210 | |
211 void clear() { | |
212 _length = 0; | |
213 _chunks.clear(); | |
214 } | |
215 } | |
OLD | NEW |