Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

confused about Java I/O, byte[] and streams

Thread view: 
luca - 17 Sep 2004 11:19 GMT
I have an array of bytes which contains binary data.
How do I go about piping this data into a servlet output stream?

  PrintWriter out = response.getWriter();
  out.print(mybytes); //mybytes has type byte[]. This doesn't seem to work

thanks

Luca
William Brogden - 17 Sep 2004 11:51 GMT
> I have an array of bytes which contains binary data.
> How do I go about piping this data into a servlet output stream?
>
>    PrintWriter out = response.getWriter();
>    out.print(mybytes); //mybytes has type byte[]. This doesn't seem to  
> work

Writers do a character conversion - you want the plain OutputStream

OutputStream os = response.getOutputStream();
os.write( mybytes );

Bill
luca - 17 Sep 2004 15:30 GMT
> Writers do a character conversion - you want the plain OutputStream
>
> OutputStream os = response.getOutputStream();
> os.write( mybytes );

this did the job, thank you, william and stefan.
For the archive, since I have a mix of strings and
binary data, I used this trick to turn my strings into bytes:

       ServletOutputStream  out = response.getOutputStream();
       out.write("Content-Type: multipart/mixed;boundary=foo\n".getBytes());
       out.write("\n".getBytes());
       out.write("--foo\n".getBytes());
       out.write("Content-Type: text/html\n".getBytes());
       out.write(somestring.getBytes());
       out.write("\n".getBytes());

Luca
Stefan Schulz - 17 Sep 2004 12:05 GMT
> I have an array of bytes which contains binary data.
> How do I go about piping this data into a servlet output stream?
>
>   PrintWriter out = response.getWriter();
>   out.print(mybytes); //mybytes has type byte[]. This doesn't seem to work

What exactly goes wrong? Sorry, but these two lines of Code don't give
many hints as to what the actual problem is.
luca - 17 Sep 2004 12:20 GMT
I don't see any output when I do a view source....

I know it's binary, but still....

thanks

Luca

>> I have an array of bytes which contains binary data.
>> How do I go about piping this data into a servlet output stream?
[quoted text clipped - 5 lines]
> What exactly goes wrong? Sorry, but these two lines of Code don't give
> many hints as to what the actual problem is.
Stefan Schulz - 17 Sep 2004 13:04 GMT
> I don't see any output when I do a view source....
>
[quoted text clipped - 13 lines]
>> What exactly goes wrong? Sorry, but these two lines of Code don't give
>> many hints as to what the actual problem is.

As William said, you should use an OutputStream for binary data, since
this is what writes raw bytes.

Other then that, have you tried breaking down the problem into smaller
units? Can you write that binary data into some file? If that works,
look at what is different for your Problem then that, and so on.

See you
Stefan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.