diff --git a/jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java b/jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java index 2d500cea759..defecf46418 100644 --- a/jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java +++ b/jdk/src/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java @@ -59,6 +59,7 @@ import com.sun.imageio.plugins.common.SubImageInputStream; import java.io.ByteArrayOutputStream; import sun.awt.image.ByteInterleavedRaster; +import sun.misc.IOUtils; class PNGImageDataEnumeration implements Enumeration { @@ -620,18 +621,9 @@ private void parse_tRNS_chunk(int chunkLength) throws IOException { private static byte[] inflate(byte[] b) throws IOException { InputStream bais = new ByteArrayInputStream(b); - InputStream iis = new InflaterInputStream(bais); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - int c; - try { - while ((c = iis.read()) != -1) { - baos.write(c); - } - } finally { - iis.close(); + try (InputStream iis = new InflaterInputStream(bais)) { + return IOUtils.readAllBytes(iis); } - return baos.toByteArray(); } private void parse_zTXt_chunk(int chunkLength) throws IOException { diff --git a/jdk/src/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java b/jdk/src/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java index 72167580a92..b874164ff1a 100644 --- a/jdk/src/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java +++ b/jdk/src/share/classes/com/sun/java/swing/plaf/gtk/Metacity.java @@ -531,14 +531,16 @@ public Object run() { URL url = new URL(new File(userHome).toURI().toURL(), ".gconf/apps/metacity/general/%25gconf.xml"); // Pending: verify character encoding spec for gconf - Reader reader = new InputStreamReader(url.openStream(), "ISO-8859-1"); - char[] buf = new char[1024]; StringBuffer strBuf = new StringBuffer(); - int n; - while ((n = reader.read(buf)) >= 0) { - strBuf.append(buf, 0, n); + try (InputStream in = url.openStream(); + Reader reader = new InputStreamReader(in, "ISO-8859-1")) + { + char[] buf = new char[1024]; + int n; + while ((n = reader.read(buf)) >= 0) { + strBuf.append(buf, 0, n); + } } - reader.close(); String str = strBuf.toString(); if (str != null) { String strLowerCase = str.toLowerCase(); diff --git a/jdk/src/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java b/jdk/src/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java index 841de2f9b02..fb4b442ad62 100644 --- a/jdk/src/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java +++ b/jdk/src/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java @@ -47,10 +47,8 @@ public final class AudioFileSoundbankReader extends SoundbankReader { public Soundbank getSoundbank(URL url) throws InvalidMidiDataException, IOException { - try { - AudioInputStream ais = AudioSystem.getAudioInputStream(url); + try (AudioInputStream ais = AudioSystem.getAudioInputStream(url)) { Soundbank sbk = getSoundbank(ais); - ais.close(); return sbk; } catch (UnsupportedAudioFileException e) { return null; diff --git a/jdk/src/share/classes/com/sun/media/sound/DLSSoundbank.java b/jdk/src/share/classes/com/sun/media/sound/DLSSoundbank.java index 1aa62d9943c..3e9ea9c19ad 100644 --- a/jdk/src/share/classes/com/sun/media/sound/DLSSoundbank.java +++ b/jdk/src/share/classes/com/sun/media/sound/DLSSoundbank.java @@ -186,22 +186,16 @@ public DLSSoundbank() { } public DLSSoundbank(URL url) throws IOException { - InputStream is = url.openStream(); - try { + try (InputStream is = url.openStream()) { readSoundbank(is); - } finally { - is.close(); } } public DLSSoundbank(File file) throws IOException { largeFormat = true; sampleFile = file; - InputStream is = new FileInputStream(file); - try { + try (InputStream is = new FileInputStream(file)) { readSoundbank(is); - } finally { - is.close(); } } @@ -870,15 +864,21 @@ private void readWaveInfoChunk(DLSSample dlssample, RIFFReader riff) } public void save(String name) throws IOException { - writeSoundbank(new RIFFWriter(name, "DLS ")); + try (RIFFWriter writer = new RIFFWriter(name, "DLS ")) { + writeSoundbank(writer); + } } public void save(File file) throws IOException { - writeSoundbank(new RIFFWriter(file, "DLS ")); + try (RIFFWriter writer = new RIFFWriter(file, "DLS ")) { + writeSoundbank(writer); + } } public void save(OutputStream out) throws IOException { - writeSoundbank(new RIFFWriter(out, "DLS ")); + try (RIFFWriter writer = new RIFFWriter(out, "DLS ")) { + writeSoundbank(writer); + } } private void writeSoundbank(RIFFWriter writer) throws IOException { @@ -918,8 +918,6 @@ private void writeSoundbank(RIFFWriter writer) throws IOException { writer.seek(bak); writeInfo(writer.writeList("INFO"), info); - - writer.close(); } private void writeSample(RIFFWriter writer, DLSSample sample) diff --git a/jdk/src/share/classes/com/sun/media/sound/JARSoundbankReader.java b/jdk/src/share/classes/com/sun/media/sound/JARSoundbankReader.java index 7bc1664f9c2..4cc1c4de4a8 100644 --- a/jdk/src/share/classes/com/sun/media/sound/JARSoundbankReader.java +++ b/jdk/src/share/classes/com/sun/media/sound/JARSoundbankReader.java @@ -61,8 +61,7 @@ public final class JARSoundbankReader extends SoundbankReader { private static boolean isZIP(URL url) { boolean ok = false; try { - InputStream stream = url.openStream(); - try { + try (InputStream stream = url.openStream()) { byte[] buff = new byte[4]; ok = stream.read(buff) == 4; if (ok) { @@ -71,8 +70,6 @@ private static boolean isZIP(URL url) { && buff[2] == 0x03 && buff[3] == 0x04); } - } finally { - stream.close(); } } catch (IOException e) { } diff --git a/jdk/src/share/classes/com/sun/media/sound/ModelByteBuffer.java b/jdk/src/share/classes/com/sun/media/sound/ModelByteBuffer.java index fb82f4d9f86..08b51865bb9 100644 --- a/jdk/src/share/classes/com/sun/media/sound/ModelByteBuffer.java +++ b/jdk/src/share/classes/com/sun/media/sound/ModelByteBuffer.java @@ -309,11 +309,13 @@ public void load() throws IOException { "No file associated with this ByteBuffer!"); } - DataInputStream is = new DataInputStream(getInputStream()); - buffer = new byte[(int) capacity()]; - offset = 0; - is.readFully(buffer); - is.close(); + try (InputStream is = getInputStream(); + DataInputStream dis = new DataInputStream(is)) + { + buffer = new byte[(int) capacity()]; + offset = 0; + dis.readFully(buffer); + } } diff --git a/jdk/src/share/classes/com/sun/media/sound/ModelByteBufferWavetable.java b/jdk/src/share/classes/com/sun/media/sound/ModelByteBufferWavetable.java index 33515f400d8..e40ac9dfe77 100644 --- a/jdk/src/share/classes/com/sun/media/sound/ModelByteBufferWavetable.java +++ b/jdk/src/share/classes/com/sun/media/sound/ModelByteBufferWavetable.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -172,18 +172,12 @@ public AudioFormat getFormat() { if (format == null) { if (buffer == null) return null; - InputStream is = buffer.getInputStream(); AudioFormat format = null; - try { + try (InputStream is = buffer.getInputStream()) { format = AudioSystem.getAudioFileFormat(is).getFormat(); } catch (Exception e) { //e.printStackTrace(); } - try { - is.close(); - } catch (IOException e) { - //e.printStackTrace(); - } return format; } return format; diff --git a/jdk/src/share/classes/com/sun/media/sound/SF2Soundbank.java b/jdk/src/share/classes/com/sun/media/sound/SF2Soundbank.java index 041c8aa1c27..86b8dc92f86 100644 --- a/jdk/src/share/classes/com/sun/media/sound/SF2Soundbank.java +++ b/jdk/src/share/classes/com/sun/media/sound/SF2Soundbank.java @@ -89,23 +89,16 @@ public SF2Soundbank() { } public SF2Soundbank(URL url) throws IOException { - - InputStream is = url.openStream(); - try { + try (InputStream is = url.openStream()) { readSoundbank(is); - } finally { - is.close(); } } public SF2Soundbank(File file) throws IOException { largeFormat = true; sampleFile = file; - InputStream is = new FileInputStream(file); - try { + try (InputStream is = new FileInputStream(file)) { readSoundbank(is); - } finally { - is.close(); } } @@ -522,22 +515,27 @@ private void readPdtaChunk(RIFFReader riff) throws IOException { } public void save(String name) throws IOException { - writeSoundbank(new RIFFWriter(name, "sfbk")); + try (RIFFWriter writer = new RIFFWriter(name, "sfbk")) { + writeSoundbank(writer); + } } public void save(File file) throws IOException { - writeSoundbank(new RIFFWriter(file, "sfbk")); + try (RIFFWriter writer = new RIFFWriter(file, "sfbk")) { + writeSoundbank(writer); + } } public void save(OutputStream out) throws IOException { - writeSoundbank(new RIFFWriter(out, "sfbk")); + try (RIFFWriter writer = new RIFFWriter(out, "sfbk")) { + writeSoundbank(writer); + } } private void writeSoundbank(RIFFWriter writer) throws IOException { writeInfo(writer.writeList("INFO")); writeSdtaChunk(writer.writeList("sdta")); writePdtaChunk(writer.writeList("pdta")); - writer.close(); } private void writeInfoStringChunk(RIFFWriter writer, String name, diff --git a/jdk/src/share/classes/com/sun/media/sound/SoftSynthesizer.java b/jdk/src/share/classes/com/sun/media/sound/SoftSynthesizer.java index 8464e5aef67..8b91da05d07 100644 --- a/jdk/src/share/classes/com/sun/media/sound/SoftSynthesizer.java +++ b/jdk/src/share/classes/com/sun/media/sound/SoftSynthesizer.java @@ -787,8 +787,12 @@ public InputStream run() { if (out != null) { try { ((SF2Soundbank) defaultSoundBank).save(out); - out.close(); } catch (final IOException ignored) { + } finally { + try { + out.close(); + } catch (final IOException ignored) { + } } } } diff --git a/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileReader.java b/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileReader.java index 20ebe4f060a..f7abc15c5f9 100644 --- a/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileReader.java +++ b/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -145,34 +145,27 @@ private MidiFileFormat getMidiFileFormatFromStream(InputStream stream, public MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException { - InputStream urlStream = url.openStream(); // throws IOException - BufferedInputStream bis = new BufferedInputStream( urlStream, bisBufferSize ); - MidiFileFormat fileFormat = null; - try { - fileFormat = getMidiFileFormat( bis ); // throws InvalidMidiDataException - } finally { - bis.close(); + try (InputStream urlStream = url.openStream(); // throws IOException + BufferedInputStream bis = new BufferedInputStream(urlStream, bisBufferSize)) + { + MidiFileFormat fileFormat = getMidiFileFormat(bis); // throws InvalidMidiDataException + return fileFormat; } - return fileFormat; } public MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException { - FileInputStream fis = new FileInputStream(file); // throws IOException - BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize); - - // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length - long length = file.length(); - if (length > Integer.MAX_VALUE) { - length = MidiFileFormat.UNKNOWN_LENGTH; - } - MidiFileFormat fileFormat = null; - try { - fileFormat = getMidiFileFormatFromStream(bis, (int) length, null); - } finally { - bis.close(); + try (FileInputStream fis = new FileInputStream(file); // throws IOException + BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize)) + { + // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length + long length = file.length(); + if (length > Integer.MAX_VALUE) { + length = MidiFileFormat.UNKNOWN_LENGTH; + } + MidiFileFormat fileFormat = getMidiFileFormatFromStream(bis, (int) length, null); + return fileFormat; } - return fileFormat; } @@ -204,28 +197,22 @@ public Sequence getSequence(InputStream stream) throws InvalidMidiDataException, public Sequence getSequence(URL url) throws InvalidMidiDataException, IOException { - InputStream is = url.openStream(); // throws IOException - is = new BufferedInputStream(is, bisBufferSize); - Sequence seq = null; - try { - seq = getSequence(is); - } finally { - is.close(); + try (InputStream is = url.openStream(); // throws IOException + BufferedInputStream bis = new BufferedInputStream(is, bisBufferSize)) + { + Sequence seq = getSequence(bis); + return seq; } - return seq; } public Sequence getSequence(File file) throws InvalidMidiDataException, IOException { - InputStream is = new FileInputStream(file); // throws IOException - is = new BufferedInputStream(is, bisBufferSize); - Sequence seq = null; - try { - seq = getSequence(is); - } finally { - is.close(); + try (InputStream is = new FileInputStream(file); // throws IOException + BufferedInputStream bis = new BufferedInputStream(is, bisBufferSize)) + { + Sequence seq = getSequence(bis); + return seq; } - return seq; } } diff --git a/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileWriter.java b/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileWriter.java index 97e0dc2928a..d8abeccaee0 100644 --- a/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileWriter.java +++ b/jdk/src/share/classes/com/sun/media/sound/StandardMidiFileWriter.java @@ -149,10 +149,10 @@ public int write(Sequence in, int type, OutputStream out) throws IOException { } public int write(Sequence in, int type, File out) throws IOException { - FileOutputStream fos = new FileOutputStream(out); // throws IOException - int bytesWritten = write( in, type, fos ); - fos.close(); - return bytesWritten; + try (FileOutputStream fos = new FileOutputStream(out)) { // throws IOException + int bytesWritten = write(in, type, fos); + return bytesWritten; + } } //================================================================================= diff --git a/jdk/src/share/classes/java/awt/Toolkit.java b/jdk/src/share/classes/java/awt/Toolkit.java index 91c28f4e28f..c37749737c0 100644 --- a/jdk/src/share/classes/java/awt/Toolkit.java +++ b/jdk/src/share/classes/java/awt/Toolkit.java @@ -713,12 +713,10 @@ public String run() { File propsFile = new File( System.getProperty("user.home") + sep + ".accessibility.properties"); - FileInputStream in = - new FileInputStream(propsFile); - - // Inputstream has been buffered in Properties class - properties.load(in); - in.close(); + try (FileInputStream in = new FileInputStream(propsFile)) { + // Inputstream has been buffered in Properties class + properties.load(in); + } } catch (Exception e) { // Per-user accessibility properties file does not exist } @@ -731,12 +729,10 @@ public String run() { File propsFile = new File( System.getProperty("java.home") + sep + "lib" + sep + "accessibility.properties"); - FileInputStream in = - new FileInputStream(propsFile); - - // Inputstream has been buffered in Properties class - properties.load(in); - in.close(); + try (FileInputStream in = new FileInputStream(propsFile)) { + // Inputstream has been buffered in Properties class + properties.load(in); + } } catch (Exception e) { // System-wide accessibility properties file does // not exist; diff --git a/jdk/src/share/classes/java/beans/Beans.java b/jdk/src/share/classes/java/beans/Beans.java index c781a98597e..f121ae52cbc 100644 --- a/jdk/src/share/classes/java/beans/Beans.java +++ b/jdk/src/share/classes/java/beans/Beans.java @@ -191,13 +191,11 @@ public static Object instantiate(ClassLoader cls, String beanName, BeanContext b serialized = true; oins.close(); } catch (IOException ex) { - ins.close(); // Drop through and try opening the class. But remember // the exception in case we can't find the class either. serex = ex; - } catch (ClassNotFoundException ex) { + } finally { ins.close(); - throw ex; } } diff --git a/jdk/src/share/classes/javax/imageio/ImageIO.java b/jdk/src/share/classes/javax/imageio/ImageIO.java index 13d623f2b7a..9552e0e34c2 100644 --- a/jdk/src/share/classes/javax/imageio/ImageIO.java +++ b/jdk/src/share/classes/javax/imageio/ImageIO.java @@ -39,8 +39,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.Iterator; -import java.util.NoSuchElementException; -import java.util.Set; import javax.imageio.spi.IIORegistry; import javax.imageio.spi.ImageReaderSpi; import javax.imageio.spi.ImageReaderWriterSpi; @@ -1394,7 +1392,7 @@ public static BufferedImage read(URL input) throws IOException { throw new IllegalArgumentException("input == null!"); } - InputStream istream = null; + InputStream istream; try { istream = input.openStream(); } catch (IOException e) { diff --git a/jdk/src/share/classes/javax/swing/JEditorPane.java b/jdk/src/share/classes/javax/swing/JEditorPane.java index 97075236668..27b15f56819 100644 --- a/jdk/src/share/classes/javax/swing/JEditorPane.java +++ b/jdk/src/share/classes/javax/swing/JEditorPane.java @@ -794,16 +794,12 @@ private Object getPostData() { private void handlePostData(HttpURLConnection conn, Object postData) throws IOException { conn.setDoOutput(true); - DataOutputStream os = null; - try { - conn.setRequestProperty("Content-Type", - "application/x-www-form-urlencoded"); - os = new DataOutputStream(conn.getOutputStream()); - os.writeBytes((String) postData); - } finally { - if (os != null) { - os.close(); - } + conn.setRequestProperty("Content-Type", + "application/x-www-form-urlencoded"); + try (OutputStream os = conn.getOutputStream(); + DataOutputStream dos = new DataOutputStream(os)) + { + dos.writeBytes((String)postData); } } diff --git a/jdk/src/share/classes/javax/swing/UIManager.java b/jdk/src/share/classes/javax/swing/UIManager.java index aca0f18458c..4d32ae08f85 100644 --- a/jdk/src/share/classes/javax/swing/UIManager.java +++ b/jdk/src/share/classes/javax/swing/UIManager.java @@ -57,7 +57,6 @@ import sun.awt.OSInfo; import sun.security.action.GetPropertyAction; import sun.swing.SwingUtilities2; -import java.lang.reflect.Method; import java.util.HashMap; import sun.awt.AppContext; import sun.awt.AWTAccessor; @@ -1239,9 +1238,9 @@ public Object run() { if (file.exists()) { // InputStream has been buffered in Properties // class - FileInputStream ins = new FileInputStream(file); - props.load(ins); - ins.close(); + try (FileInputStream ins = new FileInputStream(file)) { + props.load(ins); + } } } catch (Exception e) { diff --git a/jdk/src/share/classes/javax/swing/text/html/HTMLEditorKit.java b/jdk/src/share/classes/javax/swing/text/html/HTMLEditorKit.java index c49a1edad6e..07e43d7e560 100644 --- a/jdk/src/share/classes/javax/swing/text/html/HTMLEditorKit.java +++ b/jdk/src/share/classes/javax/swing/text/html/HTMLEditorKit.java @@ -391,12 +391,11 @@ public StyleSheet getStyleSheet() { if (defaultStyles == null) { defaultStyles = new StyleSheet(); appContext.put(DEFAULT_STYLES_KEY, defaultStyles); - try { - InputStream is = HTMLEditorKit.getResourceAsStream(DEFAULT_CSS); - Reader r = new BufferedReader( - new InputStreamReader(is, "ISO-8859-1")); + try (InputStream is = HTMLEditorKit.getResourceAsStream(DEFAULT_CSS); + InputStreamReader isr = new InputStreamReader(is, "ISO-8859-1"); + Reader r = new BufferedReader(isr)) + { defaultStyles.loadRules(r, null); - r.close(); } catch (Throwable e) { // on error we simply have no styles... the html // will look mighty wrong but still function. diff --git a/jdk/src/share/classes/javax/swing/text/html/StyleSheet.java b/jdk/src/share/classes/javax/swing/text/html/StyleSheet.java index 59a697587e6..fcb71d4362c 100644 --- a/jdk/src/share/classes/javax/swing/text/html/StyleSheet.java +++ b/jdk/src/share/classes/javax/swing/text/html/StyleSheet.java @@ -460,15 +460,12 @@ public StyleSheet[] getStyleSheets() { * @since 1.3 */ public void importStyleSheet(URL url) { - try { - InputStream is; - - is = url.openStream(); - Reader r = new BufferedReader(new InputStreamReader(is)); + try (InputStream is = url.openStream(); + InputStreamReader isr = new InputStreamReader(is); + Reader r = new BufferedReader(isr)) + { CssParser parser = new CssParser(); parser.parse(url, r, false, true); - r.close(); - is.close(); } catch (Throwable e) { // on error we simply have no styles... the html // will look mighty wrong but still function. diff --git a/jdk/src/share/classes/sun/awt/DebugSettings.java b/jdk/src/share/classes/sun/awt/DebugSettings.java index aaab07eeb9b..89ffeade5e9 100644 --- a/jdk/src/share/classes/sun/awt/DebugSettings.java +++ b/jdk/src/share/classes/sun/awt/DebugSettings.java @@ -178,9 +178,9 @@ private void loadFileProperties() { File propFile = new File(propPath); try { println("Reading debug settings from '" + propFile.getCanonicalPath() + "'..."); - FileInputStream fin = new FileInputStream(propFile); - props.load(fin); - fin.close(); + try (FileInputStream fin = new FileInputStream(propFile)) { + props.load(fin); + } } catch ( FileNotFoundException fne ) { println("Did not find settings file."); } catch ( IOException ioe ) { diff --git a/jdk/src/share/classes/sun/awt/FontConfiguration.java b/jdk/src/share/classes/sun/awt/FontConfiguration.java index 93e38e06e2e..66dc8f67f0c 100644 --- a/jdk/src/share/classes/sun/awt/FontConfiguration.java +++ b/jdk/src/share/classes/sun/awt/FontConfiguration.java @@ -48,7 +48,6 @@ import java.util.Vector; import sun.font.CompositeFontDescriptor; import sun.font.SunFontManager; -import sun.font.FontManagerFactory; import sun.font.FontUtilities; import sun.util.logging.PlatformLogger; @@ -199,14 +198,12 @@ private void readFontConfigFile(File f) { getInstalledFallbackFonts(javaLib); if (f != null) { - try { - FileInputStream in = new FileInputStream(f.getPath()); + try (FileInputStream in = new FileInputStream(f.getPath())) { if (isProperties) { loadProperties(in); } else { loadBinary(in); } - in.close(); if (FontUtilities.debugFonts()) { logger.config("Read logical font configuration from " + f); } diff --git a/jdk/src/share/classes/sun/print/PSPrinterJob.java b/jdk/src/share/classes/sun/print/PSPrinterJob.java index c286400ce5b..ec8343946fd 100644 --- a/jdk/src/share/classes/sun/print/PSPrinterJob.java +++ b/jdk/src/share/classes/sun/print/PSPrinterJob.java @@ -397,11 +397,12 @@ private static Properties initProps() { } // Load property file - InputStream in = - new BufferedInputStream(new FileInputStream(f.getPath())); Properties props = new Properties(); - props.load(in); - in.close(); + try (FileInputStream is = new FileInputStream(f.getPath()); + BufferedInputStream bis = new BufferedInputStream(is)) + { + props.load(bis); + } return props; } catch (Exception e){ return (Properties)null; diff --git a/jdk/src/solaris/classes/sun/awt/motif/MFontConfiguration.java b/jdk/src/solaris/classes/sun/awt/motif/MFontConfiguration.java index 6c28d201779..036e78cbfd8 100644 --- a/jdk/src/solaris/classes/sun/awt/motif/MFontConfiguration.java +++ b/jdk/src/solaris/classes/sun/awt/motif/MFontConfiguration.java @@ -177,7 +177,9 @@ protected void setOsNameAndVersion(){ * For Ubuntu the ID is "Ubuntu". */ Properties props = new Properties(); - props.load(new FileInputStream(f)); + try (FileInputStream fis = new FileInputStream(f)) { + props.load(fis); + } osName = props.getProperty("DISTRIB_ID"); osVersion = props.getProperty("DISTRIB_RELEASE"); } diff --git a/jdk/src/solaris/classes/sun/font/FcFontConfiguration.java b/jdk/src/solaris/classes/sun/font/FcFontConfiguration.java index ecb813de507..44983445bb4 100644 --- a/jdk/src/solaris/classes/sun/font/FcFontConfiguration.java +++ b/jdk/src/solaris/classes/sun/font/FcFontConfiguration.java @@ -317,7 +317,9 @@ protected void setOsNameAndVersion() { * For Ubuntu the ID is "Ubuntu". */ Properties props = new Properties(); - props.load(new FileInputStream(f)); + try (FileInputStream fis = new FileInputStream(f)) { + props.load(fis); + } osName = props.getProperty("DISTRIB_ID"); osVersion = props.getProperty("DISTRIB_RELEASE"); } else if ((f = new File("/etc/redhat-release")).canRead()) { @@ -400,10 +402,9 @@ private void writeFcInfo() { File dir = fcInfoFile.getParentFile(); dir.mkdirs(); File tempFile = Files.createTempFile(dir.toPath(), "fcinfo", null).toFile(); - FileOutputStream fos = new FileOutputStream(tempFile); - props.store(fos, - "JDK Font Configuration Generated File: *Do Not Edit*"); - fos.close(); + try (FileOutputStream fos = new FileOutputStream(tempFile)) { + props.store(fos, "JDK Font Configuration Generated File: *Do Not Edit*"); + } boolean renamed = tempFile.renameTo(fcInfoFile); if (!renamed && FontUtilities.debugFonts()) { System.out.println("rename failed");