Is there a good way to find the first byte of data in a MemoryStream?

  • Thread starter Thread starter Brad Wood
  • Start date Start date
B

Brad Wood

After I've written a string to a MemoryStream object, I want to set the
position to the first byte of real data, past the variable length of
bytes that indicate the buffer length.
Trying to figure out exactly at what data lengths the "length indicator"
length grows is tricky and boundary error prone.

Is there a good way?
 
If I remember correctly, the header info and the file are separated by null
chars. If so, find the first null char and then start on the first byte that
is not 00 Hex.

The last app I used this on read the header, so I know it ends with a null
char. I am just not 100% sure of how many chars constitute end of header.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
When I examine the byte array in the stream, there is no null separator.
There are nulls between each ASCII character but that is only due to
Unicode encoding and only because I wrote basic ASCII characters to the
stream.
 
Brad Wood said:
After I've written a string to a MemoryStream object, I want to set the
position to the first byte of real data, past the variable length of
bytes that indicate the buffer length.
Trying to figure out exactly at what data lengths the "length indicator"
length grows is tricky and boundary error prone.

Is there a good way?

That all depends on *how* you've written the string to the
MemoryStream. Have you used BinaryWriter? If so, using
BinaryReader.Read7BitEncodedInt is probably the easiest way, then just
call BinaryReader.Read to find the first byte of real data.

(Note that that may not be the whole of the first *character* of real
data, depending on the encoding you're using.)
 
Use the Seek Method:

ms.Seek(0, SeekOrigin.Begin);

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Kevin Spencer said:
Use the Seek Method:

ms.Seek(0, SeekOrigin.Begin);

I think the OP's point was that he didn't want the start of the stream
- he wanted to skip over the first bit of data.

However, as you've brought up Seek - I personally find the Position
property a lot clearer:

ms.Position = 0;
 
That just sets your position to 0 (the beginning of data in my stream is
never at position 0). I really don't understand what that's for since I
could simply set the position to 0...
 
Using the implementation for Read7BitEncodedInt that I found on
dotnet.framework.performance (it is a protected method), that works.
I Would never have been able to implement it on my own (not knowing how
to decode 7 bit encoded integers)...
 
Brad Wood said:
Using the implementation for Read7BitEncodedInt that I found on
dotnet.framework.performance (it is a protected method), that works.
I Would never have been able to implement it on my own (not knowing how
to decode 7 bit encoded integers)...

It's not that hard - it's specified in
BinaryWriter.Write7BitEncodedInt. In particular, if all you need to do
is skip the bytes, all you need to know is that the first byte of data
is the one *following* the first byte that doesn't have its highest bit
set.
 
I want to set the position to the first byte of real data, past the
variable length of bytes that indicate the buffer length.

I've used the MemeoryStream quite a bit. It seemed to me that you weren't
talking about the actual beginning of the stream, but "the first byte of
real data, past the variable length of bytes that indicate the buffer
length." That has been my experience with using the Seek method.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
When you use a BinaryWriter to write a string to a MemoryStream it
writes out the string length (taking 1 or 2 bytes, potentially more; I'm
not sure) before writing the string.
 
Thanks; that *is* all I needed to do.
What's even dumb and dumber is that I didn't need to actually find the
implementation of Read7BitEncodedInt - all I needed to do was to
subclass it and call it since it is protected, not private. Sheesh!
 
Ah, now I'm following you. I can see that that would happen if you were
binarily writing a string to the stream. Perhaps I misunderstood you. I
thought you were talking about the stream itself.

In any case, I'm glad you solved your problem.

--

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Back
Top