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

Side by Side Diff: pkg/http_parser/lib/src/bytes_builder.dart

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

Powered by Google App Engine
This is Rietveld 408576698