And hence it is necessary to dynamically expand the byte array in blocks to hold the decompressed data during runtime. Reserving too large a block wastes memory, and reserving too small a block loses valuable time while you continually expand the byte array. It is therefore up to the calling routine to determine the optimal block size to read.

The block size is the size of the compressed data (data.Length):

//---the de-compressed data is stored in zipStream;

// extract them out into a byte array---

dc_data = RetrieveBytesFromStream(zipStream, data.Length);

In most cases, the uncompressed data is a few times larger than the compressed data, so you would at most expand the byte array dynamically during runtime a couple of times. For instance, suppose that the compression ratio is 20% and the size of the compressed data is 2MB. In that case, the uncompressed data would be 10MB, and the byte array would be expanded dynamically five times. Ideally, the byte array should not be expanded too frequently during runtime because it severely slows down the application. Using the size of the compressed data as a block size is a good compromise.

Use the following statements to test the Compress() and Decompress() functions:

static void Main(string[] args) {

 byte[] compressedData =

  Compress('Gzip', System.Text.Encoding.ASCII.GetBytes(

  'This is a uncompressed string'));

 Console.WriteLine('Compressed: {0}',

  ASCIIEncoding.ASCII.GetString(compressedData));

 Console.WriteLine('Uncompressed: {0}',

  ASCIIEncoding.ASCII.GetString(Decompress('Gzip', compressedData)));

 Console.ReadLine();

}

The output is as shown in Figure 11-9.

Figure 11-9

The compressed data contains some unprintable characters, so you may hear some beeps when it prints. To display the compressed data using printable characters, you can define two helper functions — byteArrayToString() and stringToByteArray():

//---converts a byte array to a string---

 static string byteArrayToString(byte[] data) {

 //---copy the compressed data into a string for presentation---

 System.Text.StringBuilder s = new System.Text.StringBuilder();

 for (int i = 0; i <= data.Length - 1; i++) {

  if (i != data.Length - 1) s.Append(data[i] + ' ');

  else s.Append(data[i]);

 }

 return s.ToString();

}

//---converts a string into a byte array---

static byte[] stringToByteArray(string str) {

 //---format the compressed string into a byte array---

 string[] eachByte = str.Split(' ');

 byte[] data = new byte[eachByte.Length];

 for (int i = 0; i <= eachByte.Length - 1; i++)

  data[i] = Convert.ToByte(eachByte[i]);

 return data;

}

To use the two helper functions, make the following changes to the statements:

static void Main(string[] args) {

 byte[] compressedData =

  Compress('Gzip', System.Text.Encoding.ASCII.GetBytes(

  'This is a uncompressed string'));

 string compressedDataStr = byteArrayToString (compressedData);

 Console.WriteLine('Compressed: {0}', compressedDataStr);

 byte[] data = stringToByteArray(compressedDataStr);

 Console.WriteLine('Uncompressed: {0}',

  ASCIIEncoding.ASCII.GetString(Decompress('Gzip', data)));

 Console.ReadLine();

}

Figure 11-10 shows the output when using the two helper functions.

Figure 11-10 

Alternatively, you can also convert the compressed data to a Base64-encoded string, like this:

byte[] compressedData =

 Compress('Gzip', System.Text.Encoding.ASCII.GetBytes(

 'This is a uncompressed string'));

string compressedDataStr = Convert.ToBase64String (compressedData);

Console.WriteLine('Compressed: {0}', compressedDataStr);

byte[] data = Convert.FromBase64String ((compressedDataStr));

Console.WriteLine('Uncompressed: {0}',

ASCIIEncoding.ASCII.GetString(Decompress('Gzip', data)));

Figure 11-11 shows the output using the base64 encoding.

Figure 11-11

Serialization

Many a time you may need to persist the value of an object to secondary storage. For example, you may want to save the values of a couple of Point objects representing the positioning of an item on- screen to secondary storage. The act of 'flattening' an object into a serial form is known as serialization. The .NET Framework supports binary and XML serialization.

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату