readAsString method
Reads all remaining bytes from this input stream and decodes them into a String.
This method reads the entire stream from the current position until the end
and then converts the bytes to a string using the specified encoding.
The default encoding is Closeable.DEFAULT_ENCODING.
Parameters
encoding: The Encoding used to decode the byte data. Defaults to Closeable.DEFAULT_ENCODING.
Returns
A String representing the decoded contents of the input stream.
Example
final input = FileInputStream('message.txt');
try {
final text = await input.readAsString(encoding: Closeable.DEFAULT_ENCODING);
print('File contents: $text');
} finally {
await input.close();
}
Throws
- StreamClosedException if the stream has been closed.
- IOException if an I/O or decoding error occurs.
Implementation
Future<String> readAsString([Encoding encoding = Closeable.DEFAULT_ENCODING]) async {
checkClosed();
try {
final bytes = await readAll();
return encoding.decode(bytes);
} on FormatException catch (e) {
throw IOException('Failed to decode stream: ${e.message}');
}
}