我被一个使用可写System.IO.MemoryStream的基本实验搞砸了,这个实验基于一个字节数组,提供一个ArgumentException。
newBytes使用文字初始化。ms由数组初始化,可写标志设置为True。VB.net
Try
Dim newBytes() As Byte = {0, 128, 255, 128, 0}
Dim ms As New System.IO.MemoryStream(newBytes, True)
ms.Write({CByte(4)}, 1, 1)
Catch ex as Exception
End TryC#.net
try
byte() newBytes = {0, 128, 255, 128, 0};
System.IO.MemoryStream ms = new System.IO.MemoryStream(newBytes, true);
ms.Write(byte(4), 1, 1);
catch Exception ex
end try异常是文本“偏移量和长度超出界限的数组或计数大于从索引到源集合末尾的元素数”的ArgumentException。
显然,内存流有Length: 5,在位置1处写一个字节应该是完全可行的,为什么会有异常呢?
发布于 2018-10-04 19:49:36
MemoryStream.Write方法有三个参数:
buffer -写入数据的缓冲区offset -缓冲区中基于零的字节偏移量,用于开始将字节复制到当前流。count -要写入的最大字节数注意,第二个参数是输入数组中的偏移量,而不是输出数组中的偏移量。MemoryStream.Position属性确定输出中的当前偏移量。
https://stackoverflow.com/questions/52654031
复制相似问题