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]
# 1

apologies... that was for somebody else... :-P

Message was edited by:

@@CKM@@

@@CKM@@a at 2007-7-29 13:39:52 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

masijade.a at 2007-7-29 13:39:52 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for your reply but could you give me some solution with PipedInputStreams because my file is taking a lot of memeory

rite2rioa at 2007-7-29 13:39:53 > top of Java-index,Java Essentials,Java Programming...
# 4

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.

masijade.a at 2007-7-29 13:39:53 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks for your reply i will surely try this and let you know if i am getting it.

thanks man thanks a lot.

rite2rioa at 2007-7-29 13:39:53 > top of Java-index,Java Essentials,Java Programming...
# 6

Thanks a lot buddy i am really greatfull to you your code worked the one with the ByteArrayInputStream and ByteArrayOuputStream.

Thanks once again

rite2rioa at 2007-7-29 13:39:53 > top of Java-index,Java Essentials,Java Programming...