* by this class is consistent with that of the inner stream.
*/
public class LittleEndianInputStream extends FilterInputStream implements LittleEndianInput {
+
+ private static final int EOF = -1;
+
public LittleEndianInputStream(InputStream is) {
super(is);
}
@Override
public void readFully(byte[] buf, int off, int len) {
try {
- checkEOF(read(buf, off, len), len);
+ checkEOF(_read(buf, off, len), len);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
+ //Makes repeated calls to super.read() until length is read or EOF is reached
+ private int _read(byte[] buffer, int offset, int length) throws IOException {
+ //lifted directly from org.apache.commons.io.IOUtils 2.4
+ int remaining = length;
+ while (remaining > 0) {
+ int location = length - remaining;
+ int count = read(buffer, offset + location, remaining);
+ if (EOF == count) { // EOF
+ break;
+ }
+ remaining -= count;
+ }
+
+ return length - remaining;
+ }
+
@Override
public void readPlain(byte[] buf, int off, int len) {
readFully(buf, off, len);
import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor;
import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.XmlException;
poifs.close();
fis.close();
}
+
+ /**
+ * Test case for #59841 - this is an example on how to use encrypted temp files,
+ * which are streamed into POI opposed to having everything in memory
+ */
+ @Test
+ public void protectedXLSBZip() throws IOException, GeneralSecurityException, XmlException, OpenXML4JException {
+ File tikaProt = XSSFTestDataSamples.getSampleFile("protected_passtika.xlsb");
+ FileInputStream fis = new FileInputStream(tikaProt);
+ POIFSFileSystem poifs = new POIFSFileSystem(fis);
+ EncryptionInfo ei = new EncryptionInfo(poifs);
+ Decryptor dec = ei.getDecryptor();
+ boolean passOk = dec.verifyPassword("tika");
+ assertTrue(passOk);
+
+ // extract encrypted ooxml file and write to custom encrypted zip file
+ InputStream is = dec.getDataStream(poifs);
+
+ // provide ZipEntrySource to poi which decrypts on the fly
+ ZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(is);
+
+ // test the source
+ OPCPackage opc = OPCPackage.open(source);
+ String expected = "You can't see me";
+
+ XSSFBEventBasedExcelExtractor extractor = new XSSFBEventBasedExcelExtractor(opc);
+ extractor.setIncludeSheetNames(false);
+ String txt = extractor.getText();
+ assertEquals(expected, txt.trim());
+
+ extractor.close();
+ opc.close();
+ poifs.close();
+ fis.close();
+ }
+
}