Answer the question
In order to leave comments, you need to log in
What's weird about Stream in .NET?
Hello. Studying the descendants of the Stream class, I came across one interesting feature that haunts me.
First, why does ReadByte() return an int? Outputting this int to the console, I somehow did not notice the octal number system.
Second, why does Read() take an array of bytes as its first parameter? What should be in this array? What does the method do with this array? There is no in or out before the argument.
Answer the question
In order to leave comments, you need to log in
ReadByte returns type int , because when the end is reached, the value minus one will be returned . And the byte type can have a value in the range from zero to 255 . That is, when reading bytes, it is not possible to report that the end of the stream has been reached using the byte type ( a byte having a value of zero can be a useful byte), so the int type is used .
The Read method accepts a buffer into which the read data will be placed. Buffer is an array of bytes. Initially, it must be empty. The size of the array is up to you. The larger the buffer size, the more data will be placed in memory, the fewer operations will be done.
The Read method returns the number of bytes buffered. Zero - the end of the stream has been reached.
FileInfo f = new FileInfo(@"C:\example.dat");
using (FileStream fs = f.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (BinaryReader br = new BinaryReader(fs))
{
int bytesRead = 0;
byte[] buffer = new byte[256]; // размер буфера 256 единиц байт
StringBuilder result = new StringBuilder();
while ((bytesRead = br.Read(buffer, 0, buffer.Length)) != 0) // читаем не более 256 единиц байт в buffer
{
// из buffer следует извлекать не более bytesRead (в конце это число может быть меньше 255)
}
}
}
> Outputting this int to the console, I somehow did not notice the octal number system.
Why should they? The value of a number is always the same and does not depend on the number system. But its display may change. You can even show with your fingers, or with knots on a rope.
> Second, why does Read() get an array of bytes as its first parameter? What should be in this array? What does the method do with this array? There is no in or out before the argument.
You should probably learn C# first and understand what a reference type is.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question