aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2016-11-11 23:22:43 +0000
committerAndreas Beeker <kiwiwings@apache.org>2016-11-11 23:22:43 +0000
commit446ed93c4a0a6d0fc7127670f7fe1f38b00db4be (patch)
treeccbd2d592928c1de4287c9653180f029e07c16bf /src/examples
parentcf010d88b2a87291a77fcad66d2df510cd02a054 (diff)
downloadpoi-446ed93c4a0a6d0fc7127670f7fe1f38b00db4be.tar.gz
poi-446ed93c4a0a6d0fc7127670f7fe1f38b00db4be.zip
- SonarCube fixes
- moved SecureTempFile classes to OOXML, because of duplicated code in test and examples packages git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1769363 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/src/org/apache/poi/crypt/examples/AesZipFileZipEntrySource.java147
-rw-r--r--src/examples/src/org/apache/poi/crypt/examples/EncryptedTempData.java72
-rw-r--r--src/examples/src/org/apache/poi/crypt/examples/EncryptionUtils.java3
-rw-r--r--src/examples/src/org/apache/poi/examples/util/TempFileUtils.java7
-rw-r--r--src/examples/src/org/apache/poi/xssf/eventusermodel/examples/LoadPasswordProtectedXlsxStreaming.java35
-rw-r--r--src/examples/src/org/apache/poi/xssf/streaming/examples/SXSSFWorkbookWithCustomZipEntrySource.java113
-rw-r--r--src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java59
-rw-r--r--src/examples/src/org/apache/poi/xssf/usermodel/examples/LoadPasswordProtectedXlsx.java34
8 files changed, 67 insertions, 403 deletions
diff --git a/src/examples/src/org/apache/poi/crypt/examples/AesZipFileZipEntrySource.java b/src/examples/src/org/apache/poi/crypt/examples/AesZipFileZipEntrySource.java
deleted file mode 100644
index af281e2590..0000000000
--- a/src/examples/src/org/apache/poi/crypt/examples/AesZipFileZipEntrySource.java
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ====================================================================
- */
-
-package org.apache.poi.crypt.examples;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FilterOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.security.GeneralSecurityException;
-import java.security.SecureRandom;
-import java.util.Enumeration;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipException;
-import java.util.zip.ZipFile;
-import java.util.zip.ZipInputStream;
-import java.util.zip.ZipOutputStream;
-
-import javax.crypto.Cipher;
-import javax.crypto.CipherInputStream;
-import javax.crypto.CipherOutputStream;
-import javax.crypto.spec.SecretKeySpec;
-
-import org.apache.poi.openxml4j.util.ZipEntrySource;
-import org.apache.poi.poifs.crypt.ChainingMode;
-import org.apache.poi.poifs.crypt.CipherAlgorithm;
-import org.apache.poi.poifs.crypt.CryptoFunctions;
-import org.apache.poi.util.IOUtils;
-import org.apache.poi.util.TempFile;
-
-/**
- * An example <code>ZipEntrySource</code> that has encrypted temp files to ensure that
- * sensitive data is not stored in raw format on disk.
- */
-public class AesZipFileZipEntrySource implements ZipEntrySource {
- final File tmpFile;
- final ZipFile zipFile;
- final Cipher ci;
- boolean closed;
-
- public AesZipFileZipEntrySource(File tmpFile, Cipher ci) throws IOException {
- this.tmpFile = tmpFile;
- this.zipFile = new ZipFile(tmpFile);
- this.ci = ci;
- this.closed = false;
- }
-
- /**
- * Note: the file sizes are rounded up to the next cipher block size,
- * so don't rely on file sizes of these custom encrypted zip file entries!
- */
- @Override
- public Enumeration<? extends ZipEntry> getEntries() {
- return zipFile.entries();
- }
-
- @Override
- public InputStream getInputStream(ZipEntry entry) throws IOException {
- InputStream is = zipFile.getInputStream(entry);
- return new CipherInputStream(is, ci);
- }
-
- @Override
- public void close() throws IOException {
- if(!closed) {
- zipFile.close();
- tmpFile.delete();
- }
- closed = true;
- }
-
- @Override
- public boolean isClosed() {
- return closed;
- }
-
- public static AesZipFileZipEntrySource createZipEntrySource(InputStream is) throws IOException, GeneralSecurityException {
- // generate session key
- SecureRandom sr = new SecureRandom();
- byte[] ivBytes = new byte[16], keyBytes = new byte[16];
- sr.nextBytes(ivBytes);
- sr.nextBytes(keyBytes);
- final File tmpFile = TempFile.createTempFile("protectedXlsx", ".zip");
- copyToFile(is, tmpFile, CipherAlgorithm.aes128, keyBytes, ivBytes);
- IOUtils.closeQuietly(is);
- return fileToSource(tmpFile, CipherAlgorithm.aes128, keyBytes, ivBytes);
- }
-
- private static void copyToFile(InputStream is, File tmpFile, CipherAlgorithm cipherAlgorithm, byte keyBytes[], byte ivBytes[]) throws IOException, GeneralSecurityException {
- SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
- Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding");
-
- ZipInputStream zis = new ZipInputStream(is);
- FileOutputStream fos = new FileOutputStream(tmpFile);
- ZipOutputStream zos = new ZipOutputStream(fos);
-
- ZipEntry ze;
- while ((ze = zis.getNextEntry()) != null) {
- // the cipher output stream pads the data, therefore we can't reuse the ZipEntry with set sizes
- // as those will be validated upon close()
- ZipEntry zeNew = new ZipEntry(ze.getName());
- zeNew.setComment(ze.getComment());
- zeNew.setExtra(ze.getExtra());
- zeNew.setTime(ze.getTime());
- // zeNew.setMethod(ze.getMethod());
- zos.putNextEntry(zeNew);
- FilterOutputStream fos2 = new FilterOutputStream(zos){
- // don't close underlying ZipOutputStream
- @Override
- public void close() {}
- };
- CipherOutputStream cos = new CipherOutputStream(fos2, ciEnc);
- IOUtils.copy(zis, cos);
- cos.close();
- fos2.close();
- zos.closeEntry();
- zis.closeEntry();
- }
- zos.close();
- fos.close();
- zis.close();
- }
-
- private static AesZipFileZipEntrySource fileToSource(File tmpFile, CipherAlgorithm cipherAlgorithm, byte keyBytes[], byte ivBytes[]) throws ZipException, IOException {
- SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
- Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, "PKCS5Padding");
- return new AesZipFileZipEntrySource(tmpFile, ciDec);
- }
-
-} \ No newline at end of file
diff --git a/src/examples/src/org/apache/poi/crypt/examples/EncryptedTempData.java b/src/examples/src/org/apache/poi/crypt/examples/EncryptedTempData.java
deleted file mode 100644
index e4c5796d6f..0000000000
--- a/src/examples/src/org/apache/poi/crypt/examples/EncryptedTempData.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ====================================================================
- */
-
-package org.apache.poi.crypt.examples;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.security.SecureRandom;
-
-import javax.crypto.Cipher;
-import javax.crypto.CipherInputStream;
-import javax.crypto.CipherOutputStream;
-import javax.crypto.spec.SecretKeySpec;
-
-import org.apache.poi.poifs.crypt.ChainingMode;
-import org.apache.poi.poifs.crypt.CipherAlgorithm;
-import org.apache.poi.poifs.crypt.CryptoFunctions;
-import org.apache.poi.util.TempFile;
-
-/**
- * EncryptedTempData can be used to buffer binary data in a secure way, by using encrypted temp files.
- */
-public class EncryptedTempData {
- final static CipherAlgorithm cipherAlgorithm = CipherAlgorithm.aes128;
- final SecretKeySpec skeySpec;
- final byte[] ivBytes;
- final File tempFile;
-
- public EncryptedTempData() throws IOException {
- SecureRandom sr = new SecureRandom();
- ivBytes = new byte[16];
- byte[] keyBytes = new byte[16];
- sr.nextBytes(ivBytes);
- sr.nextBytes(keyBytes);
- skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
- tempFile = TempFile.createTempFile("poi-temp-data", ".tmp");
- }
-
- public OutputStream getOutputStream() throws IOException {
- Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, null);
- return new CipherOutputStream(new FileOutputStream(tempFile), ciEnc);
- }
-
- public InputStream getInputStream() throws IOException {
- Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, null);
- return new CipherInputStream(new FileInputStream(tempFile), ciDec);
- }
-
- public void dispose() {
- tempFile.delete();
- }
-}
diff --git a/src/examples/src/org/apache/poi/crypt/examples/EncryptionUtils.java b/src/examples/src/org/apache/poi/crypt/examples/EncryptionUtils.java
index acbfdd1890..c2b795cb5e 100644
--- a/src/examples/src/org/apache/poi/crypt/examples/EncryptionUtils.java
+++ b/src/examples/src/org/apache/poi/crypt/examples/EncryptionUtils.java
@@ -27,6 +27,9 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
public class EncryptionUtils {
+ private EncryptionUtils() {
+ }
+
public static InputStream decrypt(final InputStream inputStream, final String pwd) throws Exception {
try {
POIFSFileSystem fs = new POIFSFileSystem(inputStream);
diff --git a/src/examples/src/org/apache/poi/examples/util/TempFileUtils.java b/src/examples/src/org/apache/poi/examples/util/TempFileUtils.java
index f34f35d3cb..5d4f7ab531 100644
--- a/src/examples/src/org/apache/poi/examples/util/TempFileUtils.java
+++ b/src/examples/src/org/apache/poi/examples/util/TempFileUtils.java
@@ -25,14 +25,17 @@ import java.io.IOException;
import org.apache.poi.util.TempFile;
public class TempFileUtils {
+ private TempFileUtils() {
+ }
+
public static void checkTempFiles() throws IOException {
String tmpDir = System.getProperty(TempFile.JAVA_IO_TMPDIR) + "/poifiles";
File tempDir = new File(tmpDir);
if(tempDir.exists()) {
String[] tempFiles = tempDir.list();
- if(tempFiles.length > 0) {
+ if(tempFiles != null && tempFiles.length > 0) {
System.out.println("found files in poi temp dir " + tempDir.getAbsolutePath());
- for(String filename : tempDir.list()) {
+ for(String filename : tempFiles) {
System.out.println("file: " + filename);
}
}
diff --git a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/LoadPasswordProtectedXlsxStreaming.java b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/LoadPasswordProtectedXlsxStreaming.java
index aa7d0ceee7..80e4c7320a 100644
--- a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/LoadPasswordProtectedXlsxStreaming.java
+++ b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/LoadPasswordProtectedXlsxStreaming.java
@@ -20,12 +20,13 @@
package org.apache.poi.xssf.eventusermodel.examples;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStream;
-import org.apache.poi.crypt.examples.AesZipFileZipEntrySource;
import org.apache.poi.crypt.examples.EncryptionUtils;
import org.apache.poi.examples.util.TempFileUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
@@ -40,29 +41,25 @@ import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
*/
public class LoadPasswordProtectedXlsxStreaming {
- public static void main(String[] args) {
+ public static void main(String[] args) throws Exception {
+ if(args.length != 2) {
+ throw new IllegalArgumentException("Expected 2 params: filename and password");
+ }
+ TempFileUtils.checkTempFiles();
+ String filename = args[0];
+ String password = args[1];
+ FileInputStream fis = new FileInputStream(filename);
try {
- if(args.length != 2) {
- throw new Exception("Expected 2 params: filename and password");
- }
- TempFileUtils.checkTempFiles();
- String filename = args[0];
- String password = args[1];
- FileInputStream fis = new FileInputStream(filename);
+ InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password);
try {
- InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password);
- try {
- printSheetCount(unencryptedStream);
- } finally {
- IOUtils.closeQuietly(unencryptedStream);
- }
+ printSheetCount(unencryptedStream);
} finally {
- IOUtils.closeQuietly(fis);
+ IOUtils.closeQuietly(unencryptedStream);
}
- TempFileUtils.checkTempFiles();
- } catch(Throwable t) {
- t.printStackTrace();
+ } finally {
+ IOUtils.closeQuietly(fis);
}
+ TempFileUtils.checkTempFiles();
}
public static void printSheetCount(final InputStream inputStream) throws Exception {
diff --git a/src/examples/src/org/apache/poi/xssf/streaming/examples/SXSSFWorkbookWithCustomZipEntrySource.java b/src/examples/src/org/apache/poi/xssf/streaming/examples/SXSSFWorkbookWithCustomZipEntrySource.java
deleted file mode 100644
index 48f3f3a826..0000000000
--- a/src/examples/src/org/apache/poi/xssf/streaming/examples/SXSSFWorkbookWithCustomZipEntrySource.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ====================================================================
- */
-
-package org.apache.poi.xssf.streaming.examples;
-
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.security.GeneralSecurityException;
-import java.security.SecureRandom;
-
-import javax.crypto.Cipher;
-import javax.crypto.CipherInputStream;
-import javax.crypto.CipherOutputStream;
-import javax.crypto.spec.SecretKeySpec;
-
-import org.apache.poi.crypt.examples.AesZipFileZipEntrySource;
-import org.apache.poi.crypt.examples.EncryptedTempData;
-import org.apache.poi.openxml4j.util.ZipEntrySource;
-import org.apache.poi.poifs.crypt.ChainingMode;
-import org.apache.poi.poifs.crypt.CipherAlgorithm;
-import org.apache.poi.poifs.crypt.CryptoFunctions;
-import org.apache.poi.util.IOUtils;
-import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.apache.poi.xssf.streaming.SheetDataWriter;
-
-public class SXSSFWorkbookWithCustomZipEntrySource extends SXSSFWorkbook {
-
- public SXSSFWorkbookWithCustomZipEntrySource() {
- super(20);
- setCompressTempFiles(true);
- }
-
- @Override
- public void write(OutputStream stream) throws IOException {
- flushSheets();
- EncryptedTempData tempData = new EncryptedTempData();
- ZipEntrySource source = null;
- try {
- OutputStream os = tempData.getOutputStream();
- try {
- getXSSFWorkbook().write(os);
- } finally {
- IOUtils.closeQuietly(os);
- }
- // provide ZipEntrySource to poi which decrypts on the fly
- source = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream());
- injectData(source, stream);
- } catch (GeneralSecurityException e) {
- throw new IOException(e);
- } finally {
- tempData.dispose();
- IOUtils.closeQuietly(source);
- }
- }
-
- @Override
- protected SheetDataWriter createSheetDataWriter() throws IOException {
- return new SheetDataWriterWithDecorator();
- }
-
- static class SheetDataWriterWithDecorator extends SheetDataWriter {
- final static CipherAlgorithm cipherAlgorithm = CipherAlgorithm.aes128;
- SecretKeySpec skeySpec;
- byte[] ivBytes;
-
- public SheetDataWriterWithDecorator() throws IOException {
- super();
- }
-
- void init() {
- if(skeySpec == null) {
- SecureRandom sr = new SecureRandom();
- ivBytes = new byte[16];
- byte[] keyBytes = new byte[16];
- sr.nextBytes(ivBytes);
- sr.nextBytes(keyBytes);
- skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
- }
- }
-
- @Override
- protected OutputStream decorateOutputStream(FileOutputStream fos) {
- init();
- Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding");
- return new CipherOutputStream(fos, ciEnc);
- }
-
- @Override
- protected InputStream decorateInputStream(FileInputStream fis) {
- Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, "PKCS5Padding");
- return new CipherInputStream(fis, ciDec);
- }
- }
-}
diff --git a/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java b/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java
index 4c741873a5..9aaa8bf276 100644
--- a/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java
+++ b/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java
@@ -24,13 +24,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
-import org.apache.poi.crypt.examples.EncryptedTempData;
import org.apache.poi.examples.util.TempFileUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
+import org.apache.poi.poifs.crypt.temp.EncryptedTempData;
+import org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
@@ -47,42 +48,38 @@ import org.apache.poi.xssf.streaming.SXSSFSheet;
*/
public class SavePasswordProtectedXlsx {
- public static void main(String[] args) {
+ public static void main(String[] args) throws Exception {
+ if(args.length != 2) {
+ throw new IllegalArgumentException("Expected 2 params: filename and password");
+ }
+ TempFileUtils.checkTempFiles();
+ String filename = args[0];
+ String password = args[1];
+ SXSSFWorkbookWithCustomZipEntrySource wb = new SXSSFWorkbookWithCustomZipEntrySource();
try {
- if(args.length != 2) {
- throw new Exception("Expected 2 params: filename and password");
- }
- TempFileUtils.checkTempFiles();
- String filename = args[0];
- String password = args[1];
- SXSSFWorkbookWithCustomZipEntrySource wb = new SXSSFWorkbookWithCustomZipEntrySource();
- try {
- for(int i = 0; i < 10; i++) {
- SXSSFSheet sheet = wb.createSheet("Sheet" + i);
- for(int r = 0; r < 1000; r++) {
- SXSSFRow row = sheet.createRow(r);
- for(int c = 0; c < 100; c++) {
- SXSSFCell cell = row.createCell(c);
- cell.setCellValue("abcd");
- }
+ for(int i = 0; i < 10; i++) {
+ SXSSFSheet sheet = wb.createSheet("Sheet" + i);
+ for(int r = 0; r < 1000; r++) {
+ SXSSFRow row = sheet.createRow(r);
+ for(int c = 0; c < 100; c++) {
+ SXSSFCell cell = row.createCell(c);
+ cell.setCellValue("abcd");
}
}
- EncryptedTempData tempData = new EncryptedTempData();
- try {
- wb.write(tempData.getOutputStream());
- save(tempData.getInputStream(), filename, password);
- System.out.println("Saved " + filename);
- } finally {
- tempData.dispose();
- }
+ }
+ EncryptedTempData tempData = new EncryptedTempData();
+ try {
+ wb.write(tempData.getOutputStream());
+ save(tempData.getInputStream(), filename, password);
+ System.out.println("Saved " + filename);
} finally {
- wb.close();
- wb.dispose();
+ tempData.dispose();
}
- TempFileUtils.checkTempFiles();
- } catch(Throwable t) {
- t.printStackTrace();
+ } finally {
+ wb.close();
+ wb.dispose();
}
+ TempFileUtils.checkTempFiles();
}
public static void save(final InputStream inputStream, final String filename, final String pwd)
diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/LoadPasswordProtectedXlsx.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/LoadPasswordProtectedXlsx.java
index 7bb677bf22..da6032f867 100644
--- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/LoadPasswordProtectedXlsx.java
+++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/LoadPasswordProtectedXlsx.java
@@ -22,10 +22,10 @@ package org.apache.poi.xssf.usermodel.examples;
import java.io.FileInputStream;
import java.io.InputStream;
-import org.apache.poi.crypt.examples.AesZipFileZipEntrySource;
import org.apache.poi.crypt.examples.EncryptionUtils;
import org.apache.poi.examples.util.TempFileUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -38,29 +38,25 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
*/
public class LoadPasswordProtectedXlsx {
- public static void main(String[] args) {
+ public static void main(String[] args) throws Exception {
+ if(args.length != 2) {
+ throw new IllegalArgumentException("Expected 2 params: filename and password");
+ }
+ TempFileUtils.checkTempFiles();
+ String filename = args[0];
+ String password = args[1];
+ FileInputStream fis = new FileInputStream(filename);
try {
- if(args.length != 2) {
- throw new Exception("Expected 2 params: filename and password");
- }
- TempFileUtils.checkTempFiles();
- String filename = args[0];
- String password = args[1];
- FileInputStream fis = new FileInputStream(filename);
+ InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password);
try {
- InputStream unencryptedStream = EncryptionUtils.decrypt(fis, password);
- try {
- printSheetCount(unencryptedStream);
- } finally {
- IOUtils.closeQuietly(unencryptedStream);
- }
+ printSheetCount(unencryptedStream);
} finally {
- IOUtils.closeQuietly(fis);
+ IOUtils.closeQuietly(unencryptedStream);
}
- TempFileUtils.checkTempFiles();
- } catch(Throwable t) {
- t.printStackTrace();
+ } finally {
+ IOUtils.closeQuietly(fis);
}
+ TempFileUtils.checkTempFiles();
}
public static void printSheetCount(final InputStream inputStream) throws Exception {