I am embedding my size in a Stream in the first 4 byte. When I think about it, it should be possible to do it better, but that depends.

Here is how I currently do it:

bsize = ms.Length;
tcp.GetStream().Write(BitConverter.GetBytes(bsize), 0, intsize);
tcp.GetStream().Write(ms.GetBuffer(), 0, (int)bsize);

As you can see, it works and it's nothing weird. I just send 2 streams, the first being the length and the other being the data.

So, there aren't many things that can be done to improve or even change this.

But my idea is: why use 2 streams when you can probably embed the size immediately on the second, so you only need to write it once?

Sadly, I am not sure how to do that, or if it's even an improvement. Here is my testing code to achieve it:

bsize = ms.Length;
ms.Write(BitConverter.GetBytes(bsize), 0, intsize);
ms.Position = 0;
tcp.GetStream().Write(ms.GetBuffer(), 0, (int)bsize+4);

So, I thought: why can't you just write the length to the MemoryStream and add it before the actual data?

But, it doesn't work, and I am not sure if it's adding or overwriting data. I suppose it's overwriting, which means it won't work.

From my testing, I think Write adds data to the end, which complicates things.

  • \$\begingroup\$ are you getting an error when you try to write to the tcp.GetStream()? \$\endgroup\$
    – Malachi
    Aug 22, 2013 at 17:09
  • \$\begingroup\$ No, why should i? \$\endgroup\$
    – Zerowalker
    Aug 22, 2013 at 17:18
  • \$\begingroup\$ you said that it wasn't working the way you originally tried, I was wondering if it gave you an error or if you just weren't getting the right output. \$\endgroup\$
    – Malachi
    Aug 22, 2013 at 17:31
  • \$\begingroup\$ Oh, you mean "Not sure why me previous testing failed, but here are the results:" ?, what i meant was, i got worse benchmark results, but i retried and did everything more through. \$\endgroup\$
    – Zerowalker
    Aug 22, 2013 at 18:18

2 Answers 2


I was looking into the MemoryStream and I found this

Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.

on MemoryStream.Write Method

I would look around a little bit on the MSDN sites that I am going to list and see if you want to change around the code that you have so that you can maybe use the Byte[] Parameter.

MemoryStream Class

although it looks like most of those Constructors create non-resizable instances, whereas the a Memory Stream Created with out any Parameters ...

Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

so to me it looks like the BlockCopy or 2 streams methods are the only way to go.

  • \$\begingroup\$ Hmm, that´s to bad, as there is no way to do the other way around (tell the stream to read the last part, as they don´t know the end). Well, an interesting thing is that byte[] alter this for MemoyStreams. Will have to look into that. Thanks \$\endgroup\$
    – Zerowalker
    Aug 22, 2013 at 18:20

if you take and create two Byte[] Variables and fill the first with the information that you want to send then you can figure the size add it to the second Byte array and then append the information to the second array as well then just send the second Byte Array in your stream.

Check out the First Answer to this Question on StackOverflow

Best way to combine two or more byte arrays in C#

that first answer is very detailed so you should be able to figure it into your code, he even breaks down the performance of two methods of doing it.

  • \$\begingroup\$ Tried it and updated my post, interesting approach, sadly it´s slower. \$\endgroup\$
    – Zerowalker
    Aug 22, 2013 at 14:58
  • \$\begingroup\$ did you try the System.Array.Copy? sometimes they have two options on those kinds of things for different purposes, maybe in this circumstance it might work better. another thing is that you are doing another operation in the code so it will have to take the time to do that stuff and that would be the decrease in speed. \$\endgroup\$
    – Malachi
    Aug 22, 2013 at 15:01
  • \$\begingroup\$ Tried it now. And did a more detailed testing, and actually i think that all performance the same. Will post the results in my post. But, isn´t it possible to write the length to the MemoryStream, i mean, that is already created, and should spare time to just write to it. And i don´t get what you mean with "another operation in the code". Thanks \$\endgroup\$
    – Zerowalker
    Aug 22, 2013 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.