• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to convert from stream to byte array?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies to all, i'm doing this on C# not on java. My bad !
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic