Jay S

Jay S

  • NA
  • 230
  • 57.8k

Binaryreader returning wrong position

Nov 24 2016 5:50 AM
Hello.
 
I am reading in a file as a stream through a binaryreader. I read in a number of bytes but when checking the basestream position its a lot further a long than it should be.
 
Code example: 
  1. public void MainLoader()  
  2. {  
  3.     FileStream input = System.IO.File.Open(path, System.IO.FileMode.Open);  
  4.     BinaryReader binaryReader = new BinaryReader(input, Encoding.GetEncoding(1252));  
  5.     byte[] bytes = binaryReader.ReadBytes(256); // 256 bytes  
  6.     binaryReader.ReadInt32(); //4 bytes  
  7.     binaryReader.ReadInt32(); //4 bytes  
  8.     binaryReader.ReadInt16(); //2 bytes  
  9.     binaryReader.ReadInt32(); //4 bytes  
  10.     binaryReader.ReadInt16(); //2 bytes  
  11.     binaryReader.ReadByte(); // 1 byte  
  12.     binaryReader.ReadBoolean(); // 1 byte  
  13.     binaryReader.ReadByte(); // 1 byte  
  14.     binaryReader.ReadByte(); // 1 byte  
  15.     binaryReader.ReadInt16(); //2 bytes  
  16.     binaryReader.ReadByte(); // 1 byte  
  17.     binaryReader.ReadBoolean(); // 1 byte  
  18.     binaryReader.ReadByte(); // 1 byte  
  19.     binaryReader.ReadByte(); // 1 byte  
  20.     binaryReader.ReadByte(); // 1 byte  
  21.     binaryReader.ReadInt16(); //2 bytes  
  22.     binaryReader.ReadInt16(); //2 bytes  
  23.     binaryReader.ReadByte(); // 1 byte  
  24.     //New for 2017  
  25.     binaryReader.ReadByte(); // 1 byte  
  26.     binaryReader.ReadByte(); // 1 byte  
  27.     binaryReader.ReadByte(); // 1 byte  
  28.     binaryReader.ReadByte(); // 1 byte  
  29.     // should return 292  
  30.     var position = binaryReader.BaseStream.Position  
  31.     binaryReader.Close();  
  32. }  
 Adding up all the read in bytes should be 292, and this should be the stream position too ( I read from the start of the file)
 
The issue I have is that in fact it returns 4096. I do not understand this, what could be influences this.
 
I have thought maybe Culture settings, Encoding, or Endians, but I am no sure.
 
Any thoughts would be great.