diff options
Diffstat (limited to 'src/java')
8 files changed, 14 insertions, 47 deletions
diff --git a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java index b5f0d3f5f0..90a3015166 100644 --- a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java +++ b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java @@ -55,10 +55,8 @@ public class FormulaViewer * @throws IOException if the file contained errors */ public void run() throws IOException { - POIFSFileSystem fs = new POIFSFileSystem(new File(file), true); - try { - InputStream is = BiffViewer.getPOIFSInputStream(fs); - try { + try (POIFSFileSystem fs = new POIFSFileSystem(new File(file), true)) { + try (InputStream is = BiffViewer.getPOIFSInputStream(fs)) { List<Record> records = RecordFactory.createRecords(is); for (Record record : records) { @@ -70,11 +68,7 @@ public class FormulaViewer } } } - } finally { - is.close(); } - } finally { - fs.close(); } } diff --git a/src/java/org/apache/poi/hssf/eventusermodel/HSSFEventFactory.java b/src/java/org/apache/poi/hssf/eventusermodel/HSSFEventFactory.java index 32463a55e2..1283ae517b 100644 --- a/src/java/org/apache/poi/hssf/eventusermodel/HSSFEventFactory.java +++ b/src/java/org/apache/poi/hssf/eventusermodel/HSSFEventFactory.java @@ -76,11 +76,8 @@ public class HSSFEventFactory { name = WORKBOOK_DIR_ENTRY_NAMES[0]; } - InputStream in = dir.createDocumentInputStream(name); - try { + try (InputStream in = dir.createDocumentInputStream(name)) { processEvents(req, in); - } finally { - in.close(); } } @@ -111,12 +108,9 @@ public class HSSFEventFactory { */ public short abortableProcessWorkbookEvents(HSSFRequest req, DirectoryNode dir) throws IOException, HSSFUserException { - InputStream in = dir.createDocumentInputStream("Workbook"); - try { - return abortableProcessEvents(req, in); - } finally { - in.close(); - } + try (InputStream in = dir.createDocumentInputStream("Workbook")) { + return abortableProcessEvents(req, in); + } } /** 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 e3662b40ba..4c4ca2a3fb 100644 --- a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java @@ -186,11 +186,8 @@ public class StandardEncryptor extends Encryptor implements Cloneable { // value, depending on the block size of the chosen encryption algorithm leos.writeLong(countBytes); - FileInputStream fis = new FileInputStream(fileOut); - try { + try (FileInputStream fis = new FileInputStream(fileOut)) { 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/ss/format/CellElapsedFormatter.java b/src/java/org/apache/poi/ss/format/CellElapsedFormatter.java index 45c9b92f68..4a5e370e86 100644 --- a/src/java/org/apache/poi/ss/format/CellElapsedFormatter.java +++ b/src/java/org/apache/poi/ss/format/CellElapsedFormatter.java @@ -200,11 +200,8 @@ public class CellElapsedFormatter extends CellFormatter { parts[i] = specs.get(i).valueFor(elapsed); } - Formatter formatter = new Formatter(toAppendTo, Locale.ROOT); - try { + try (Formatter formatter = new Formatter(toAppendTo, Locale.ROOT)) { formatter.format(printfFmt, parts); - } finally { - formatter.close(); } } diff --git a/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java b/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java index 37f202c9dd..2233c284aa 100644 --- a/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java +++ b/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java @@ -63,11 +63,8 @@ public class CellGeneralFormatter extends CellFormatter { stripZeros = false; } - Formatter formatter = new Formatter(toAppendTo, locale); - try { + try (Formatter formatter = new Formatter(toAppendTo, locale)) { formatter.format(locale, fmt, value); - } finally { - formatter.close(); } if (stripZeros) { // strip off trailing zeros diff --git a/src/java/org/apache/poi/ss/format/CellNumberFormatter.java b/src/java/org/apache/poi/ss/format/CellNumberFormatter.java index 6ab581d52d..7a3070a542 100644 --- a/src/java/org/apache/poi/ss/format/CellNumberFormatter.java +++ b/src/java/org/apache/poi/ss/format/CellNumberFormatter.java @@ -456,11 +456,8 @@ public class CellNumberFormatter extends CellFormatter { writeFraction(value, null, fractional, output, mods); } else { StringBuffer result = new StringBuffer(); - Formatter f = new Formatter(result, locale); - try { + try (Formatter f = new Formatter(result, locale)) { f.format(locale, printfFmt, value); - } finally { - f.close(); } if (numerator == null) { @@ -734,11 +731,8 @@ public class CellNumberFormatter extends CellFormatter { private void writeSingleInteger(String fmt, int num, StringBuffer output, List<Special> numSpecials, Set<CellNumberStringMod> mods) { StringBuffer sb = new StringBuffer(); - Formatter formatter = new Formatter(sb, locale); - try { + try (Formatter formatter = new Formatter(sb, locale)) { formatter.format(locale, fmt, num); - } finally { - formatter.close(); } writeInteger(sb, output, numSpecials, mods, false); } diff --git a/src/java/org/apache/poi/util/FontMetricsDumper.java b/src/java/org/apache/poi/util/FontMetricsDumper.java index 46d9b127cd..321f0d5ea9 100644 --- a/src/java/org/apache/poi/util/FontMetricsDumper.java +++ b/src/java/org/apache/poi/util/FontMetricsDumper.java @@ -66,11 +66,8 @@ public class FontMetricsDumper { props.setProperty("font." + fontName + ".widths", widths.toString()); } - OutputStream fileOut = new FileOutputStream("font_metrics.properties"); - try { + try (OutputStream fileOut = new FileOutputStream("font_metrics.properties")) { props.store(fileOut, "Font Metrics"); - } finally { - fileOut.close(); } } } diff --git a/src/java/org/apache/poi/util/HexRead.java b/src/java/org/apache/poi/util/HexRead.java index a7bec002f9..c6a5fb4f73 100644 --- a/src/java/org/apache/poi/util/HexRead.java +++ b/src/java/org/apache/poi/util/HexRead.java @@ -36,11 +36,8 @@ public class HexRead { */ public static byte[] readData( String filename ) throws IOException { File file = new File( filename ); - InputStream stream = new FileInputStream( file ); - try { - return readData( stream, -1 ); - } finally { - stream.close(); + try (InputStream stream = new FileInputStream(file)) { + return readData(stream, -1); } } |