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