concept InputStream in category dart

appears as: InputStream, n InputStream, InputStream
Dart in Action

This is an excerpt from Manning's book Dart in Action.

Behind the readAsString() method, File uses an InputStream type, which returns sequential data in a buffered, nonblocking fashion. The data returned by the InputStream’s read() method is a List<int>, but it represents an array of bytes, each byte representing a single byte in the TryOptions.dart file.

Listing 13.3 uses an InputStream to implement the outputFile() function. The InputStream’s onData() method is called repeatedly when data is available; you access that data by calling the InputStream’s read() function, which may return data or may return null if no more data is available, as shown in figure 13.6.

Figure 13.6. Reading data from a file’s InputStream

When no more data is available, the onClosed() method is called, allowing you to output the complete data to the console. You use a StringBuffer type to store the multiple strings created by each call to onData(). The contents of the StringBuffer’s internal list of strings is efficiently converted into a single string when you call the StringBuffer’s toString() function. The following listing uses a file’s InputStream in the outputFile() function and also contains the existing listDir() function you saw earlier in the chapter, for completeness.

Listing 13.3. Implementing outputFile() with an InputStream
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage