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

Side by Side Diff: lib/io/socket_stream_impl.dart

Issue 11338037: Start reading data from sockets directly into external byte arrays (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments from ager@ Created 8 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 | « lib/io/socket.dart ('k') | runtime/bin/builtin_impl_sources.gypi » ('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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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) : _socket = socket { 6 _SocketInputStream(Socket socket) : _socket = socket {
7 if (_socket._closed) _closed = true; 7 if (_socket._closed) _closed = true;
8 _socket.onClosed = _onClosed; 8 _socket.onClosed = _onClosed;
9 } 9 }
10 10
11 List<int> read([int len]) { 11 List<int> read([int len]) {
12 int bytesToRead = available(); 12 return _socket.read(len);
13 if (bytesToRead == 0) return null;
14 if (len !== null) {
15 if (len <= 0) {
16 throw new StreamException("Illegal length $len");
17 } else if (bytesToRead > len) {
18 bytesToRead = len;
19 }
20 }
21 List<int> buffer = new Uint8List(bytesToRead);
22 int bytesRead = _socket.readList(buffer, 0, bytesToRead);
23 if (bytesRead == 0) {
24 // On MacOS when reading from a tty Ctrl-D will result in one
25 // byte reported as available. Attempting to read it out will
26 // result in zero bytes read. When that happens there is no data
27 // which is indicated by a null return value.
28 return null;
29 } else if (bytesRead < bytesToRead) {
30 List<int> newBuffer = new Uint8List(bytesRead);
31 newBuffer.setRange(0, bytesRead, buffer);
32 return newBuffer;
33 } else {
34 return buffer;
35 }
36 } 13 }
37 14
38 int readInto(List<int> buffer, [int offset = 0, int len]) { 15 int readInto(List<int> buffer, [int offset = 0, int len]) {
39 if (_closed) return null; 16 if (_closed) return null;
40 if (len === null) len = buffer.length; 17 if (len === null) len = buffer.length;
41 if (offset < 0) throw new StreamException("Illegal offset $offset"); 18 if (offset < 0) throw new StreamException("Illegal offset $offset");
42 if (len < 0) throw new StreamException("Illegal length $len"); 19 if (len < 0) throw new StreamException("Illegal length $len");
43 return _socket.readList(buffer, offset, len); 20 return _socket.readList(buffer, offset, len);
44 } 21 }
45 22
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 } 198 }
222 } 199 }
223 200
224 Socket _socket; 201 Socket _socket;
225 _BufferList _pendingWrites; 202 _BufferList _pendingWrites;
226 Function _onNoPendingWrites; 203 Function _onNoPendingWrites;
227 Function _onClosed; 204 Function _onClosed;
228 bool _closing = false; 205 bool _closing = false;
229 bool _closed = false; 206 bool _closed = false;
230 } 207 }
OLDNEW
« no previous file with comments | « lib/io/socket.dart ('k') | runtime/bin/builtin_impl_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698