Which code segment should you use?

You are writing a method to compress an array of bytes.
The array is passed to the method in a parameter named document.
You need to compress the incoming array of bytes and return the result as an array of bytes.
Which code segment should you use?

You are writing a method to compress an array of bytes.
The array is passed to the method in a parameter named document.
You need to compress the incoming array of bytes and return the result as an array of bytes.
Which code segment should you use?

A.
MemoryStream strm = new MemoryStream(document);
DeflateStream deflate = new DeflateStream(strm, CompressionMode.Compress);
byte[] result = new byte[document.Lenght];
deflate.Write(result, 0, result.Lenght);
return result;

B.
MemoryStream strm = new MemoryStream(document);
DeflateStream deflate = new DeflateStream(strm, CompressionMode.Compress);
deflate.Write(document, 0, document.Lenght);
deflate.Close();
return strm.ToArray();

C.
MemoryStream strm = new MemoryStream();
DeflateStream deflate = new DeflateStream(strm, CompressionMode.Compress);
deflate.Write(document, 0, document.Lenght);
deflate.Close();
return strm.ToArray();

D.
MemoryStream inStream = new MemoryStream(document);
DeflateStream deflate = new DeflateStream(inStream, CompressionMode.Compress);
MemoryStream outStream = new MemoryStream();
int b;
while ((b = deflate.ReadByte()) != -1) {
outStream. WriteByte((byte)b);
}
return outStream.ToArray();

Explanation:
The document is compressed and written to a new MemoryStream using the Deflate class.
Finally the compressed data can be returned as an array of bytes using the ToArray method of the MemoryStream.
A does not compress and write the document, instead it is compressing and writing an empty array
B & D are reading and writing to the same document.



Leave a Reply 0

Your email address will not be published. Required fields are marked *