Hi Everyone,

I am trying to integrate excel password protection of XSSF documents into an 
existing process that currently uses POI to write the documents. I am looking 
to avoid the need to write the file to disk before encrypting it and would like 
it to be streamed through the encryption as it is being written out.

I have been able to accomplish this by using Piped streams and a separate 
reader thread:

PipedInputStream pis = new PipedInputStream(16 * 1024);
       PipedOutputStream pos = new PipedOutputStream(pis);

       WorkbookWriterThread thread = new WorkbookWriterThread(pis,
              outputFile, cipherAlg,
              hashAlg, password);
       thread.start();
       workbook.write(pos);

...but this is not ideal and would like to avoid using a separate thread if 
possible.

Looking through the code, it appears the OPCPackage IS available on the 
XSSFWorkbook object, but does not appear to be loaded/committed completely by 
the time I try to write it to the encryption stream.

outputEncryptionStream = enc.getDataStream(fs);
       destOutputStream = new FileOutputStream(outputFile);
       pkg = ((XSSFWorkbook) workbook).getPackage();

       pkg.save(outputEncryptionStream);
       fs.writeFilesystem(destOutputStream);

However if I open() the OPCPackage with an input stream or file, it works as 
expected.

       opcPackage = OPCPackage.open(in);
       outputEncryptionStream = enc.getDataStream(fs);
       opcPackage.save(outputEncryptionStream);

       fs.writeFilesystem(out);

My ultimate goal is to avoid having duplicate workbook data in memory, 
considering the existing memory footprint of an XSSFWorkbook.  What I am seeing 
right now is that the OPCPackage.open(InputStream) gives a brief spike in 
memory which seems to be GC'd almost immediately before the data starts being 
written to disk, but I'm worried this spike could be the tipping point in an 
OOM.

It seems like an achievable goal to be able to stream the existing workbook 
through an encryptor when writing it out to disk, and I'm wondering what I 
might be missing. Does anyone have any suggestions on how to accomplish this?

ERICK LICHTAS
Linoma Software
Senior Software Engineer
p. 402.944.4242 x714
f. 402.944.4243
www.LinomaSoftware.com<www.linomasoftware.com>
www.GoAnywheremft.com<www.goanywheremft.com>

Reply via email to