use try block to close output streams

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895197 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-11-20 12:53:20 +00:00
parent 14c7218be8
commit 363abd6efe
5 changed files with 16 additions and 15 deletions

View File

@ -16,11 +16,7 @@
==================================================================== */
package org.apache.poi.ooxml.dev;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -47,7 +43,7 @@ import org.xml.sax.InputSource;
* Reads a zipped OOXML file and produces a copy with the included
* pretty-printed XML files.
*
* This is useful for comparing OOXML files produced by different tools as the often
* This is useful for comparing OOXML files produced by different tools as they often
* use different formatting of the XML.
*/
public class OOXMLPrettyPrint {
@ -108,7 +104,9 @@ public class OOXMLPrettyPrint {
pretty(document, out, 2);
} else {
System.out.println("Not pretty-printing non-XML file " + name);
IOUtils.copy(file.getInputStream(entry), out);
try (InputStream in = file.getInputStream(entry)) {
IOUtils.copy(in, out);
}
}
} catch (Exception e) {
throw new IOException("While handling entry " + name, e);

View File

@ -73,8 +73,11 @@ public final class PackageHelper {
dest.addRelationship(part.getPartName(), rel.getTargetMode(), rel.getRelationshipType());
part_tgt = dest.createPart(part.getPartName(), part.getContentType());
try (OutputStream out = part_tgt.getOutputStream()) {
IOUtils.copy(part.getInputStream(), out);
try (
InputStream in = part.getInputStream();
OutputStream out = part_tgt.getOutputStream()
) {
IOUtils.copy(in, out);
}
if (part.hasRelationships()) {

View File

@ -118,9 +118,9 @@ public final class EncryptedTempFilePackagePart extends PackagePart {
}
@Override
public boolean load(InputStream ios) throws InvalidFormatException {
public boolean load(InputStream is) throws InvalidFormatException {
try (OutputStream os = getOutputStreamImpl()) {
IOUtils.copy(ios, os);
IOUtils.copy(is, os);
} catch(IOException e) {
throw new InvalidFormatException(e.getMessage(), e);
}

View File

@ -112,10 +112,10 @@ public final class MemoryPackagePart extends PackagePart {
}
@Override
public boolean load(InputStream ios) throws InvalidFormatException {
public boolean load(InputStream is) throws InvalidFormatException {
try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
// Grab the data
IOUtils.copy(ios, baos);
IOUtils.copy(is, baos);
// Save it
data = baos.toByteArray();
} catch (IOException e) {

View File

@ -117,9 +117,9 @@ public final class TempFilePackagePart extends PackagePart {
}
@Override
public boolean load(InputStream ios) throws InvalidFormatException {
public boolean load(InputStream is) throws InvalidFormatException {
try (OutputStream os = getOutputStreamImpl()) {
IOUtils.copy(ios, os);
IOUtils.copy(is, os);
} catch(IOException e) {
throw new InvalidFormatException(e.getMessage(), e);
}