Which code segment should you use?

You need to write a code segment that transfers the first 80 bytes from a stream variable named stream1 into a new byte array named byteArray.
You also need to ensure that the code segment assigns the number of bytes that are transferred to an integer variable
named bytesTransferred.
Which code segment should you use?

You need to write a code segment that transfers the first 80 bytes from a stream variable named stream1 into a new byte array named byteArray.
You also need to ensure that the code segment assigns the number of bytes that are transferred to an integer variable
named bytesTransferred.
Which code segment should you use?

A.
bytesTransferred = stream1.Read(byteArray,0, 80);

B.
for (int i = 0; i <80; i++) {
stream1.WriteByte(byteArray[i]);
bytesTransferred = i;
if (!stream1.CanWrite) {
break;
}
}

C.
while (bytesTransferred < 80) {
stream1.Seek(1, SeekOrigin.Current);
byteArray[bytesTransferred++] = Convert.ToByte(stream1.ReadByte()); }

D.
stream1.Write(byteArray, 0, 80);
bytesTransferred = byteArray.Length;

Explanation:
The Read() method accepts a byte array and the start position and number of bytes to read as parameters.
B & D The question indicates that data should be read from the stream not written to it.
C it is unnecessary to attempt to read byte by byte, the Read() method provides a very efficient way of reading into a byte array.



Leave a Reply 1

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


mr_tienvu

mr_tienvu

I have the same idea. A