OLD | NEW |
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>> { | 13 class _StdStream extends Stream<List<int>> { |
14 final Stream<List<int>> _stdin; | 14 final Stream<List<int>> _stream; |
15 | 15 |
16 _Stdin(Stream<List<int>> this._stdin); | 16 _StdStream(Stream<List<int>> this._stream); |
17 | 17 |
18 StreamSubscription<List<int>> listen(void onData(List<int> event), | 18 StreamSubscription<List<int>> listen(void onData(List<int> event), |
19 {void onError(AsyncError error), | 19 {void onError(AsyncError error), |
20 void onDone(), | 20 void onDone(), |
21 bool unsubscribeOnError}) { | 21 bool unsubscribeOnError}) { |
22 return _stdin.listen( | 22 return _stream.listen( |
23 onData, | 23 onData, |
24 onError: onError, | 24 onError: onError, |
25 onDone: onDone, | 25 onDone: onDone, |
26 unsubscribeOnError: unsubscribeOnError); | 26 unsubscribeOnError: unsubscribeOnError); |
27 } | 27 } |
28 } | 28 } |
29 | 29 |
30 class _StdSink implements IOSink { | 30 class _StdSink implements IOSink { |
31 final IOSink _ioSink; | 31 final IOSink _sink; |
32 | 32 |
33 _StdSink(IOSink this._ioSink); | 33 _StdSink(IOSink this._sink); |
34 | 34 |
35 Encoding get encoding => _ioSink.encoding; | 35 Encoding get encoding => _sink.encoding; |
36 void set encoding(Encoding encoding) { | 36 void set encoding(Encoding encoding) { |
37 _ioSink.encoding = encoding; | 37 _sink.encoding = encoding; |
38 } | 38 } |
39 void write(object) => _ioSink.write(object); | 39 void write(object) => _sink.write(object); |
40 void writeln([object = "" ]) => _ioSink.writeln(object); | 40 void writeln([object = "" ]) => _sink.writeln(object); |
41 void writeAll(objects, [sep = ""]) => _ioSink.writeAll(objects, sep); | 41 void writeAll(objects, [sep = ""]) => _sink.writeAll(objects, sep); |
42 void writeBytes(List<int> data) => _ioSink.writeBytes(data); | 42 void writeBytes(List<int> data) => _sink.writeBytes(data); |
43 void writeCharCode(int charCode) => _ioSink.writeCharCode(charCode); | 43 void writeCharCode(int charCode) => _sink.writeCharCode(charCode); |
44 Future consume(Stream<List<int>> stream) => _ioSink.consume(stream); | 44 Future consume(Stream<List<int>> stream) => _sink.consume(stream); |
45 Future addStream(Stream<List<int>> stream) => _ioSink.addStream(stream); | 45 Future addStream(Stream<List<int>> stream) => _sink.addStream(stream); |
46 Future writeStream(Stream<List<int>> stream) | 46 Future writeStream(Stream<List<int>> stream) => _sink.writeStream(stream); |
47 => _ioSink.writeStream(stream); | 47 Future close() => _sink.close(); |
48 Future close() => _ioSink.close(); | 48 Future get done => _sink.done; |
49 Future get done => _ioSink.done; | |
50 } | 49 } |
51 | 50 |
52 class StdioType { | 51 class StdioType { |
53 static const StdioType TERMINAL = const StdioType._("terminal"); | 52 static const StdioType TERMINAL = const StdioType._("terminal"); |
54 static const StdioType PIPE = const StdioType._("pipe"); | 53 static const StdioType PIPE = const StdioType._("pipe"); |
55 static const StdioType FILE = const StdioType._("file"); | 54 static const StdioType FILE = const StdioType._("file"); |
56 static const StdioType OTHER = const StdioType._("other"); | 55 static const StdioType OTHER = const StdioType._("other"); |
57 final String name; | 56 final String name; |
58 const StdioType._(String this.name); | 57 const StdioType._(String this.name); |
59 String toString() => "StdioType: $name"; | 58 String toString() => "StdioType: $name"; |
(...skipping 23 matching lines...) Expand all Loading... |
83 | 82 |
84 IOSink get stderr { | 83 IOSink get stderr { |
85 if (_stderr == null) { | 84 if (_stderr == null) { |
86 _stderr = _StdIOUtils._getStdioOutputStream(2); | 85 _stderr = _StdIOUtils._getStdioOutputStream(2); |
87 } | 86 } |
88 return _stderr; | 87 return _stderr; |
89 } | 88 } |
90 | 89 |
91 | 90 |
92 StdioType stdioType(object) { | 91 StdioType stdioType(object) { |
93 if (object is _Stdin) { | 92 if (object is _StdStream) { |
94 object = object._stdin; | 93 object = object._stream; |
95 } else if (object is _StdSink) { | 94 } else if (object is _StdSink) { |
96 object = object._ioSink; | 95 object = object._sink; |
97 } | 96 } |
98 if (object is _FileStream) { | 97 if (object is _FileStream) { |
99 return StdioType.FILE; | 98 return StdioType.FILE; |
100 } | 99 } |
101 if (object is Socket) { | 100 if (object is Socket) { |
102 switch (_StdIOUtils._socketType(object._nativeSocket)) { | 101 switch (_StdIOUtils._socketType(object._nativeSocket)) { |
103 case _STDIO_HANDLE_TYPE_TERMINAL: return StdioType.TERMINAL; | 102 case _STDIO_HANDLE_TYPE_TERMINAL: return StdioType.TERMINAL; |
104 case _STDIO_HANDLE_TYPE_PIPE: return StdioType.PIPE; | 103 case _STDIO_HANDLE_TYPE_PIPE: return StdioType.PIPE; |
105 case _STDIO_HANDLE_TYPE_FILE: return StdioType.FILE; | 104 case _STDIO_HANDLE_TYPE_FILE: return StdioType.FILE; |
106 } | 105 } |
107 } | 106 } |
108 if (object is IOSink) { | 107 if (object is IOSink) { |
109 try { | 108 try { |
110 if (object._target is _FileStreamConsumer) { | 109 if (object._target is _FileStreamConsumer) { |
111 return StdioType.FILE; | 110 return StdioType.FILE; |
112 } | 111 } |
113 } catch (e) { | 112 } catch (e) { |
114 // Only the interface implemented, _sink not available. | 113 // Only the interface implemented, _sink not available. |
115 } | 114 } |
116 } | 115 } |
117 return StdioType.OTHER; | 116 return StdioType.OTHER; |
118 } | 117 } |
119 | 118 |
120 | 119 |
121 class _StdIOUtils { | 120 class _StdIOUtils { |
122 external static IOSink _getStdioOutputStream(int fd); | 121 external static IOSink _getStdioOutputStream(int fd); |
123 external static Stream<List<int>> _getStdioInputStream(); | 122 external static Stream<List<int>> _getStdioInputStream(); |
124 external static int _socketType(nativeSocket); | 123 external static int _socketType(nativeSocket); |
125 } | 124 } |
OLD | NEW |