diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-12-28 10:51:24 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-12-28 10:51:24 +0000 |
commit | 70dc07dd3b6a6bc7c85546f8a1ba30e9851b1e83 (patch) | |
tree | a745b64c3503132c45a1bafce26fb14140a888f7 | |
parent | dcdb14c5f2711c0fc6d5e3c268e7c0113f081564 (diff) | |
download | poi-70dc07dd3b6a6bc7c85546f8a1ba30e9851b1e83.tar.gz poi-70dc07dd3b6a6bc7c85546f8a1ba30e9851b1e83.zip |
close cursors in finally blocks
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896472 13f79535-47bb-0310-9956-ffa450edef68
3 files changed, 45 insertions, 28 deletions
diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java index 3c52ecf253..68ea9229ef 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java @@ -101,33 +101,46 @@ public final class LoadEmbedded { String contentType = pPart.getContentType(); if (contentType.equals("application/vnd.ms-excel")) { // Excel Workbook - either binary or OpenXML - HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream()); - embeddedWorkbook.close(); + try (InputStream stream = pPart.getInputStream()) { + HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(stream); + embeddedWorkbook.close(); + } } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { // Excel Workbook - OpenXML file format - XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream()); - embeddedWorkbook.close(); + try (InputStream stream = pPart.getInputStream()) { + XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(stream); + embeddedWorkbook.close(); + } } else if (contentType.equals("application/msword")) { // Word Document - binary (OLE2CDF) file format - HWPFDocument document = new HWPFDocument(pPart.getInputStream()); - document.close(); + try (InputStream stream = pPart.getInputStream()) { + HWPFDocument document = new HWPFDocument(stream); + document.close(); + } } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) { // Word Document - OpenXML file format - XWPFDocument document = new XWPFDocument(pPart.getInputStream()); - document.close(); + try (InputStream stream = pPart.getInputStream()) { + XWPFDocument document = new XWPFDocument(stream); + document.close(); + } } else if (contentType.equals("application/vnd.ms-powerpoint")) { // PowerPoint Document - binary file format - HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream()); - slideShow.close(); + try (InputStream stream = pPart.getInputStream()) { + HSLFSlideShow slideShow = new HSLFSlideShow(stream); + slideShow.close(); + } } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) { // PowerPoint Document - OpenXML file format - XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream()); - slideShow.close(); + try (InputStream stream = pPart.getInputStream()) { + XMLSlideShow slideShow = new XMLSlideShow(stream); + slideShow.close(); + } } else { // Any other type of embedded object. System.out.println("Unknown Embedded Document: " + contentType); - InputStream inputStream = pPart.getInputStream(); - inputStream.close(); + try (InputStream inputStream = pPart.getInputStream()) { + + } } } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xslf/AddVideoToPptx.java.txt b/poi-examples/src/main/java/org/apache/poi/examples/xslf/AddVideoToPptx.java.txt index 2d32c4d81b..835fe63d6d 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xslf/AddVideoToPptx.java.txt +++ b/poi-examples/src/main/java/org/apache/poi/examples/xslf/AddVideoToPptx.java.txt @@ -166,14 +166,16 @@ public class AddVideoToPptx { ext.setUri("{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}");
String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main";
XmlCursor cur = ext.newCursor();
- cur.toEndToken();
- cur.beginElement(new QName(p14Ns, "media", "p14"));
- cur.insertNamespace("p14", p14Ns);
- cur.insertAttributeWithValue(new QName(STRelationshipId.type.getName().getNamespaceURI(), "embed"), prsEmbed1.getId());
- cur.beginElement(new QName(p14Ns, "trim", "p14"));
- cur.insertAttributeWithValue("st", df_time.format(seconds*1000.0));
- cur.dispose();
-
+ try {
+ cur.toEndToken();
+ cur.beginElement(new QName(p14Ns, "media", "p14"));
+ cur.insertNamespace("p14", p14Ns);
+ cur.insertAttributeWithValue(new QName(STRelationshipId.type.getName().getNamespaceURI(), "embed"), prsEmbed1.getId());
+ cur.beginElement(new QName(p14Ns, "trim", "p14"));
+ cur.insertAttributeWithValue("st", df_time.format(seconds*1000.0));
+ } finally {
+ cur.dispose();
+ }
}
static void addTimingInfo(XSLFSlide slide1, XSLFPictureShape pic1) {
diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xslf/LinkVideoToPptx.java b/poi-examples/src/main/java/org/apache/poi/examples/xslf/LinkVideoToPptx.java index 9fcf312011..265fa730d4 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xslf/LinkVideoToPptx.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xslf/LinkVideoToPptx.java @@ -85,12 +85,14 @@ public final class LinkVideoToPptx { String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main"; XmlCursor cur = ext.newCursor(); - cur.toEndToken(); - cur.beginElement(new QName(p14Ns, "media", "p14")); - cur.insertNamespace("p14", p14Ns); - cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId()); - cur.dispose(); - + try { + cur.toEndToken(); + cur.beginElement(new QName(p14Ns, "media", "p14")); + cur.insertNamespace("p14", p14Ns); + cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId()); + } finally { + cur.dispose(); + } CTSlide xslide = slide1.getXmlObject(); CTTimeNodeList ctnl; |