EncryptedTempData tempData = new EncryptedTempData();
ZipEntrySource source = null;
try {
- OutputStream os = tempData.getOutputStream();
- try {
+ try (OutputStream os = tempData.getOutputStream()) {
getXSSFWorkbook().write(os);
- } finally {
- IOUtils.closeQuietly(os);
}
// provide ZipEntrySource to poi which decrypts on the fly
source = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream());
throw new FileNotFoundException(file.toString());
}
- try {
- NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly);
- try {
- return create(fs, password);
- } catch (RuntimeException e) {
- // ensure that the file-handle is closed again
- IOUtils.closeQuietly(fs);
- throw e;
- }
+ try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly)) {
+ return create(fs, password);
} catch(OfficeXmlFileException e) {
// opening as .xls failed => try opening as .xlsx
OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE); // NOSONAR
* @throws IOException when there's a problem with the workbook part's stream
*/
public String getAbsPathMetadata() throws IOException {
- InputStream is = null;
- try {
- is = workbookPart.getInputStream();
- PathExtractor p = new PathExtractor(workbookPart.getInputStream());
+ try (InputStream is = workbookPart.getInputStream()) {
+ PathExtractor p = new PathExtractor(is);
p.parse();
return p.getPath();
- } finally {
- IOUtils.closeQuietly(is);
}
}
}
@Override
- @SuppressWarnings("resource")
public DirectoryEntry getDirectory() throws IOException {
- InputStream is = null;
- try {
- is = getObjectPart().getInputStream();
+ try (InputStream is = getObjectPart().getInputStream()) {
return new POIFSFileSystem(is).getRoot();
- } finally {
- IOUtils.closeQuietly(is);
}
}
String getText(Charset charset) throws IOException {
StringBuilder sb = new StringBuilder();
- Reader r = null;
- try {
- r = new InputStreamReader(new ByteArrayInputStream(rawTextBytes), charset);
+ try (Reader r = new InputStreamReader(new ByteArrayInputStream(rawTextBytes), charset)) {
for (int i = 0; i < numChars; i++) {
sb.appendCodePoint(readCodePoint(r));
}
- } finally {
- IOUtils.closeQuietly(r);
}
return sb.toString();
}
Decryptor dec = getEncryptionInfo().getDecryptor();
dec.setChunkSize(-1);
- LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(docstream, offset); // NOSONAR
- ChunkedCipherInputStream ccis = null;
- try {
- ccis = (ChunkedCipherInputStream)dec.getDataStream(lei, docstream.length-offset, 0);
+ try (LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(docstream, offset);
+ ChunkedCipherInputStream ccis = (ChunkedCipherInputStream)dec.getDataStream(lei, docstream.length-offset, 0)) {
ccis.initCipherForBlock(persistId);
// decrypt header and read length to be decrypted
} catch (Exception e) {
throw new EncryptedPowerPointFileException(e);
- } finally {
- IOUtils.closeQuietly(ccis);
- IOUtils.closeQuietly(lei);
}
}
return;
}
- LittleEndianByteArrayOutputStream los = new LittleEndianByteArrayOutputStream(pictstream, offset); // NOSONAR
ChunkedCipherOutputStream ccos = null;
- try {
+ try (LittleEndianByteArrayOutputStream los = new LittleEndianByteArrayOutputStream(pictstream, offset)) {
Encryptor enc = getEncryptionInfo().getEncryptor();
enc.setChunkSize(-1);
ccos = enc.getDataStream(los, 0);
throw new EncryptedPowerPointFileException(e);
} finally {
IOUtils.closeQuietly(ccos);
- IOUtils.closeQuietly(los);
}
}