Memorystream copy to two new memorystreams

cpopham

Well-known member
Joined
Feb 18, 2004
Messages
273
Okay, here is my code:

Code:
        Dim msNewStream As New MemoryStream
        Dim msstreamhass As New MemoryStream


        msNewStream.Write(msStream.ToArray, 0, msStream.Length - (hashsize * 8))
        Dim intP As Integer = msStream.Length - (hashsize * 8)
        intP = msStream.Length
        msStream.Position = 0

            msstreamhass.Write(msStream.ToArray, (msStream.Length - 159), msStream.Length)

The first part works fine "msNewStream.Write(msStream.ToArray, 0, msStream.Length - (hashsize * 8))"

The second one always throws an exception of:
Offset and Length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

Anyone have any ideas?

Thanks, Chester
 
Not sure if there is a bug in the lines
Code:
Dim intP As Integer = msStream.Length - (hashsize * 8)
intP = msStream.Length
as you are assigning to intP in both cases but never using it in between.
 
As you say PlausiblyDamp they are not using intP between the assigments. But intP isnt actually being used at all in the code provided so Im assuming that isnt whats causing your application to crash.

When you say the second part causes in error, are you refering to the line
Code:
msstreamhass.Write(msStream.ToArray, (msStream.Length - 159), msStream.Length)

For now Im going to go ahead and assume this is the line throwing the error. There are 3 parameters being passed in a buffer, a start point and a length. For now lets ignore the buffer and look at the other 2 values. You are specifying the start point as 159 places before the end of the stream and then specifying the length as the full length of the stream. Im assuming this is going to cause an error but Ive never used a memorystream so I cant say for certain.

If the aim is to get only the last section of the stream I would suggest what you try this instead...
Code:
msstreamhass.Write(msStream.ToArray, (msStream.Length - 159), 159)
 
I have changed my method of working the problem and it is working fine now. I was using intP as merely a check when stepping through the code. I just wanted to see what values were there and was not actually using the variable for anything. I now have a new problem though that is dealing with this same application, but this one is for hashing so I will start a new thread on it. But thanks for the help :)

Chester
 
Back
Top