This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ByteArrayOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ByteArrayOutputStream" Watch "ByteArrayOutputStream" New topic


Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Hi freinds,
ByteArrayOutputStream uses a byte array as the destination(as given by Api doc)Here is an example from a book
1.ByteArrayOutputStream f=new ByteArrayOutputStream();
2.String s="This should end up in in the array";
3.byte [] buf=s.getBytes();
Line4 writes the content of 'buf ' into ByteArrayOutputStream am I right here ??
If right then it seems ByteArrayOutputStream is using a byte array as source rather than a destination.
I'm very confused with this please help me.

Sunil Mehta

Joined: Nov 07, 2000
Posts: 8
Hi Nasir
Here is the code you are looking for.
public class BytArr{
public static void main(String args[]){
ByteArrayOutputStream f=new ByteArrayOutputStream();
String s="This should end up in the array";
int n = s.length();
byte bt[] = new byte[n];
for (int i=0;i<s.length();i++)>
bt[i] = (new Integer(s.charAt(i))).byteValue();
good luck.
Sunil Mehta

Joined: Nov 07, 2000
Posts: 8
line 9:
for (int i=0;i<s.length();i++)>
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Thanks Sunil
but i'm not looking for the code.
could anyone please help me on whatever i mentioned above.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Nasir,
It does both . The Class libraries states:

"You can use a byte array output stream to treat a <code>byte</code> array as an output stream on which you can perform write operations."

The <code>write()</code> method takes a byte array as an argument.
So, it uses a byte array as an 'input' and also 'outputs' a byte array. Because it is output as a byte array, it can be read back into a byte array using ByteArrayInputStream.
Hope that helps.
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited December 10, 2000).]

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link:
subject: ByteArrayOutputStream
It's not a secret anymore!