aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-11-20 13:51:00 +0000
committerPJ Fanning <fanningpj@apache.org>2021-11-20 13:51:00 +0000
commitdeab1884378f082cca2f6acc6ab0b049b6b03cbe (patch)
tree76e3c4347921800b278bffd140acc4ef643af294 /poi-ooxml
parent0825c7a4f8f3878f17b2a1d667478e8965ea1150 (diff)
downloadpoi-deab1884378f082cca2f6acc6ab0b049b6b03cbe.tar.gz
poi-deab1884378f082cca2f6acc6ab0b049b6b03cbe.zip
use try block to close input streams
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895203 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java7
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java6
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xslf/util/PDFFormat.java20
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java42
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java36
5 files changed, 60 insertions, 51 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
index 6cd0a02815..f17a163c8a 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
@@ -464,8 +464,11 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
closeImpl();
}
} else if (this.output != null) {
- save(this.output);
- output.close();
+ try {
+ save(this.output);
+ } finally {
+ output.close();
+ }
}
// ensure all held resources are freed
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java
index 11f94477f3..c2a0bd81a9 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java
@@ -182,9 +182,9 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
}
public void setData(byte[] data) throws IOException {
- OutputStream os = getPackagePart().getOutputStream();
- os.write(data);
- os.close();
+ try (OutputStream os = getPackagePart().getOutputStream()) {
+ os.write(data);
+ }
// recalculate now since we already have the data bytes available anyhow
checksum = IOUtils.calculateChecksum(data);
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/PDFFormat.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/PDFFormat.java
index 1b71fb3f7b..44675edcf7 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/PDFFormat.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/PDFFormat.java
@@ -61,11 +61,14 @@ public class PDFFormat implements OutputFormat {
@Override
public void writeSlide(MFProxy proxy, File outFile) throws IOException {
- pdfBoxGraphics2D.dispose();
+ try {
+ pdfBoxGraphics2D.dispose();
- PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
- contentStream.drawForm(appearanceStream);
- contentStream.close();
+ PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
+ contentStream.drawForm(appearanceStream);
+ } finally {
+ contentStream.close();
+ }
}
@Override
@@ -75,9 +78,12 @@ public class PDFFormat implements OutputFormat {
@Override
public void close() throws IOException {
- document.close();
- if (fontTextDrawer != null) {
- fontTextDrawer.close();
+ try {
+ document.close();
+ } finally {
+ if (fontTextDrawer != null) {
+ fontTextDrawer.close();
+ }
}
}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java
index a4c56db4cd..c53cc39312 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java
@@ -115,28 +115,28 @@ public class XSSFBEventBasedExcelExtractor extends XSSFEventBasedExcelExtractor
SheetTextExtractor sheetExtractor = new SheetTextExtractor();
XSSFBHyperlinksTable hyperlinksTable = null;
while (iter.hasNext()) {
- InputStream stream = iter.next();
- if (getIncludeSheetNames()) {
- text.append(iter.getSheetName());
- text.append('\n');
+ try (InputStream stream = iter.next()) {
+ if (getIncludeSheetNames()) {
+ text.append(iter.getSheetName());
+ text.append('\n');
+ }
+ if (handleHyperlinksInCells) {
+ hyperlinksTable = new XSSFBHyperlinksTable(iter.getSheetPart());
+ }
+ XSSFBCommentsTable comments = getIncludeCellComments() ? iter.getXSSFBSheetComments() : null;
+ processSheet(sheetExtractor, styles, comments, strings, stream);
+ if (getIncludeHeadersFooters()) {
+ sheetExtractor.appendHeaderText(text);
+ }
+ sheetExtractor.appendCellText(text);
+ if (getIncludeTextBoxes()) {
+ processShapes(iter.getShapes(), text);
+ }
+ if (getIncludeHeadersFooters()) {
+ sheetExtractor.appendFooterText(text);
+ }
+ sheetExtractor.reset();
}
- if (handleHyperlinksInCells) {
- hyperlinksTable = new XSSFBHyperlinksTable(iter.getSheetPart());
- }
- XSSFBCommentsTable comments = getIncludeCellComments() ? iter.getXSSFBSheetComments() : null;
- processSheet(sheetExtractor, styles, comments, strings, stream);
- if (getIncludeHeadersFooters()) {
- sheetExtractor.appendHeaderText(text);
- }
- sheetExtractor.appendCellText(text);
- if (getIncludeTextBoxes()) {
- processShapes(iter.getShapes(), text);
- }
- if (getIncludeHeadersFooters()) {
- sheetExtractor.appendFooterText(text);
- }
- sheetExtractor.reset();
- stream.close();
}
return text.toString();
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
index 35fcb58685..64d90fdec3 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
@@ -267,25 +267,25 @@ public class XSSFEventBasedExcelExtractor
SheetTextExtractor sheetExtractor = new SheetTextExtractor();
while (iter.hasNext()) {
- InputStream stream = iter.next();
- if (includeSheetNames) {
- text.append(iter.getSheetName());
- text.append('\n');
+ try (InputStream stream = iter.next()) {
+ if (includeSheetNames) {
+ text.append(iter.getSheetName());
+ text.append('\n');
+ }
+ Comments comments = includeCellComments ? iter.getSheetComments() : null;
+ processSheet(sheetExtractor, styles, comments, strings, stream);
+ if (includeHeadersFooters) {
+ sheetExtractor.appendHeaderText(text);
+ }
+ sheetExtractor.appendCellText(text);
+ if (includeTextBoxes) {
+ processShapes(iter.getShapes(), text);
+ }
+ if (includeHeadersFooters) {
+ sheetExtractor.appendFooterText(text);
+ }
+ sheetExtractor.reset();
}
- Comments comments = includeCellComments ? iter.getSheetComments() : null;
- processSheet(sheetExtractor, styles, comments, strings, stream);
- if (includeHeadersFooters) {
- sheetExtractor.appendHeaderText(text);
- }
- sheetExtractor.appendCellText(text);
- if (includeTextBoxes) {
- processShapes(iter.getShapes(), text);
- }
- if (includeHeadersFooters) {
- sheetExtractor.appendFooterText(text);
- }
- sheetExtractor.reset();
- stream.close();
}
return text.toString();