I'm currently writing a binary reader that reads a value from a BinaryReader
at a given position. For that I have to store the current position of the MemoryStream
so I can restore it afterwards.
public byte GetByte(int position)
{
long oldPosition = this.memoryStream.Position;
this.memoryStream.Position = position;
byte value = this.ReadByte();
this.memoryStream.Position = oldPosition;
return value;
}
EDIT: As requested, more similar functions:
public byte[] GetByteArray(int position, int length)
{
long oldPosition = this.memoryStream.Position;
this.memoryStream.Position = position;
byte[] value = this.ReadByteArray(length);
this.memoryStream.Position = oldPosition;
return value;
}
public bool GetBoolean(int position)
{
long oldPosition = this.memoryStream.Position;
this.memoryStream.Position = position;
bool value = this.ReadBoolean();
this.memoryStream.Position = oldPosition;
return value;
}
public ushort GetUInt16(int position)
{
long oldPosition = this.memoryStream.Position;
this.memoryStream.Position = position;
ushort value = this.ReadUInt16();
this.memoryStream.Position = oldPosition;
return value;
}
and so on..
Is there any way to shorten this process and avoid doubled-code since I have many functions similar to that?
Stream.Position
. If you can be assured that it's only ever aMemoryStream
that you'll be working with, you could produce a thread-safe implementation by working with thebyte
array (MemoryStream.GetBuffer()
) and using BitConverter to read values. \$\endgroup\$