Synchronously read a line from stdin. This call will block until a full line is available.
The argument encoding
can be used to changed how the input should be decoded. Default is systemEncoding.
If retainNewlines
is false
, the returned String will not contain the final newline. If true
, the returned String will contain the line terminator. Default is false
.
If end-of-file is reached after any bytes have been read from stdin, that data is returned. Returns null
if no bytes preceded the end of input.
String readLineSync( {Encoding encoding: systemEncoding, bool retainNewlines: false}) { const CR = 13; const LF = 10; final List<int> line = <int>[]; // On Windows, if lineMode is disabled, only CR is received. bool crIsNewline = Platform.isWindows && (stdioType(stdin) == StdioType.terminal) && !lineMode; if (retainNewlines) { int byte; do { byte = readByteSync(); if (byte < 0) { break; } line.add(byte); } while (byte != LF && !(byte == CR && crIsNewline)); if (line.isEmpty) { return null; } } else if (crIsNewline) { // CR and LF are both line terminators, neither is retained. while (true) { int byte = readByteSync(); if (byte < 0) { if (line.isEmpty) return null; break; } if (byte == LF || byte == CR) break; line.add(byte); } } else { // Case having to handle CR LF as a single unretained line terminator. outer: while (true) { int byte = readByteSync(); if (byte == LF) break; if (byte == CR) { do { byte = readByteSync(); if (byte == LF) break outer; line.add(CR); } while (byte == CR); // Fall through and handle non-CR character. } if (byte < 0) { if (line.isEmpty) return null; break; } line.add(byte); } } return encoding.decode(line); }
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-io/Stdin/readLineSync.html