From: Dominik Stadler Date: Fri, 30 Dec 2016 22:12:06 +0000 (+0000) Subject: Ensure streams are closed always, reformat code somewhat X-Git-Tag: REL_3_16_BETA2~89 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9005eb2f012c6dafb3c51dd1c1991d948ee79c5f;p=poi.git Ensure streams are closed always, reformat code somewhat git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1776647 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java index c62a3c226f..8cc1d96bd8 100644 --- a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java @@ -90,8 +90,22 @@ public class OldExcelExtractor implements Closeable { if (poifs != null) { poifs.close(); } + } catch (IOException e) { + // ensure streams are closed correctly + if (poifs != null) { + poifs.close(); + } + + throw e; + } catch (RuntimeException e) { + // ensure streams are closed correctly + if (poifs != null) { + poifs.close(); + } + + throw e; } - + @SuppressWarnings("resource") FileInputStream biffStream = new FileInputStream(f); // NOSONAR try { diff --git a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java index a8e920dc11..6b18f0d9f2 100644 --- a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java +++ b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java @@ -57,9 +57,9 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { private final File fileOut; private final DirectoryNode dir; - private long pos = 0; - private long totalPos = 0; - private long written = 0; + private long pos; + private long totalPos; + private long written; // the cipher can't be final, because for the last chunk we change the padding // and therefore need to change the cipher too @@ -206,7 +206,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { * * @throws BadPaddingException * @throws IllegalBlockSizeException - * @throws ShortBufferException + * @throws ShortBufferException */ protected int invokeCipher(int posInChunk, boolean doFinal) throws GeneralSecurityException { byte plain[] = (plainByteFlags.isEmpty()) ? null : chunk.clone(); @@ -281,8 +281,11 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { os.write(buf); FileInputStream fis = new FileInputStream(fileOut); - IOUtils.copy(fis, os); - fis.close(); + try { + IOUtils.copy(fis, os); + } finally { + fis.close(); + } os.close(); diff --git a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java index 5372c2af8e..2fcd912a95 100644 --- a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java @@ -119,8 +119,7 @@ public class StandardEncryptor extends Encryptor implements Cloneable { throws IOException, GeneralSecurityException { createEncryptionInfoEntry(dir); DataSpaceMapUtils.addDefaultDataSpace(dir); - OutputStream countStream = new StandardCipherOutputStream(dir); - return countStream; + return new StandardCipherOutputStream(dir); } protected class StandardCipherOutputStream extends FilterOutputStream implements POIFSWriterListener { @@ -188,8 +187,11 @@ public class StandardEncryptor extends Encryptor implements Cloneable { leos.writeLong(countBytes); FileInputStream fis = new FileInputStream(fileOut); - IOUtils.copy(fis, leos); - fis.close(); + try { + IOUtils.copy(fis, leos); + } finally { + fis.close(); + } if (!fileOut.delete()) { logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut); } diff --git a/src/java/org/apache/poi/util/FontMetricsDumper.java b/src/java/org/apache/poi/util/FontMetricsDumper.java index 5147646571..46d9b127cd 100644 --- a/src/java/org/apache/poi/util/FontMetricsDumper.java +++ b/src/java/org/apache/poi/util/FontMetricsDumper.java @@ -24,15 +24,13 @@ import java.awt.GraphicsEnvironment; import java.awt.Toolkit; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.util.Properties; @SuppressWarnings("deprecation") -public class FontMetricsDumper -{ +public class FontMetricsDumper { @SuppressForbidden("command line tool") - public static void main( String[] args ) throws IOException - { - + public static void main(String[] args) throws IOException { Properties props = new Properties(); Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); @@ -68,13 +66,10 @@ public class FontMetricsDumper props.setProperty("font." + fontName + ".widths", widths.toString()); } - FileOutputStream fileOut = new FileOutputStream("font_metrics.properties"); - try - { + OutputStream fileOut = new FileOutputStream("font_metrics.properties"); + try { props.store(fileOut, "Font Metrics"); - } - finally - { + } finally { fileOut.close(); } } diff --git a/src/java/org/apache/poi/util/HexRead.java b/src/java/org/apache/poi/util/HexRead.java index 4d9ac97525..e191f5c7d9 100644 --- a/src/java/org/apache/poi/util/HexRead.java +++ b/src/java/org/apache/poi/util/HexRead.java @@ -25,8 +25,7 @@ import java.util.ArrayList; * Utilities to read hex from files. * TODO - move to test packages */ -public class HexRead -{ +public class HexRead { /** * This method reads hex data from a filename and returns a byte array. * The file may contain line comments that are preceeded with a # symbol. @@ -35,16 +34,12 @@ public class HexRead * @return The bytes read from the file. * @throws IOException If there was a problem while reading the file. */ - public static byte[] readData( String filename ) throws IOException - { + public static byte[] readData( String filename ) throws IOException { File file = new File( filename ); - FileInputStream stream = new FileInputStream( file ); - try - { + InputStream stream = new FileInputStream( file ); + try { return readData( stream, -1 ); - } - finally - { + } finally { stream.close(); } } @@ -59,16 +54,12 @@ public class HexRead * @see #readData(String) */ public static byte[] readData(InputStream stream, String section ) throws IOException { - - try - { + try { StringBuffer sectionText = new StringBuffer(); boolean inSection = false; int c = stream.read(); - while ( c != -1 ) - { - switch ( c ) - { + while ( c != -1 ) { + switch ( c ) { case '[': inSection = true; break; @@ -87,18 +78,15 @@ public class HexRead } c = stream.read(); } - } - finally - { + } finally { stream.close(); } + throw new IOException( "Section '" + section + "' not found" ); } - public static byte[] readData( String filename, String section ) throws IOException - { - File file = new File( filename ); - FileInputStream stream = new FileInputStream( file ); - return readData(stream, section); + + public static byte[] readData( String filename, String section ) throws IOException { + return readData(new FileInputStream( filename ), section); } @SuppressWarnings("fallthrough") @@ -110,8 +98,7 @@ public class HexRead List bytes = new ArrayList(); final char a = 'a' - 10; final char A = 'A' - 10; - while ( true ) - { + while ( true ) { int count = stream.read(); int digitValue = -1; if ( '0' <= count && count <= '9' ) { @@ -131,8 +118,7 @@ public class HexRead b <<= 4; b += (byte) digitValue; characterCount++; - if ( characterCount == 2 ) - { + if ( characterCount == 2 ) { bytes.add( Byte.valueOf( b ) ); characterCount = 0; b = (byte) 0; @@ -141,8 +127,7 @@ public class HexRead } Byte[] polished = bytes.toArray(new Byte[bytes.size()]); byte[] rval = new byte[polished.length]; - for ( int j = 0; j < polished.length; j++ ) - { + for ( int j = 0; j < polished.length; j++ ) { rval[j] = polished[j].byteValue(); } return rval; @@ -156,11 +141,9 @@ public class HexRead } } - static private void readToEOL( InputStream stream ) throws IOException - { + static private void readToEOL( InputStream stream ) throws IOException { int c = stream.read(); - while ( c != -1 && c != '\n' && c != '\r' ) - { + while ( c != -1 && c != '\n' && c != '\r' ) { c = stream.read(); } } diff --git a/src/ooxml/java/org/apache/poi/xdgf/util/VsdxToPng.java b/src/ooxml/java/org/apache/poi/xdgf/util/VsdxToPng.java index 9ab71ea8cc..c5d2ba325c 100644 --- a/src/ooxml/java/org/apache/poi/xdgf/util/VsdxToPng.java +++ b/src/ooxml/java/org/apache/poi/xdgf/util/VsdxToPng.java @@ -95,8 +95,11 @@ public class VsdxToPng { graphics.dispose(); FileOutputStream out = new FileOutputStream(outFile); - ImageIO.write(img, "png", out); - out.close(); + try { + ImageIO.write(img, "png", out); + } finally { + out.close(); + } } public static void renderToPng(XmlVisioDocument document, diff --git a/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java b/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java index 8a150323f7..c46867d2ed 100644 --- a/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java +++ b/src/testcases/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java @@ -235,6 +235,14 @@ public final class TestOldExcelExtractor { } catch (RecordFormatException e) { // expected here } + + // a POIFS file which is not a Workbook + try { + new OldExcelExtractor(POIDataSamples.getDocumentInstance().getFile("47304.doc")); + fail("Should catch Exception here"); + } catch (FileNotFoundException e) { + // expected here + } } @Test