programming c# 4.0.pdf

Seekoriginbegin means the beginning of the file

Info icon This preview shows pages 444–446. Sign up to view the full content.

SeekOrigin.Begin means the beginning of the file, SeekOrigin.End means the end of the file (and so the offset counts backward—you don’t need to say −100 , just 100 ). There’s also SeekOrigin.Current which allows you to move relative to the current po- sition. You could use this to read 10 bytes ahead, for example (maybe to work out what you were looking at in context), and then seek back to where you were by calling Seek(-10, SeekOrigin.Current) . Not all streams support seeking. For example, some streams represent network connections, which you might use to download gigabytes of data. The .NET Framework doesn’t remember every single byte just in case you ask it to seek later on, so if you attempt to rewind such a stream, Seek will throw a NotSupportedException . You can find out whether seeking is supported from a stream’s CanSeek property. 420 | Chapter 11: Files and Streams
Image of page 444

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Writing Data with Streams We don’t just have to use streaming APIs for reading. We can write to the stream, too. One very common programming task is to copy data from one stream to another. We use this kind of thing all the time—copying data, or concatenating the content of several files into another, for example. (If you want to copy an entire file, you’d use File.Copy , but streams give you the flexibility to concatenate or modify data, or to work with nonfile sources.) Example 11-46 shows how to read data from one stream and write it into another. This is just for illustrative purposes—.NET 4 added a new CopyTo method to Stream which does this for you. In practice you’d need Example 11-46 only if you were targeting an older version of the .NET Framework, but it’s a good way to see how to write to a stream. Example 11-46. Copying from one stream to another private static void WriteTo(Stream source, Stream target, int bufferLength) { bufferLength = Math.Max(100, bufferLength); var buffer = new byte[bufferLength]; int bytesRead; do { bytesRead = source. Read (buffer, 0, buffer.Length); if (bytesRead != 0) { target. Write (buffer, 0, bytesRead); } } while (bytesRead > 0); } We create a buffer which is at least 100 bytes long. We then Read from the source and Write to the target, using the buffer as the intermediary. Notice that the Write method takes the same parameters as the read: the buffer, an offset into that buffer, and the number of bytes to write (which in this case is the number of bytes read from the source buffer, hence the slightly confusing variable name). As with Read , it steadily advances the current position in the stream as it writes, just like that ticker tape. Unlike Read , Write will always process as many bytes as we ask it to, so with Write , there’s no need to keep looping round until it has written all the data. Obviously, we need to keep looping until we’ve read everything from the source stream.
Image of page 445
Image of page 446
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern