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

Side by Side Diff: runtime/bin/socket_stream.dart

Issue 8506005: Merge the two buffer list implementations (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments from ager@ Created 9 years, 1 month 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 | « runtime/bin/buffer_list.dart ('k') | runtime/bin/string_stream.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 class SocketInputStream implements InputStream { 5 class SocketInputStream implements InputStream {
6 SocketInputStream(Socket socket) { 6 SocketInputStream(Socket socket) {
7 _socket = socket; 7 _socket = socket;
8 } 8 }
9 9
10 List<int> read([int len]) { 10 List<int> read([int len]) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 } 47 }
48 48
49 void set errorHandler(void callback()) { 49 void set errorHandler(void callback()) {
50 _socket.errorHandler = callback; 50 _socket.errorHandler = callback;
51 } 51 }
52 52
53 Socket _socket; 53 Socket _socket;
54 } 54 }
55 55
56 56
57 class _BufferList2 {
58 _BufferList2() {
59 clear();
60 }
61
62 // Adds a new buffer to the list possibly with an offset of the
63 // first byte of interest. The offset can only be specified if the
64 // buffer list is empty.
65 void add(List<int> buffer, [int offset = 0]) {
66 assert(offset == 0 || _buffers.isEmpty());
67 _buffers.addLast(buffer);
68 _length += buffer.length - offset;
69 if (offset != 0) _index = offset;
70 }
71
72 List<int> get first() => _buffers.first();
73 int get index() => _index;
74
75 void removeBytes(int count) {
76 int firstRemaining = first.length - _index;
77 assert(count <= firstRemaining);
78 if (count == firstRemaining) {
79 _buffers.removeFirst();
80 _index = 0;
81 } else {
82 _index += count;
83 }
84 _length -= count;
85 }
86
87 int get length() => _length;
88
89 bool isEmpty() => _buffers.isEmpty();
90
91 void clear() {
92 _index = 0;
93 _length = 0;
94 _buffers = new Queue();
95 }
96
97 int _length; // Total length of pending data.
98 Queue<List<int>> _buffers;
99 int _index; // Offset into the first buffer of next write position.
100 }
101
102
103 class SocketOutputStream implements OutputStream { 57 class SocketOutputStream implements OutputStream {
104 SocketOutputStream(Socket socket) 58 SocketOutputStream(Socket socket)
105 : _socket = socket, _pendingWrites = new _BufferList2() { 59 : _socket = socket, _pendingWrites = new _BufferList() {
106 _socket.writeHandler = _writeHandler; 60 _socket.writeHandler = _writeHandler;
107 _socket.errorHandler = _errorHandler; 61 _socket.errorHandler = _errorHandler;
108 } 62 }
109 63
110 bool write(List<int> buffer, [bool copyBuffer = true]) { 64 bool write(List<int> buffer, [bool copyBuffer = true]) {
111 return _write(buffer, 0, buffer.length, copyBuffer); 65 return _write(buffer, 0, buffer.length, copyBuffer);
112 } 66 }
113 67
114 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { 68 bool writeFrom(List<int> buffer, [int offset = 0, int len]) {
115 return _write( 69 return _write(
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 if (_noPendingWriteHandler != null) _noPendingWriteHandler(); 146 if (_noPendingWriteHandler != null) _noPendingWriteHandler();
193 } 147 }
194 } 148 }
195 149
196 void _errorHandler() { 150 void _errorHandler() {
197 close(); 151 close();
198 if (_streamErrorHandler != null) _streamErrorHandler(); 152 if (_streamErrorHandler != null) _streamErrorHandler();
199 } 153 }
200 154
201 Socket _socket; 155 Socket _socket;
202 _BufferList2 _pendingWrites; 156 _BufferList _pendingWrites;
203 var _noPendingWriteHandler; 157 var _noPendingWriteHandler;
204 var _streamErrorHandler; 158 var _streamErrorHandler;
205 bool _closing = false; 159 bool _closing = false;
206 bool _closed = false; 160 bool _closed = false;
207 } 161 }
OLDNEW
« no previous file with comments | « runtime/bin/buffer_list.dart ('k') | runtime/bin/string_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698