use try with resources in examples

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1816189 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2017-11-23 20:03:25 +00:00
parent 3e4e43b28b
commit 76a16ba175
6 changed files with 10 additions and 40 deletions

View File

@ -46,11 +46,8 @@ public class SXSSFWorkbookWithCustomZipEntrySource extends SXSSFWorkbook {
EncryptedTempData tempData = new EncryptedTempData(); EncryptedTempData tempData = new EncryptedTempData();
ZipEntrySource source = null; ZipEntrySource source = null;
try { try {
OutputStream os = tempData.getOutputStream(); try (OutputStream os = tempData.getOutputStream()) {
try {
getXSSFWorkbook().write(os); getXSSFWorkbook().write(os);
} finally {
IOUtils.closeQuietly(os);
} }
// provide ZipEntrySource to poi which decrypts on the fly // provide ZipEntrySource to poi which decrypts on the fly
source = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream()); source = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream());

View File

@ -250,15 +250,8 @@ public class WorkbookFactory {
throw new FileNotFoundException(file.toString()); throw new FileNotFoundException(file.toString());
} }
try { try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly)) {
NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly); return create(fs, password);
try {
return create(fs, password);
} catch (RuntimeException e) {
// ensure that the file-handle is closed again
IOUtils.closeQuietly(fs);
throw e;
}
} catch(OfficeXmlFileException e) { } catch(OfficeXmlFileException e) {
// opening as .xls failed => try opening as .xlsx // opening as .xls failed => try opening as .xlsx
OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE); // NOSONAR OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE); // NOSONAR

View File

@ -87,14 +87,10 @@ public class XSSFBReader extends XSSFReader {
* @throws IOException when there's a problem with the workbook part's stream * @throws IOException when there's a problem with the workbook part's stream
*/ */
public String getAbsPathMetadata() throws IOException { public String getAbsPathMetadata() throws IOException {
InputStream is = null; try (InputStream is = workbookPart.getInputStream()) {
try { PathExtractor p = new PathExtractor(is);
is = workbookPart.getInputStream();
PathExtractor p = new PathExtractor(workbookPart.getInputStream());
p.parse(); p.parse();
return p.getPath(); return p.getPath();
} finally {
IOUtils.closeQuietly(is);
} }
} }

View File

@ -171,14 +171,9 @@ public class XSSFObjectData extends XSSFSimpleShape implements ObjectData {
} }
@Override @Override
@SuppressWarnings("resource")
public DirectoryEntry getDirectory() throws IOException { public DirectoryEntry getDirectory() throws IOException {
InputStream is = null; try (InputStream is = getObjectPart().getInputStream()) {
try {
is = getObjectPart().getInputStream();
return new POIFSFileSystem(is).getRoot(); return new POIFSFileSystem(is).getRoot();
} finally {
IOUtils.closeQuietly(is);
} }
} }

View File

@ -230,14 +230,10 @@ public class HemfText {
String getText(Charset charset) throws IOException { String getText(Charset charset) throws IOException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Reader r = null; try (Reader r = new InputStreamReader(new ByteArrayInputStream(rawTextBytes), charset)) {
try {
r = new InputStreamReader(new ByteArrayInputStream(rawTextBytes), charset);
for (int i = 0; i < numChars; i++) { for (int i = 0; i < numChars; i++) {
sb.appendCodePoint(readCodePoint(r)); sb.appendCodePoint(readCodePoint(r));
} }
} finally {
IOUtils.closeQuietly(r);
} }
return sb.toString(); return sb.toString();
} }

View File

@ -179,10 +179,8 @@ public class HSLFSlideShowEncrypted implements Closeable {
Decryptor dec = getEncryptionInfo().getDecryptor(); Decryptor dec = getEncryptionInfo().getDecryptor();
dec.setChunkSize(-1); dec.setChunkSize(-1);
LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(docstream, offset); // NOSONAR try (LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(docstream, offset);
ChunkedCipherInputStream ccis = null; ChunkedCipherInputStream ccis = (ChunkedCipherInputStream)dec.getDataStream(lei, docstream.length-offset, 0)) {
try {
ccis = (ChunkedCipherInputStream)dec.getDataStream(lei, docstream.length-offset, 0);
ccis.initCipherForBlock(persistId); ccis.initCipherForBlock(persistId);
// decrypt header and read length to be decrypted // decrypt header and read length to be decrypted
@ -193,9 +191,6 @@ public class HSLFSlideShowEncrypted implements Closeable {
} catch (Exception e) { } catch (Exception e) {
throw new EncryptedPowerPointFileException(e); throw new EncryptedPowerPointFileException(e);
} finally {
IOUtils.closeQuietly(ccis);
IOUtils.closeQuietly(lei);
} }
} }
@ -288,10 +283,9 @@ public class HSLFSlideShowEncrypted implements Closeable {
return; return;
} }
LittleEndianByteArrayOutputStream los = new LittleEndianByteArrayOutputStream(pictstream, offset); // NOSONAR
ChunkedCipherOutputStream ccos = null; ChunkedCipherOutputStream ccos = null;
try { try (LittleEndianByteArrayOutputStream los = new LittleEndianByteArrayOutputStream(pictstream, offset)) {
Encryptor enc = getEncryptionInfo().getEncryptor(); Encryptor enc = getEncryptionInfo().getEncryptor();
enc.setChunkSize(-1); enc.setChunkSize(-1);
ccos = enc.getDataStream(los, 0); ccos = enc.getDataStream(los, 0);
@ -362,7 +356,6 @@ public class HSLFSlideShowEncrypted implements Closeable {
throw new EncryptedPowerPointFileException(e); throw new EncryptedPowerPointFileException(e);
} finally { } finally {
IOUtils.closeQuietly(ccos); IOUtils.closeQuietly(ccos);
IOUtils.closeQuietly(los);
} }
} }