• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert from stream to byte array?

 
gavin goh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private byte[] GetStreamAsByteArray (System.IO.Stream stream)
{
int streamLength = Convert.ToInt32(stream.length);
byte[] fileData = new byte[streamLength + 1];

stream.Read(fileData, 0, streamLength);
stream.Close();

return fileData;
}

This is what i found on the internet, but it can't seem to work.
Experts out there please help me.
My project deadline is in less than a week
Thanks !
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it would help to ask this question in a C# forum? ... I am assuming that you do know that this isn't Java code.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java you'd use a ByteArrayOutputStream to write to, then call its toByteArray() method. There is no such equivalent in C# I noticed.

You could write to a StringWriter, then convert the string into a byte[] somehow. Or you could put each byte into a List<byte> (yes people, this is allowed in C# ), then convert that into a byte[].
 
gavin goh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies to all, i'm doing this on C# not on java. My bad !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic