15.3. The BufferedStream Class

A buffer is a block of bytes in memory used to cache data, thereby reducing the number of calls to the operating system. Buffers thus improve read and write performance. A buffer can be used for either reading or writing, but never both simultaneously. This is similar to Java's java.io.BufferedInputStream and java.io.BufferedOutputStream. In Java a typical practice is to wrap streams into their buffered versions so as to reduce the I/O calls. The same principle holds true for C#. System.IO.BufferedStream wraps over other streams (which do the actual I/O—say, a FileStream). Wrapping the FileStream with a BufferedStream speeds up file read and write operations.

Listing 15.3 shows how to read the file created in

Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.