800b4b6c37
For the dart:io types InputStream and OutputStream the top level function stdioType will return whether the stream is attached to a terminal, pipe, file or something else. BUG=dart:2789 R=ager@google.com Review URL: https://codereview.chromium.org//11773041 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@16839 260f80e4-7a28-3924-810f-c04153c831b5
48 lines
1.5 KiB
Dart
48 lines
1.5 KiB
Dart
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
patch class _StdIOUtils {
|
|
static InputStream _getStdioInputStream() {
|
|
switch (_getStdioHandleType(0)) {
|
|
case _STDIO_HANDLE_TYPE_TERMINAL:
|
|
case _STDIO_HANDLE_TYPE_PIPE:
|
|
case _STDIO_HANDLE_TYPE_SOCKET:
|
|
Socket s = new _Socket._internalReadOnly();
|
|
_getStdioHandle(s, 0);
|
|
s._closed = false;
|
|
return s.inputStream;
|
|
case _STDIO_HANDLE_TYPE_FILE:
|
|
return new _FileInputStream.fromStdio(0);
|
|
default:
|
|
throw new FileIOException("Unsupported stdin type");
|
|
}
|
|
}
|
|
|
|
static OutputStream _getStdioOutputStream(int fd) {
|
|
assert(fd == 1 || fd == 2);
|
|
switch (_getStdioHandleType(fd)) {
|
|
case _STDIO_HANDLE_TYPE_TERMINAL:
|
|
case _STDIO_HANDLE_TYPE_PIPE:
|
|
case _STDIO_HANDLE_TYPE_SOCKET:
|
|
Socket s = new _Socket._internalWriteOnly();
|
|
_getStdioHandle(s, fd);
|
|
s._closed = false;
|
|
return s.outputStream;
|
|
case _STDIO_HANDLE_TYPE_FILE:
|
|
return new _FileOutputStream.fromStdio(fd);
|
|
default:
|
|
throw new FileIOException("Unsupported stdin type");
|
|
}
|
|
}
|
|
|
|
static int _socketType(Socket socket) {
|
|
return _getSocketType(socket);
|
|
}
|
|
}
|
|
|
|
|
|
_getStdioHandle(Socket socket, int num) native "Socket_GetStdioHandle";
|
|
_getStdioHandleType(int num) native "File_GetStdioHandleType";
|
|
_getSocketType(Socket socket) native "Socket_GetType";
|