not able to convert outputstream to inputstream
Hi freinds plz help me on this i am not getting a solution for this
converting inputstream to outputstream
public ReportGenerator(Connection con,String input,OutputStream out){
try{
System.out.println("inside ReportGenerator()");
InputStream bais = new ByteArrayInputStream(input.getBytes());
JasperCompileManager.compileReportToStream (bais,out);
System.out.println("Afeter the compile");
bais = new ByteArrayInputStream(out.toString().getBytes());
//here its giving an error saying cant load Inputstream
JasperFillManager.fillReportToStream (bais,out,new HashMap(),con);
bais = new ByteArrayInputStream(out.toString().getBytes());
JasperExportManager.exportReportToPdfStream(bais,out);
}catch(Exception e){
e.printStackTrace();
}
so i tried using PipedStream for the same thing this is the code using piped streams
public ReportGenerator(Connection con,String input,OutputStream out){
try{
byte arrayOfByte[] = input.getBytes();
System.out.println("Storing the input into a byte array");
PipedInputStream pins = new PipedInputStream();
PipedOutputStream pouts = new PipedOutputStream(pins);
System.out.println("Storing the PipedInputStream into PipedOutputStream");
System.out.println("length === "+arrayOfByte.length);
//its giveing me the output till here and after this it is getting hanged so pls someone suggest a solution for this problem or send me the code.
pouts.write(arrayOfByte,0, arrayOfByte.length);
pins.connect(pouts);
JasperCompileManager.compileReportToStream(bias,out);
JasperFillManager.fillReportToStream(pins,out,new HashMap(),con);
JasperExportManager.exportReportToPdfStream (pins,out);
}catch(Exception e){
e.printStackTrace();
}
Thanks for your help
[1899 byte] By [
rite2rioa] at [2007-11-27 11:10:09]

If the entire thing fits in memory, does it fit into memory twice?
If so, then try like this
public ReportGenerator(Connection con,String input,OutputStream out) {
try{
System.out.println("inside ReportGenerator()");
InputStream bais = new ByteArrayInputStream(input.getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JasperCompileManager.compileReportToStream (bais, baos);
System.out.println("After the compile");
bais = new ByteArrayInputStream(baos.toByteArray());
// Just to make sure there is no conflict with the byte[]'s
baos = new ByteArrayOutputStream();
JasperFillManager.fillReportToStream (bais, baos,new HashMap(),con);
bais = new ByteArrayInputStream(baos.toByteArray());
JasperExportManager.exportReportToPdfStream(bais,out);
}catch(Exception e){
e.printStackTrace();
}
}
I wouldn't try this on files that are very large though.
Of course, you could always create a temporary file with File.createTempFile(String, String) and use that as an itermediate step.
So use a tempfile. That is the proper solution. And don't give the input as a string, but rather as an InputStream (as I assume it is being read from a file anyway).
public ReportGenerator(Connection con,InputStream input,OutputStream out) {
try{
System.out.println("inside ReportGenerator()");
File temp1 = File.createTempFile("tempPDF_", ".out");
File temp2 = File.createTempFile("tempPDF_", ".out");
FileOutputStream fos = new FileOutputStream(temp1);
JasperCompileManager.compileReportToStream (input, fos);
fos.close(); // Assuming compileReportToStream flushes.
System.out.println("After the compile");
FileInputStream fis = new FileInputStream(temp1);
fos = new FileOutputStream(temp2);
JasperFillManager.fillReportToStream (fis, fos,new HashMap(),con);
fos.close(); // Assuming fillReportToStream flushes.
fis.close();
temp1.delete();
fis = new FileInputStream(temp2);
JasperExportManager.exportReportToPdfStream(fis,out);
fis.close();
temp2.delete();
}catch(Exception e){
e.printStackTrace();
}
}
Message was edited by:
masijade.
**** cut and paste errors.
Edit again:
I mean come on, this is not that hard. Put a little effort into it. Although, I know the next argument to come will be that you are not allowed to create temp files.