Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Convert outputstream to inputstream

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 ?



Re: Convert outputstream to inputstream

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

Re: Convert outputstream to inputstream

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.

Re: Convert outputstream to inputstream

Tue Dec 30, 2008 6:35 pm

why don't you make client and server , using sockets ?

Re: Convert outputstream to inputstream

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.

Re: Convert outputstream to inputstream

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).

Re: Convert outputstream to inputstream

Sat Apr 04, 2009 9:25 am

thanks alot :)

Re: Convert outputstream to inputstream

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();
} }


Post a reply
  Related Posts  to : Convert outputstream to inputstream
 outputstream to inputstream     -  
 How to Convert WMV to AVI on Mac     -  
 Convert To Upper Case in C++     -  
 convert timestamp using javascript     -  
 Convert String to Date     -  
 convert pascal code to c ?     -  
 convert hexadecimal to decimal     -  
 convert XMLGregorianCalendar to GregorianCalendar     -  
 convert word, Excel to PDF     -  

Topic Tags

Java Files and I/O