File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ByteArrayOutputStream" Watch "ByteArrayOutputStream" New topic
Author

ByteArrayOutputStream

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();
4.f.write(buf);
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
Greenhorn

Joined: Nov 07, 2000
Posts: 8
Hi Nasir
Here is the code you are looking for.
import java.io.*;
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();
}
f.write(bt,0,s.length());
System.out.println(f.toString());
}
}
good luck.
Sunil Mehta
Greenhorn

Joined: Nov 07, 2000
Posts: 8
correction:
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.
------------------
Jane
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: http://aspose.com/file-tools
 
subject: ByteArrayOutputStream