它在这里说msdn.microsoft.com/en-us/library/system.io.stream.read.aspx表示Stream.Read
和Stream.Write
方法都自动提前在流中的位置/偏移量为什么这里的例子http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx和http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx手动更改偏移?设置流中的偏移量
如果知道流的大小,是否只在循环中设置偏移量,如果不知道大小并使用缓冲区,则将其设置为0?
// Now read s into a byte buffer.
byte[] bytes = new byte[s.Length];
int numBytesToRead = (int) s.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to 10.
int n = s.Read(bytes, numBytesRead, 10);
// The end of the file is reached.
if (n == 0)
{
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
和
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
......什么? –
Stream中的内部偏移量与在缓冲区和流之间读取/写入数据时需要关心的偏移量/长度(可能是2.流)之间存在差异。 – nos
@sehe:责备你的操作系统。 –