How do I get from a OutputStream to a InputStream
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, November 6, 2001 01:51 AM

I have the following method that should return a InputStream, but the data I need to return comes from Marshaller.marshal which writes to a PrinterWriter. How do I do this (the following code dosen't work, I have tryed to understand the sun tutorial on this, but failed). I have also included the ReverseStreamThread class. public InputStream getContacts() { PipedOutputStream po = new PipedOutputStream(); PipedInputStream pi = new PipedInputStream(po); BufferedOutputStream out = new BufferedOutputStream(po); Marshaller.marshal(this.contactDatabase,new PrintWriter(out)); new ReverseStreamThread(out,pi).start(); return pi; } Here comes the ReverseStreamThread class:    More>>

I have the following method that should return a InputStream, but the data I need to return comes from Marshaller.marshal which writes to a PrinterWriter. How do I do this (the following code dosen't work, I have tryed to understand the sun tutorial on this, but failed). I have also included the ReverseStreamThread class.



			
public InputStream getContacts() {
PipedOutputStream po = new PipedOutputStream();
PipedInputStream pi = new PipedInputStream(po);
BufferedOutputStream out = new BufferedOutputStream(po);
Marshaller.marshal(this.contactDatabase,new PrintWriter(out));
new ReverseStreamThread(out,pi).start();
return pi;
}


Here comes the ReverseStreamThread class:


			
public class ReverseStreamThread extends Thread {
private OutputStream out = null;
private InputStream in = null;

public ReverseStreamThread(OutputStream out, InputStream in) {
this.out = out;
this.in = in;
}

public void run() {
if (out != null && in != null) {
try {
int input;
while ((input = in.read()) > 0) {
out.write(input);
out.flush();
}
out.close();
} catch (IOException e) {
System.err.println("ReverseThread run: " + e);
}
}
}
}
   <<Less

Re: How do I get from a OutputStream to a InputStream

Posted By:   John_Zukowski  
Posted On:   Thursday, November 29, 2001 08:57 AM

If marshal() needs to send output to a PrintWriter, send one that is backed by a StringWriter. Then, you can just call the toString() method of StringWriter to get its contents. You can then create a StringReader from that String.
About | Sitemap | Contact