我通过二进制阅读器以流的形式读入一个文件。我读入了一些字节,但当检查基本流位置时,它比它应该的长度要远得多。
代码示例:
public void MainLoader()
{
FileStream input = System.IO.File.Open(path, System.IO.FileMode.Open);
BinaryReader binaryReader = new BinaryReader(input, Encoding.GetEncoding(1252));
byte[] bytes = binaryReader.ReadBytes(256); // 256 bytes
binaryReader.ReadInt32(); //4 bytes
binaryReader.ReadInt32(); //4 bytes
binaryReader.ReadInt16(); //2 bytes
binaryReader.ReadInt32(); //4 bytes
binaryReader.ReadInt16(); //2 bytes
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadBoolean(); // 1 byte
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadInt16(); //2 bytes
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadBoolean(); // 1 byte
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadInt16(); //2 bytes
binaryReader.ReadInt16(); //2 bytes
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadByte(); // 1 byte
binaryReader.ReadByte(); // 1 byte
// should return 292
var position = binaryReader.BaseStream.Position
binaryReader.Close();
}当通过winform应用程序运行时,它可以正常工作,postion为292。当通过Xamarin.Android应用程序运行时,它会变得一团糟,并返回为4096。
我已经尝试使用默认编码和UTF8编码,没有做任何更改。我开始怀疑是不是有一些CultureInfo设置
当代码实际读取292字节时,流是如何到达这个位置的?
发布于 2018-09-08 19:05:32
对于使用启用了调试可视化工具的DevExpress代码刷新的任何人,这会自动检查使用流的代码的结果,例如BinaryReader,这会导致在执行实际行时流处于错误的位置。在我的例子中(也是一个Xamarin.Android应用程序),我正在使用二进制读取器从我的流中读取所有字节,当我试图围绕代码进行调试时,自动检查正在移动位置,并在我继续执行读取序列时导致“无法在流的末尾读取”。
对于Coderush用户来说,解决这个问题的唯一方法是关闭Debug Visualizer,或者确保在启用Debug Visualizer时不中断和单步执行使用流的代码行。
https://stackoverflow.com/questions/40784930
复制相似问题