ByteArrayInputStream and ByteArrayOutputStream

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

ByteArrayInputStream and ByteArrayOutputStream

 
0
  #1
Jul 31st, 2008
Hi all,

I am getting the content of an object which is returned as ByteArrayInputStream, and then I am assigning to another object, which accepts input as ByteArrayOutputStream. Can someone please suggest as how can I convert ByteArrayInputStream to ByteArrayOutputStream...

Here's my code:
  1. ByteArrayInputStream b_input = source_object.getContent();
  2.  
  3. // how can I set the content of my target_object to the content of my source_object
  4. target_object.setContent(<it accepts a ByteArrayOutputStream>)

Please advise...


Thanks,
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: ByteArrayInputStream and ByteArrayOutputStream

 
0
  #2
Jul 31st, 2008
This may help--

  1. import java.io.*;
  2.  
  3. public class BAM{
  4.  
  5.  
  6. public static void main(String... args){
  7.  
  8. // dummy bytes to manipulate
  9. byte myBytes[] =
  10. {
  11. ( (1 << 2) + (1 << 3) ), // 2^2 + 2^3 = 12 -- 1100
  12. ( (1 << 4) + (1 << 2) ), // 2^4 + 2^2 = 20 -- 10100
  13. ( (1 << 2) + (1 << 0) ) // 2^2 + 2^0 = 5 -- 101
  14. };
  15.  
  16. ByteArrayInputStream bais = new ByteArrayInputStream(myBytes);
  17.  
  18. System.out.println("Bytes left in inputstream = " + bais.available()); // displays the available bytes in this stream
  19.  
  20. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  21.  
  22. System.out.println("writing information to outputstream, read from input stream");
  23. while(bais.available() != 0){
  24. baos.write(bais.read());
  25. try{Thread.sleep(500); System.out.print(".");}catch(Throwable t){}
  26. }
  27.  
  28. System.out.println("Bytes left in inputstream = " + bais.available());
  29. // displays the available bytes after writing the read bytes into the output stream
  30.  
  31. System.out.println(baos); // displaying the information written to the output stream - should be displayed as chars
  32.  
  33. System.out.println(
  34. (char)(byte)(new Byte((byte)12)) + " " +
  35. (char)(byte)(new Byte((byte)20)) + " " +
  36. (char)(byte)(new Byte((byte)5))); // proof that the information was written to the stream as bytes but displayed as chars
  37. }
  38. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: ByteArrayInputStream and ByteArrayOutputStream

 
0
  #3
Aug 1st, 2008
Thanks, will try this.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC