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

Side by Side Diff: sdk/lib/io/stdio.dart

Issue 13787003: Wrap stdout/stderr/stdin to only expose the relevant methods. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 | « runtime/bin/stdio_patch.dart ('k') | no next file » | 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) 2013, the Dart project authors. Please see the AUTHORS file 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 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 part of dart.io; 5 part of dart.io;
6 6
7 const int _STDIO_HANDLE_TYPE_TERMINAL = 0; 7 const int _STDIO_HANDLE_TYPE_TERMINAL = 0;
8 const int _STDIO_HANDLE_TYPE_PIPE = 1; 8 const int _STDIO_HANDLE_TYPE_PIPE = 1;
9 const int _STDIO_HANDLE_TYPE_FILE = 2; 9 const int _STDIO_HANDLE_TYPE_FILE = 2;
10 const int _STDIO_HANDLE_TYPE_SOCKET = 3; 10 const int _STDIO_HANDLE_TYPE_SOCKET = 3;
11 const int _STDIO_HANDLE_TYPE_OTHER = 4; 11 const int _STDIO_HANDLE_TYPE_OTHER = 4;
12 12
13 class _Stdin extends Stream<List<int>> {
14 final Stream<List<int>> _stdin;
15
16 _Stdin(Stream<List<int>> this._stdin);
17
18 StreamSubscription<List<int>> listen(void onData(List<int> event),
19 {void onError(AsyncError error),
20 void onDone(),
21 bool unsubscribeOnError}) {
22 return _stdin.listen(
23 onData,
24 onError: onError,
25 onDone: onDone,
26 unsubscribeOnError: unsubscribeOnError);
27 }
28 }
29
30 class _StdSink implements IOSink {
31 final IOSink _ioSink;
32
33 _StdSink(IOSink this._ioSink);
34
35 Encoding get encoding => _ioSink.encoding;
36 void set encoding(Encoding encoding) {
37 _ioSink.encoding = encoding;
38 }
39 void write(object) => _ioSink.write(object);
40 void writeln([object = "" ]) => _ioSink.writeln(object);
41 void writeAll(objects, [sep = ""]) => _ioSink.writeAll(objects, sep);
42 void writeBytes(List<int> data) => _ioSink.writeBytes(data);
43 void writeCharCode(int charCode) => _ioSink.writeCharCode(charCode);
44 Future<T> consume(Stream<List<int>> stream) => _ioSink.consume(stream);
ahe 2013/04/08 12:25:25 The use of T without having T declared causes a wa
Anders Johnsen 2013/06/11 12:15:04 Yeah, bad testing from my side. Sorry https://cod
45 Future<T> addStream(Stream<List<int>> stream) => _ioSink.addStream(stream);
46 Future<T> writeStream(Stream<List<int>> stream)
47 => _ioSink.writeStream(stream);
48 Future close() => _ioSink.close();
49 Future<T> get done => _ioSink.done;
50 }
13 51
14 class StdioType { 52 class StdioType {
15 static const StdioType TERMINAL = const StdioType._("terminal"); 53 static const StdioType TERMINAL = const StdioType._("terminal");
16 static const StdioType PIPE = const StdioType._("pipe"); 54 static const StdioType PIPE = const StdioType._("pipe");
17 static const StdioType FILE = const StdioType._("file"); 55 static const StdioType FILE = const StdioType._("file");
18 static const StdioType OTHER = const StdioType._("other"); 56 static const StdioType OTHER = const StdioType._("other");
19 final String name; 57 final String name;
20 const StdioType._(String this.name); 58 const StdioType._(String this.name);
21 String toString() => "StdioType: $name"; 59 String toString() => "StdioType: $name";
22 } 60 }
(...skipping 22 matching lines...) Expand all
45 83
46 IOSink get stderr { 84 IOSink get stderr {
47 if (_stderr == null) { 85 if (_stderr == null) {
48 _stderr = _StdIOUtils._getStdioOutputStream(2); 86 _stderr = _StdIOUtils._getStdioOutputStream(2);
49 } 87 }
50 return _stderr; 88 return _stderr;
51 } 89 }
52 90
53 91
54 StdioType stdioType(object) { 92 StdioType stdioType(object) {
93 if (object is _Stdin) {
94 object = object._stdin;
95 } else if (object is _StdSink) {
96 object = object._ioSink;
97 }
55 if (object is _FileStream) { 98 if (object is _FileStream) {
56 return StdioType.FILE; 99 return StdioType.FILE;
57 } 100 }
58 if (object is Socket) { 101 if (object is Socket) {
59 switch (_StdIOUtils._socketType(object._nativeSocket)) { 102 switch (_StdIOUtils._socketType(object._nativeSocket)) {
60 case _STDIO_HANDLE_TYPE_TERMINAL: return StdioType.TERMINAL; 103 case _STDIO_HANDLE_TYPE_TERMINAL: return StdioType.TERMINAL;
61 case _STDIO_HANDLE_TYPE_PIPE: return StdioType.PIPE; 104 case _STDIO_HANDLE_TYPE_PIPE: return StdioType.PIPE;
62 case _STDIO_HANDLE_TYPE_FILE: return StdioType.FILE; 105 case _STDIO_HANDLE_TYPE_FILE: return StdioType.FILE;
63 } 106 }
64 } 107 }
65 if (object is IOSink) { 108 if (object is IOSink) {
66 try { 109 try {
67 if (object._target is _FileStreamConsumer) { 110 if (object._target is _FileStreamConsumer) {
68 return StdioType.FILE; 111 return StdioType.FILE;
69 } 112 }
70 } catch (e) { 113 } catch (e) {
71 // Only the interface implemented, _sink not available. 114 // Only the interface implemented, _sink not available.
72 } 115 }
73 } 116 }
74 return StdioType.OTHER; 117 return StdioType.OTHER;
75 } 118 }
76 119
77 120
78 class _StdIOUtils { 121 class _StdIOUtils {
79 external static IOSink _getStdioOutputStream(int fd); 122 external static IOSink _getStdioOutputStream(int fd);
80 external static Stream<List<int>> _getStdioInputStream(); 123 external static Stream<List<int>> _getStdioInputStream();
81 external static int _socketType(nativeSocket); 124 external static int _socketType(nativeSocket);
82 } 125 }
OLDNEW
« no previous file with comments | « runtime/bin/stdio_patch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698