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

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

Issue 8493002: Change the default for the len argument to writeFrom on OutputStream (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor changes 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
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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 } 100 }
101 101
102 102
103 class SocketOutputStream implements OutputStream { 103 class SocketOutputStream implements OutputStream {
104 SocketOutputStream(Socket socket) 104 SocketOutputStream(Socket socket)
105 : _socket = socket, _pendingWrites = new _BufferList2() { 105 : _socket = socket, _pendingWrites = new _BufferList2() {
106 _socket.writeHandler = _writeHandler; 106 _socket.writeHandler = _writeHandler;
107 _socket.errorHandler = _errorHandler; 107 _socket.errorHandler = _errorHandler;
108 } 108 }
109 109
110 bool write(List<int> buffer) { 110 bool write(List<int> buffer, [bool copyBuffer = false]) {
Mads Ager (google) 2011/11/07 09:18:34 Ah, it is actually false by default. Update commen
Søren Gjesse 2011/11/08 09:31:53 Changed to default to be true (as stated in the co
111 return _write(buffer, 0, buffer.length, false); 111 return _write(buffer, 0, buffer.length, copyBuffer);
112 } 112 }
113 113
114 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { 114 bool writeFrom(List<int> buffer, [int offset = 0, int len]) {
115 return _write(buffer, offset, (len == null) ? buffer.length : len, true); 115 return _write(
116 buffer, offset, (len == null) ? buffer.length - offset : len, true);
116 } 117 }
117 118
118 void close() { 119 void close() {
119 if (!_pendingWrites.isEmpty()) { 120 if (!_pendingWrites.isEmpty()) {
120 // Mark the socket for close when all data is written. 121 // Mark the socket for close when all data is written.
121 _closing = true; 122 _closing = true;
122 _socket.writeHandler = _writeHandler; 123 _socket.writeHandler = _writeHandler;
123 } else { 124 } else {
124 // Close the socket for writing. 125 // Close the socket for writing.
125 _socket._closeWrite(); 126 _socket._closeWrite();
(...skipping 16 matching lines...) Expand all
142 void set closeHandler(void callback()) { 143 void set closeHandler(void callback()) {
143 _socket.closeHandler = callback; 144 _socket.closeHandler = callback;
144 } 145 }
145 146
146 void set errorHandler(void callback()) { 147 void set errorHandler(void callback()) {
147 _streamErrorHandler = callback; 148 _streamErrorHandler = callback;
148 } 149 }
149 150
150 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { 151 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) {
151 if (_closing || _closed) throw new StreamException("Stream closed"); 152 if (_closing || _closed) throw new StreamException("Stream closed");
152 if (len == null) len = buffer.length;
153 int bytesWritten = 0; 153 int bytesWritten = 0;
154 if (_pendingWrites.isEmpty()) { 154 if (_pendingWrites.isEmpty()) {
155 // If nothing is buffered write as much as possible and buffer 155 // If nothing is buffered write as much as possible and buffer
156 // the rest. 156 // the rest.
157 bytesWritten = _socket.writeList(buffer, offset, len); 157 bytesWritten = _socket.writeList(buffer, offset, len);
158 if (bytesWritten == len) return true; 158 if (bytesWritten == len) return true;
159 } 159 }
160 160
161 // Place remaining data on the pending writes queue. 161 // Place remaining data on the pending writes queue.
162 int notWrittenOffset = offset + bytesWritten;
162 if (copyBuffer) { 163 if (copyBuffer) {
163 List<int> newBuffer = 164 List<int> newBuffer =
164 buffer.getRange(offset + bytesWritten, buffer.length); 165 buffer.getRange(notWrittenOffset, len - bytesWritten);
165 _pendingWrites.add(newBuffer); 166 _pendingWrites.add(newBuffer);
166 } else { 167 } else {
167 _pendingWrites.add(buffer, bytesWritten); 168 assert(offset + len = buffer.length);
169 _pendingWrites.add(buffer, notWrittenOffset);
168 } 170 }
169 } 171 }
170 172
171 void _writeHandler() { 173 void _writeHandler() {
172 // Write as much buffered data to the socket as possible. 174 // Write as much buffered data to the socket as possible.
173 while (!_pendingWrites.isEmpty()) { 175 while (!_pendingWrites.isEmpty()) {
174 List<int> buffer = _pendingWrites.first; 176 List<int> buffer = _pendingWrites.first;
175 int offset = _pendingWrites.index; 177 int offset = _pendingWrites.index;
176 int bytesToWrite = buffer.length - offset; 178 int bytesToWrite = buffer.length - offset;
177 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); 179 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite);
(...skipping 18 matching lines...) Expand all
196 if (_streamErrorHandler != null) _streamErrorHandler(); 198 if (_streamErrorHandler != null) _streamErrorHandler();
197 } 199 }
198 200
199 Socket _socket; 201 Socket _socket;
200 _BufferList2 _pendingWrites; 202 _BufferList2 _pendingWrites;
201 var _noPendingWriteHandler; 203 var _noPendingWriteHandler;
202 var _streamErrorHandler; 204 var _streamErrorHandler;
203 bool _closing = false; 205 bool _closing = false;
204 bool _closed = false; 206 bool _closed = false;
205 } 207 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698