c# - Convert Stream to Byte Array -


I have created a simple extension to convert a stream into a byte array:

  Public stable byte [] ToByte (this stream value) {If (value == empty) return zero; If (value is memorystream) {return (memorystream) value) .ORA (); } Use other {(MemoryStream Stream = new memorystream ()) {value.CopyTo (stream); Return stream. Tore (); }}}   

This is the first time I use it works fine ...

If I use it the second time on the same stream, Byte [] is byte [0]

I debug and I think that this happens because the stream follows the end of the stream after the first conversion.

What is the correct way to correct it?

As you say, you are at the end of the stream after reading first. Thus, you must reset the memory stream with:

  stream.Seek (0, SeekOrigin.Begin);   

or

  stream.Position = 0;   

Before reading it again. Note that the CanSeek on the stream should be correct for any approach to work. MemoriStream OK, some other streams will be set to false.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -