Java2 codes,problems ,discussions and solutions are here
Sat Dec 27, 2008 8:38 am
I have method that returns outputstream and I want to pass this to another method that takes inputstream as a parameter
is it possible ?
Sat Dec 27, 2008 9:50 pm
Dear Mohamed,
well you cant do that since these are 2 different types of streams, but why dont you post what your code is about and i think it will be more helpful.
Regards
Tue Dec 30, 2008 10:20 am
i have outputstream that write(buffer); and inputstream that read(buffer)
and it is the same buffer means one class write and the other read but they are 2 applications and they uses sockets but i want them one program so i used file to write buffer then i read buffer again from file
but there is a lot of processing is there is any solutions sorry for not write the code it seems to be complicated.
Tue Dec 30, 2008 6:35 pm
why don't you make client and server , using sockets ?
Tue Dec 30, 2008 10:16 pm
well you can create a third class -assuming that write is in a class diffrent than read- and create two objects one from write and one from read, and the shared buffer you want to use, and start using the operations u want
e.g.
MyWriter write= new ...;
MyReader read=new ...;
String buffer;
read.read(buffer);
if(buffer!=null)
write.write(buffer);
i hope i understand your problem right and i hope my answer helps you.
Fri Apr 03, 2009 10:55 pm
If your data can fits into memory the best way is to first write the data to a ByteArrayOutputStream, then get the byte array and make a new ByteArrayInputStream from the previous byte array.
- Code:
ByteArrayOutputStream buffer=new ByteArrayOutputStream();
write(buffer);
InputStream is=new ByteArrayInputStream(buffer.toByteArray());
...
Another possibility is to write your data to a temporary file and read it back.
A third possibility is to use java pipes (PipeInputStream and PipeOutputStream) this last possibility involve to make a new thread and it's quite complicated. I suggest you have a look to
http://io-tools.googlecode.com (OutputStreamToInputStream class).
Sat Apr 04, 2009 9:25 am
thanks alot
Mon May 25, 2009 8:45 am
Code sample below converts inputstream to outputstream.
Code:
- Code:
import java.io.*;
public class InputToOutputStream {
public static void main( String args[] ) throws IOException
{
InputStream in = new FileInputStream ( "InputToOutputStream.java" );
OutputStream out = System.out;
int nextChar;
while ( ( nextChar = in.read() ) != -1 )
out.write( Character.toUpperCase( (char) nextChar ) );
out.write( '\n' );
out.flush();
} }
Topic Tags
Java Files and I/O
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.