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

Side by Side Diff: lib/src/byte_accumulator_sink.dart

Issue 1911053002: Add sinks to provide synchronous access to events. (Closed) Base URL: git@github.com:dart-lang/convert@master
Patch Set: Code review changes Created 4 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
« no previous file with comments | « lib/src/accumulator_sink.dart ('k') | lib/src/string_accumulator_sink.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) 2016, 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 import 'dart:convert';
6 import 'dart:typed_data';
7
8 import 'package:typed_data/typed_data.dart';
9
10 /// A sink that provides access to the concatenated bytes passed to it.
11 ///
12 /// See also [ByteConversionSink.withCallback].
13 class ByteAccumulatorSink extends ByteConversionSinkBase {
14 /// The bytes accumulated so far.
15 ///
16 /// The returned [Uint8List] is viewing a shared buffer, so it should not be
17 /// changed and any bytes outside the view should not be accessed.
18 Uint8List get bytes => new Uint8List.view(_buffer.buffer, 0, _buffer.length);
19
20 final _buffer = new Uint8Buffer();
21
22 /// Whether [close] has been called.
23 bool get isClosed => _isClosed;
24 var _isClosed = false;
25
26 /// Removes all bytes from [bytes].
27 ///
28 /// This can be used to avoid double-processing data.
29 void clear() {
30 _buffer.clear();
31 }
32
33 void add(List<int> bytes) {
34 if (_isClosed) {
35 throw new StateError("Can't add to a closed sink.");
36 }
37
38 _buffer.addAll(bytes);
39 }
40
41 void addSlice(List<int> chunk, int start, int end, bool isLast) {
42 if (_isClosed) {
43 throw new StateError("Can't add to a closed sink.");
44 }
45
46 _buffer.addAll(chunk, start, end);
47 if (isLast) _isClosed = true;
48 }
49
50 void close() {
51 _isClosed = true;
52 }
53 }
OLDNEW
« no previous file with comments | « lib/src/accumulator_sink.dart ('k') | lib/src/string_accumulator_sink.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698