Ok before the code here is the skinny:
I am developing a tool which reads a binary file from a Game client, this binary file contains many fields (115) but the important ones are all strings that the file (db_item.rdb) stores in an area of 256 bytes. Most of these strings have a length lower than 10, so ~240 empty bytes are being processed 24 times per row @ 26k rows. (Don't blame me for this bad design, I didn't make the game :P)
So for each field I process I call a method readStream() which reads a MemoryStream I have previously created by loading a fore mentioned .rdb
Currently my execution time for reading this file (db_item.rdb) is 2-2.2s with most of the time being spent in readStream() (~740ms)
EDIT: Clarify Question:
Is it possible and if so, how would one read through a memorystream until you encounter a blank char ('\0')? Or is a memorystream simply not suited to such a task?
Something like this could work. However depending on the length of stream not sure if this will make much difference in process time.
This uses the MemoryStream.ReadByte Method()
Return Value
Type: System.Int32
The byte cast to a Int32, or -1 if the end of the stream has been reached.
Remarks
If the read operation is successful, the current position within the stream is advanced by one byte. If an exception occurs, the current position within the stream is unchanged.
private static byte[] readStream(MemoryStream ms, int size)
{
byte[] buffer = new byte[size];
if (size != 256)
ms.Read(buffer, 0, buffer.Length);
else
{
for (int i = 0; i < 256; i++)
{
byte b;
var r = ms.ReadByte();
if (r < 0) //read byte returns -1 if at end of stream
break;
b = (byte)r;
if (b == '\0') //equals our empty flag
break;
buffer[i] = b;
}
}
return buffer;
}