Why does Request.InputStream contain an extra byte?

I am loading a byte array into an ASHX handler

byte[] serializedRequest = SerializeRequest();

var uri = new Uri(_server.ActionUrl);
using (WebClient client = new WebClient())
{
    client.UploadData(uri, serializedRequest);
}

      

What is received by the handler

Dim str As Stream = context.Request.InputStream
Dim transformation(str.Length - 1) As Byte ' here I have extra "0"-byte
str.Position = 0
str.Read(transformation, 0, transformation.Length)

      

As you can see what I need to do str.Length - 1

is to declare a byte array. And this is in development. I don't even know how it will behave when deployed. Where does this byte come from? Is this a reliable way or should I add some bytes at the beginning of the stream to indicate how many bytes are read from the Request.InputStream?

+3


source to share


1 answer


Dim x(y) as Byte

actually means "array with upper bound y (length = y + 1)" ( Arrays in VB.NET ).

Dim transformation(str.Length) As Byte

actually declares a larger array than you need, so the operator is str.Length - 1

correct.



In reality, there is no byte with 0 values, because there is Stream.Read()

no need to read the stream to the end (check the return value of the method) and leave an extra byte with the default value (0).

+2


source







All Articles