FILES, STREAMS, AND IO in C#

Creating DataMatrix in C# FILES, STREAMS, AND IO

CHAPTER 20 FILES, STREAMS, AND IO
Data Matrix ECC200 Generation In C#
Using Barcode encoder for Visual Studio .NET Control to generate, create Data Matrix image in .NET framework applications.
www.OnBarcode.com
Data Matrix ECC200 Decoder In Visual C#
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
It is important to call the Close method when you have finished with a Stream. This ensures that the resources that have been tied up in the data store are released and can be used again. Compiling and running Listing 20-16 produces the following results: CanRead: True CanWrite: True CanSeek: True Writing value: Writing value: Writing value: Writing value: Writing value: Read value: 0 Read value: 1 Read value: 2 Read value: 3 Read value: 4 Writing value: Writing value: Writing value: Writing value: Writing value: Press enter to
Barcode Creator In Visual C#
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Make European Article Number 13 In Visual C#.NET
Using Barcode generator for .NET Control to generate, create UPC - 13 image in Visual Studio .NET applications.
www.OnBarcode.com
0 1 2 3 4
GS1 - 12 Maker In Visual C#
Using Barcode encoder for .NET framework Control to generate, create Universal Product Code version A image in Visual Studio .NET applications.
www.OnBarcode.com
Code 128 Code Set B Generator In C#.NET
Using Barcode maker for .NET Control to generate, create Code-128 image in .NET framework applications.
www.OnBarcode.com
10 11 12 13 14 finish
Drawing UCC - 12 In C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create EAN128 image in .NET framework applications.
www.OnBarcode.com
Paint C 2 Of 5 In Visual C#.NET
Using Barcode creation for VS .NET Control to generate, create Industrial 2 of 5 image in .NET framework applications.
www.OnBarcode.com
Using Base Streams
Generating ECC200 In None
Using Barcode creation for Office Word Control to generate, create Data Matrix 2d barcode image in Office Word applications.
www.OnBarcode.com
DataMatrix Drawer In Java
Using Barcode encoder for Java Control to generate, create Data Matrix image in Java applications.
www.OnBarcode.com
The .NET Framework Class Library contains a number of base streams, each of which works with a different kind of data store. In this section, we ll look at two in detail. They work with an in-memory store and files on disk, and we ll see examples of another kind in 21 when we come to look at networking. Streams that have data stores are called base streams, and they are said to be backed by that store. For example, a FileStream object has a file as its data store, so a FileStream is said to be backed by a file. FileStream is one of the backing stream classes we will look at in this section. The other is MemoryStream, which is backed by an array of bytes held in memory.
Barcode Recognizer In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Create UPCA In None
Using Barcode generation for Software Control to generate, create UPC Symbol image in Software applications.
www.OnBarcode.com
Using the MemoryStream Class
Encode EAN / UCC - 13 In None
Using Barcode encoder for Software Control to generate, create EAN13 image in Software applications.
www.OnBarcode.com
EAN13 Generation In Java
Using Barcode generation for Java Control to generate, create EAN-13 image in Java applications.
www.OnBarcode.com
The MemoryStream class is backed by a byte array held in memory. This means that the data you write to the stream is not persistent and will be lost when your program finishes or the MemoryStream object is destroyed by the garbage collector. You obtain MemoryStream objects by using the MemoryStream class constructor. Table 20-22 describes the constructors for this class.
Decoding Code 3 Of 9 In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Generating GS1 - 13 In None
Using Barcode creator for Font Control to generate, create EAN-13 image in Font applications.
www.OnBarcode.com
CHAPTER 20 FILES, STREAMS, AND IO
Draw PDF-417 2d Barcode In None
Using Barcode printer for Office Excel Control to generate, create PDF-417 2d barcode image in Excel applications.
www.OnBarcode.com
Barcode Printer In None
Using Barcode generator for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Table 20-22. MemoryStream Constructors
Universal Product Code Version A Maker In Java
Using Barcode encoder for Android Control to generate, create UCC - 12 image in Android applications.
www.OnBarcode.com
Printing Data Matrix In Objective-C
Using Barcode encoder for iPhone Control to generate, create Data Matrix image in iPhone applications.
www.OnBarcode.com
Constructor
MemoryStream()
Description
Creates an automatically resizing MemoryBuffer with no initial content. Creates an automatically resizing MemoryBuffer with no initial content and an initial capacity specified by the parameter value. Creates a fixed-capacity MemoryBuffer using the byte array as the initial content. The capacity is fixed to the size of the array.
MemoryStream(int)
MemoryStream(byte[])
If you create a MemoryStream using the default constructor or the version that takes an int parameter, then the byte array that backs the stream will be resized to accommodate any data that you write. If you use the constructor that takes a byte array, then the capacity of the stream is fixed, but the initial content of the stream is set to be the content of the array. In addition to the members defined by the Stream class, MemoryStream has some additional members that are specific to a memory-backed stream. You can t use these members if you have upcast to the base class, but they can be helpful if you are working with the MemoryStream type directly. The additional members introduced by the MemoryStream class are described in Table 20-23. Table 20-23. Additional MemoryStream Members
Member
Capacity
Description
Gets or sets the size of the byte array that backs the MemoryStream. Returns a byte array containing the contents of the stream. Bytes that have been allocated in the array but that have not been assigned a value are omitted. Writes the contents of the MemoryStream to another Stream object.
ToArray()
WriteTo(Stream)
Listing 20-17 demonstrates the use of the MemoryStream class. Listing 20-17. Using the MemoryStream Class using System; using System.IO; class Listing 17 { static void Main(string[] args) {
CHAPTER 20 FILES, STREAMS, AND IO
// create an empty MemoryStream MemoryStream myStream = new MemoryStream(); // write a series of bytes to the stream for (int i = 0; i < 5; i++) { myStream.WriteByte((byte)i); } // reposition the cursor to the start of the stream myStream.Seek(0, SeekOrigin.Begin); // read back the byte values for (int value; (value = myStream.ReadByte()) > -1; ) { Console.WriteLine("Read value: {0}", value); } // get the data in the stream as an array byte[] dataArray = myStream.ToArray(); // create a new memory stream using the dataArray MemoryStream myOtherStream = new MemoryStream(dataArray); // write out the capacity of the stream Console.WriteLine("Capacity: {0}", myOtherStream.Capacity); // read the data back from the stream // read back the byte values for (int i = 0; i < 5; i++) { Console.WriteLine("Read value from second stream: {0}", myOtherStream.ReadByte()); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } Listing 20-17 creates a MemoryStream and writes a series of byte values to it. These values are then read back, converted to a byte array, and used as the basis for a second MemoryStream. When reading the data from the first MemoryStream, I read all the available data by detecting the -1 value that is returned when the end of the stream is reached (as opposed to reading a fixed number of bytes as in the previous example): for (int value; (value = myStream.ReadByte()) > -1; ) { Console.WriteLine("Read value: {0}", value); } I have used a for loop where the condition reads a byte, assigns it to a variable, and checks for the -1 value in a single statement. This is possible in C# because the result of the assignment operator (=) is the value that is being assigned. In the example, this means that the value of assigning a byte value to the value variable is the byte value. Compiling and running Listing 20-17 produces the following results:
Copyright © OnBarcode.com . All rights reserved.