[ajug-members] Chaining OutputStreams
Ben McEwen
bmcewen at fleetcor.com
Mon Apr 11 14:34:22 EDT 2005
I haven't used iText in a while, but I think the PdfEncryptor writes the
encrypted PDF to the output stream; you shouldn't need to do anything with
the ServletOutputStream otherthan passing it to the PdfEncryptor.encrypt
function. I have not tested this, but I think this is all you need to do:
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
PdfReader reader = new PdfReader("WEB-INF/" + filename);
PdfEncryptor.encrypt(reader, baos, true, userPwd, null,
PdfWriter.AllowPrinting | PdfWriter.AllowDegradedPrinting);
/* Set response header info and content type */
//Set the response's content length
response.setContentLength(baos.size());
// Flush byte array to servlet output stream.
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
Hope that helps,
Ben
-----Original Message-----
From: ajug-members-bounces at ajug.org [mailto:ajug-members-bounces at ajug.org]
On Behalf Of Lee Grey
Sent: Monday, April 11, 2005 6:03 AM
To: ajug-members at ajug.org
Subject: [ajug-members] Chaining OutputStreams
I'm trying to use iText (http://www.lowagie.com/iText) to encrypt a PDF
before sending it through a servlet. I'm confused by the use of
BufferedInputStream and BufferedOutputStream, when you need something in the
middle. I need to take output from the PDFEncryptor and then stream that
out to the servlet response, but I don't know how to do that. This was all
working fine when I had a BufferedInputStream reading a file from disk, but
now I've got a second OutputStream, and I don't know how to feed one from
the other. Here's what I've got, which is probably a mess.
// transmit encrypted file
ServletOutputStream out = response.getOutputStream();
BufferedOutputStream buf = new BufferedOutputStream(out);
BufferedOutputStream bos = new BufferedOutputStream(buf);
String filename = JFig.getInstance().getValue("file1",
"filename");
try {
// encrypt the file
PdfReader reader = new PdfReader("WEB-INF/" + filename);
String userPwd = StringUtil.createPDFPassword();
PdfEncryptor.encrypt(reader, bos, true, userPwd, null,
PdfWriter.AllowPrinting | PdfWriter.AllowDegradedPrinting);
byte[] buffer = new byte[4*1024];
int totalBytes = 0;
int bytesRead;
while(( bytesRead = bos.read(buffer, 0, buffer.length))
!= -1) {
out.write(buffer, 0, bytesRead);
totalBytes += bytesRead;
}
log.debug("wrote " + totalBytes + " bytes");
out.flush();
} catch(Exception e) {
if( log.isErrorEnabled() ) {
log.error("Failure during streaming of " + filename, e);
}
} finally {
if( buf != null )
buf.close();
if( bos != null )
bos.close();
if( out != null ) {
out.flush();
out.close();
}
}
The big problem comes in the while loop, where you have to have an
InputStream to be able to call read(). Maybe that's not a problem, if you
know what you're doing. Can someone please show me how to send the output
of the PdfEncryptor out to the servlet response stream?
Thanks,
Lee Grey
Grey Matter
http://www.leegrey.com/hmm
_______________________________________________
ajug-members mailing list
ajug-members at ajug.org
http://www.ajug.org/mailman/listinfo/ajug-members
More information about the ajug-members
mailing list