diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 1fb72fc46..a74bd499d 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -39,6 +39,7 @@ import cc.arduino.view.Event; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; +import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.lang3.StringUtils; import processing.app.debug.TargetBoard; import processing.app.debug.TargetPackage; @@ -2517,9 +2518,7 @@ public class Base { } return buffer; } finally { - if (input != null) { - input.close(); - } + IOUtils.closeQuietly(input); } } @@ -2567,12 +2566,8 @@ public class Base { } to.flush(); } finally { - if (from != null) { - from.close(); // ?? - } - if (to != null) { - to.close(); // ?? - } + IOUtils.closeQuietly(from); + IOUtils.closeQuietly(to); } targetFile.setLastModified(sourceFile.lastModified()); diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 77f3eab22..954c539ba 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -28,6 +28,7 @@ import cc.arduino.view.StubMenuListener; import com.google.common.base.Predicate; import com.jcraft.jsch.JSchException; import jssc.SerialPortException; +import org.apache.commons.compress.utils.IOUtils; import processing.app.debug.*; import processing.app.forms.PasswordAuthorizationDialog; import processing.app.helpers.OSUtils; @@ -965,11 +966,7 @@ public class Editor extends JFrame implements RunnerListener { //System.err.println("Ignoring " + filename + " (" + e.getMessage() + ")"); e.printStackTrace(); } finally { - if (zipFile != null) - try { - zipFile.close(); - } catch (IOException e) { - } + IOUtils.closeQuietly(zipFile); } return null; } diff --git a/app/src/processing/app/EditorConsoleStream.java b/app/src/processing/app/EditorConsoleStream.java index 6ad0a336a..6b8925bc3 100644 --- a/app/src/processing/app/EditorConsoleStream.java +++ b/app/src/processing/app/EditorConsoleStream.java @@ -1,6 +1,7 @@ package processing.app; import cc.arduino.files.DeleteFilesOnShutdown; +import org.apache.commons.compress.utils.IOUtils; import static processing.app.I18n._; @@ -82,17 +83,13 @@ class EditorConsoleStream extends OutputStream { System.setErr(systemErr); // close the PrintStream - consoleOut.close(); - consoleErr.close(); + IOUtils.closeQuietly(consoleOut); + IOUtils.closeQuietly(consoleErr); // also have to close the original FileOutputStream // otherwise it won't be shut down completely - try { - stdoutFile.close(); - stderrFile.close(); - } catch (IOException e) { - e.printStackTrace(); - } + IOUtils.closeQuietly(stdoutFile); + IOUtils.closeQuietly(stderrFile); outFile.delete(); errFile.delete(); @@ -149,4 +146,4 @@ class EditorConsoleStream extends OutputStream { currentConsole = console; } -} \ No newline at end of file +} diff --git a/app/src/processing/app/UpdateCheck.java b/app/src/processing/app/UpdateCheck.java index b3b48dfef..b13e7040f 100644 --- a/app/src/processing/app/UpdateCheck.java +++ b/app/src/processing/app/UpdateCheck.java @@ -22,6 +22,7 @@ package processing.app; +import org.apache.commons.compress.utils.IOUtils; import processing.app.legacy.PApplet; import javax.swing.*; @@ -133,9 +134,7 @@ public class UpdateCheck implements Runnable { reader = new BufferedReader(new InputStreamReader(url.openStream())); return Integer.parseInt(reader.readLine()); } finally { - if (reader != null) { - reader.close(); - } + IOUtils.closeQuietly(reader); } } } diff --git a/app/src/processing/app/syntax/PdeKeywords.java b/app/src/processing/app/syntax/PdeKeywords.java index 3b5a575ce..8740b0675 100644 --- a/app/src/processing/app/syntax/PdeKeywords.java +++ b/app/src/processing/app/syntax/PdeKeywords.java @@ -25,6 +25,7 @@ package processing.app.syntax; import cc.arduino.contributions.libraries.ContributedLibrary; +import org.apache.commons.compress.utils.IOUtils; import org.fife.ui.rsyntaxtextarea.TokenMap; import org.fife.ui.rsyntaxtextarea.TokenTypes; import processing.app.Base; @@ -126,9 +127,7 @@ public class PdeKeywords { fillMissingTokenType(); } finally { - if (reader != null) { - reader.close(); - } + IOUtils.closeQuietly(reader); } } diff --git a/app/src/processing/app/syntax/SketchTextArea.java b/app/src/processing/app/syntax/SketchTextArea.java index 580fe99f7..f31a45ab3 100644 --- a/app/src/processing/app/syntax/SketchTextArea.java +++ b/app/src/processing/app/syntax/SketchTextArea.java @@ -30,6 +30,7 @@ package processing.app.syntax; +import org.apache.commons.compress.utils.IOUtils; import org.fife.ui.rsyntaxtextarea.*; import org.fife.ui.rsyntaxtextarea.Theme; import org.fife.ui.rsyntaxtextarea.Token; @@ -102,9 +103,7 @@ public class SketchTextArea extends RSyntaxTextArea { Theme theme = Theme.load(defaultXmlInputStream); theme.apply(this); } finally { - if (defaultXmlInputStream != null) { - defaultXmlInputStream.close(); - } + IOUtils.closeQuietly(defaultXmlInputStream); } setForeground(processing.app.Theme.getColor("editor.fgcolor")); diff --git a/app/src/processing/app/tools/Archiver.java b/app/src/processing/app/tools/Archiver.java index 81a60e41b..664d4dff6 100644 --- a/app/src/processing/app/tools/Archiver.java +++ b/app/src/processing/app/tools/Archiver.java @@ -23,6 +23,7 @@ package processing.app.tools; +import org.apache.commons.compress.utils.IOUtils; import processing.app.Base; import processing.app.Editor; import processing.app.Sketch; @@ -124,22 +125,21 @@ public class Archiver implements Tool { if (filename != null) { newbie = new File(directory, filename); + ZipOutputStream zos = null; try { //System.out.println(newbie); - FileOutputStream zipOutputFile = new FileOutputStream(newbie); - ZipOutputStream zos = new ZipOutputStream(zipOutputFile); + zos = new ZipOutputStream(new FileOutputStream(newbie)); // recursively fill the zip file buildZip(location, name, zos); // close up the jar file zos.flush(); - zos.close(); - editor.statusNotice("Created archive " + newbie.getName() + "."); - } catch (IOException e) { e.printStackTrace(); + } finally { + IOUtils.closeQuietly(zos); } } else { editor.statusNotice(_("Archive sketch canceled.")); diff --git a/app/src/processing/app/tools/FixEncoding.java b/app/src/processing/app/tools/FixEncoding.java index 5bfb52462..926fed6e9 100644 --- a/app/src/processing/app/tools/FixEncoding.java +++ b/app/src/processing/app/tools/FixEncoding.java @@ -29,6 +29,7 @@ import java.io.IOException; import javax.swing.JOptionPane; +import org.apache.commons.compress.utils.IOUtils; import processing.app.*; import static processing.app.I18n._; @@ -83,16 +84,19 @@ public class FixEncoding implements Tool { protected String loadWithLocalEncoding(File file) throws IOException { // FileReader uses the default encoding, which is what we want. - FileReader fr = new FileReader(file); - BufferedReader reader = new BufferedReader(fr); + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader(file)); - StringBuffer buffer = new StringBuffer(); - String line = null; - while ((line = reader.readLine()) != null) { - buffer.append(line); - buffer.append('\n'); + StringBuffer buffer = new StringBuffer(); + String line; + while ((line = reader.readLine()) != null) { + buffer.append(line); + buffer.append('\n'); + } + return buffer.toString(); + } finally { + IOUtils.closeQuietly(reader); } - reader.close(); - return buffer.toString(); } } diff --git a/app/src/processing/app/tools/ZipDeflater.java b/app/src/processing/app/tools/ZipDeflater.java index 55f0c0c8b..1425d8802 100644 --- a/app/src/processing/app/tools/ZipDeflater.java +++ b/app/src/processing/app/tools/ZipDeflater.java @@ -10,6 +10,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; +import org.apache.commons.compress.utils.IOUtils; import processing.app.helpers.FileUtils; public class ZipDeflater { @@ -54,12 +55,8 @@ public class ZipDeflater { fos.write(buffer, 0, len); } } finally { - if (fos != null) { - fos.close(); - } - if (zipInputStream != null) { - zipInputStream.close(); - } + IOUtils.closeQuietly(fos); + IOUtils.closeQuietly(zipInputStream); } } } diff --git a/app/test/processing/app/I18NTest.java b/app/test/processing/app/I18NTest.java index 1df455f91..219f6f7ab 100644 --- a/app/test/processing/app/I18NTest.java +++ b/app/test/processing/app/I18NTest.java @@ -29,6 +29,7 @@ package processing.app; +import org.apache.commons.compress.utils.IOUtils; import org.junit.Ignore; import org.junit.Test; @@ -63,9 +64,7 @@ public class I18NTest { is = new FileInputStream(file); properties.load(is); } finally { - if (is != null) { - is.close(); - } + IOUtils.closeQuietly(is); } return properties; } diff --git a/arduino-core/src/cc/arduino/contributions/GPGDetachedSignatureVerifier.java b/arduino-core/src/cc/arduino/contributions/GPGDetachedSignatureVerifier.java index 2b23c3387..a3ed88148 100644 --- a/arduino-core/src/cc/arduino/contributions/GPGDetachedSignatureVerifier.java +++ b/arduino-core/src/cc/arduino/contributions/GPGDetachedSignatureVerifier.java @@ -77,12 +77,8 @@ public class GPGDetachedSignatureVerifier { return pgpSignature.verify(); } finally { - if (signatureInputStream != null) { - signatureInputStream.close(); - } - if (signedFileInputStream != null) { - signedFileInputStream.close(); - } + IOUtils.closeQuietly(signatureInputStream); + IOUtils.closeQuietly(signedFileInputStream); } } @@ -92,9 +88,7 @@ public class GPGDetachedSignatureVerifier { keyIn = new BufferedInputStream(new FileInputStream(file)); return readPublicKey(keyIn, keyId); } finally { - if (keyIn != null) { - keyIn.close(); - } + IOUtils.closeQuietly(keyIn); } } diff --git a/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java b/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java index ba2f6f64a..b66d41c5e 100644 --- a/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java +++ b/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java @@ -37,6 +37,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.mrbean.MrBeanModule; import com.google.common.base.Function; import com.google.common.collect.FluentIterable; +import org.apache.commons.compress.utils.IOUtils; import processing.app.BaseNoGui; import processing.app.I18n; import processing.app.helpers.FileUtils; @@ -96,9 +97,7 @@ public class LibrariesIndexer { } } } finally { - if (indexIn != null) { - indexIn.close(); - } + IOUtils.closeQuietly(indexIn); } } diff --git a/arduino-core/src/cc/arduino/contributions/packages/ContributionsIndexer.java b/arduino-core/src/cc/arduino/contributions/packages/ContributionsIndexer.java index b5180a27a..1e5845214 100644 --- a/arduino-core/src/cc/arduino/contributions/packages/ContributionsIndexer.java +++ b/arduino-core/src/cc/arduino/contributions/packages/ContributionsIndexer.java @@ -44,6 +44,7 @@ import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.Iterables; import com.google.common.collect.Multimaps; +import org.apache.commons.compress.utils.IOUtils; import processing.app.BaseNoGui; import processing.app.debug.TargetPackage; import processing.app.debug.TargetPlatform; @@ -172,9 +173,7 @@ public class ContributionsIndexer { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return mapper.readValue(inputStream, ContributionsIndex.class); } finally { - if (inputStream != null) { - inputStream.close(); - } + IOUtils.closeQuietly(inputStream); } } diff --git a/arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java b/arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java index b7ede72c2..48d9cdd4b 100644 --- a/arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java +++ b/arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java @@ -33,6 +33,7 @@ import cc.arduino.packages.BoardPort; import cc.arduino.packages.Discovery; import cc.arduino.packages.discoverers.network.BoardReachabilityFilter; import cc.arduino.packages.discoverers.network.NetworkChecker; +import org.apache.commons.compress.utils.IOUtils; import processing.app.BaseNoGui; import processing.app.helpers.PreferencesMap; import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter; @@ -199,12 +200,6 @@ public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino. @Override public void inetAddressRemoved(InetAddress address) { JmDNS jmDNS = mappedJmDNSs.remove(address); - if (jmDNS != null) { - try { - jmDNS.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + IOUtils.closeQuietly(jmDNS); } } diff --git a/arduino-core/src/cc/arduino/packages/ssh/SCP.java b/arduino-core/src/cc/arduino/packages/ssh/SCP.java index 458e1b8d5..efeb761e0 100644 --- a/arduino-core/src/cc/arduino/packages/ssh/SCP.java +++ b/arduino-core/src/cc/arduino/packages/ssh/SCP.java @@ -32,6 +32,7 @@ package cc.arduino.packages.ssh; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.Session; +import org.apache.commons.compress.utils.IOUtils; import java.io.*; @@ -61,12 +62,8 @@ public class SCP extends SSH { } public void close() throws IOException { - if (out != null) { - out.close(); - } - if (in != null) { - in.close(); - } + IOUtils.closeQuietly(out); + IOUtils.closeQuietly(in); if (channel != null) { channel.disconnect(); } @@ -118,9 +115,7 @@ public class SCP extends SSH { buf[0] = 0; out.write(buf, 0, 1); } finally { - if (fis != null) { - fis.close(); - } + IOUtils.closeQuietly(fis); } ensureAcknowledged(); diff --git a/arduino-core/src/cc/arduino/packages/ssh/SSH.java b/arduino-core/src/cc/arduino/packages/ssh/SSH.java index 0bb6f8a34..1e20adebb 100644 --- a/arduino-core/src/cc/arduino/packages/ssh/SSH.java +++ b/arduino-core/src/cc/arduino/packages/ssh/SSH.java @@ -33,6 +33,7 @@ import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; +import org.apache.commons.compress.utils.IOUtils; import java.io.IOException; import java.io.InputStream; @@ -70,12 +71,8 @@ public class SSH { return exitCode == 0; } finally { - if (stdout != null) { - stdout.close(); - } - if (stderr != null) { - stderr.close(); - } + IOUtils.closeQuietly(stdout); + IOUtils.closeQuietly(stderr); if (channel != null) { channel.disconnect(); } diff --git a/arduino-core/src/cc/arduino/utils/ArchiveExtractor.java b/arduino-core/src/cc/arduino/utils/ArchiveExtractor.java index fe68fa870..2b13dea2b 100644 --- a/arduino-core/src/cc/arduino/utils/ArchiveExtractor.java +++ b/arduino-core/src/cc/arduino/utils/ArchiveExtractor.java @@ -35,6 +35,7 @@ import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; +import org.apache.commons.compress.utils.IOUtils; import processing.app.I18n; import processing.app.Platform; @@ -258,9 +259,7 @@ public class ArchiveExtractor { } } finally { - if (in != null) { - in.close(); - } + IOUtils.closeQuietly(in); } // Set folders timestamps @@ -294,9 +293,7 @@ public class ArchiveExtractor { size -= length; } } finally { - if (fos != null) { - fos.close(); - } + IOUtils.closeQuietly(fos); } } diff --git a/arduino-core/src/cc/arduino/utils/FileHash.java b/arduino-core/src/cc/arduino/utils/FileHash.java index 0ce8afd4c..8c658eb37 100644 --- a/arduino-core/src/cc/arduino/utils/FileHash.java +++ b/arduino-core/src/cc/arduino/utils/FileHash.java @@ -28,6 +28,8 @@ */ package cc.arduino.utils; +import org.apache.commons.compress.utils.IOUtils; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -68,9 +70,7 @@ public class FileHash { } return algorithm + ":" + res; } finally { - if (in != null) { - in.close(); - } + IOUtils.closeQuietly(in); } } } diff --git a/arduino-core/src/cc/arduino/utils/network/FileDownloader.java b/arduino-core/src/cc/arduino/utils/network/FileDownloader.java index 358de9759..83358a32b 100644 --- a/arduino-core/src/cc/arduino/utils/network/FileDownloader.java +++ b/arduino-core/src/cc/arduino/utils/network/FileDownloader.java @@ -29,6 +29,7 @@ package cc.arduino.utils.network; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.compress.utils.IOUtils; import processing.app.PreferencesData; import java.io.File; @@ -221,22 +222,10 @@ public class FileDownloader extends Observable { setError(e); } finally { - if (file != null) { - try { - file.close(); - } catch (Exception e) { - //ignore - } - } + IOUtils.closeQuietly(file); synchronized (this) { - if (stream != null) { - try { - stream.close(); - } catch (Exception e) { - //ignore - } - } + IOUtils.closeQuietly(stream); } } } diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index cb039d8e3..7005b7005 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -8,6 +8,7 @@ import cc.arduino.files.DeleteFilesOnShutdown; import cc.arduino.packages.DiscoveryManager; import cc.arduino.packages.Uploader; import com.fasterxml.jackson.core.JsonProcessingException; +import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.logging.impl.LogFactoryImpl; import org.apache.commons.logging.impl.NoOpLog; import processing.app.debug.Compiler; @@ -315,14 +316,15 @@ public class BaseNoGui { static public File getSketchbookLibrariesFolder() { File libdir = new File(getSketchbookFolder(), "libraries"); if (!libdir.exists()) { + FileWriter freadme = null; try { libdir.mkdirs(); - File readme = new File(libdir, "readme.txt"); - FileWriter freadme = new FileWriter(readme); + freadme = new FileWriter(new File(libdir, "readme.txt")); freadme.write(_("For information on installing libraries, see: " + "http://www.arduino.cc/en/Guide/Libraries\n")); - freadme.close(); } catch (Exception e) { + } finally { + IOUtils.closeQuietly(freadme); } } return libdir; @@ -595,11 +597,8 @@ public class BaseNoGui { try { out = new FileOutputStream(indexFile); out.write("{ \"packages\" : [ ] }".getBytes()); - out.close(); } finally { - if (out != null) { - out.close(); - } + IOUtils.closeQuietly(out); } } @@ -643,9 +642,7 @@ public class BaseNoGui { } catch (IOException e) { e.printStackTrace(); } finally { - if (out != null) { - out.close(); - } + IOUtils.closeQuietly(out); } } } diff --git a/arduino-core/src/processing/app/PreferencesData.java b/arduino-core/src/processing/app/PreferencesData.java index 7733b08e4..c3a0f9069 100644 --- a/arduino-core/src/processing/app/PreferencesData.java +++ b/arduino-core/src/processing/app/PreferencesData.java @@ -13,6 +13,7 @@ import java.util.Arrays; import java.util.Iterator; import java.util.MissingResourceException; +import org.apache.commons.compress.utils.IOUtils; import processing.app.helpers.PreferencesHelper; import processing.app.helpers.PreferencesMap; import processing.app.legacy.PApplet; @@ -124,9 +125,7 @@ public class PreferencesData { writer.flush(); } finally { - if (writer != null) { - writer.close(); - } + IOUtils.closeQuietly(writer); } try { diff --git a/arduino-core/src/processing/app/debug/Compiler.java b/arduino-core/src/processing/app/debug/Compiler.java index d6ecbcfd6..944a09ba2 100644 --- a/arduino-core/src/processing/app/debug/Compiler.java +++ b/arduino-core/src/processing/app/debug/Compiler.java @@ -39,6 +39,7 @@ import cc.arduino.packages.BoardPort; import cc.arduino.packages.Uploader; import cc.arduino.packages.UploaderFactory; +import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.ExecuteStreamHandler; @@ -100,12 +101,14 @@ public class Compiler implements MessageConsumer { compiler.cleanup(prefsChanged, tempBuildFolder); if (prefsChanged) { + PrintWriter out = null; try { - PrintWriter out = new PrintWriter(buildPrefsFile); + out = new PrintWriter(buildPrefsFile); out.print(newBuildPrefs); - out.close(); } catch (IOException e) { System.err.println(_("Could not write build preferences file")); + } finally { + IOUtils.closeQuietly(out); } } @@ -612,6 +615,7 @@ public class Compiler implements MessageConsumer { private boolean isAlreadyCompiled(File src, File obj, File dep, Map prefs) { boolean ret=true; + BufferedReader reader = null; try { //System.out.println("\n isAlreadyCompiled: begin checks: " + obj.getPath()); if (!obj.exists()) return false; // object file (.o) does not exist @@ -620,7 +624,7 @@ public class Compiler implements MessageConsumer { long obj_modified = obj.lastModified(); if (src_modified >= obj_modified) return false; // source modified since object compiled if (src_modified >= dep.lastModified()) return false; // src modified since dep compiled - BufferedReader reader = new BufferedReader(new FileReader(dep.getPath())); + reader = new BufferedReader(new FileReader(dep.getPath())); String line; boolean need_obj_parse = true; while ((line = reader.readLine()) != null) { @@ -664,9 +668,10 @@ public class Compiler implements MessageConsumer { //System.out.println(" isAlreadyCompiled: prerequisite ok"); } } - reader.close(); } catch (Exception e) { return false; // any error reading dep file = recompile it + } finally { + IOUtils.closeQuietly(reader); } if (ret && verbose) { System.out.println(I18n.format(_("Using previously compiled file: {0}"), obj.getPath())); @@ -1267,13 +1272,7 @@ public class Compiler implements MessageConsumer { ex.printStackTrace(); throw new RunnerException(ex.toString()); } finally { - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException e) { - //noop - } - } + IOUtils.closeQuietly(outputStream); } // grab the imports from the code just preproc'd diff --git a/arduino-core/src/processing/app/helpers/FileUtils.java b/arduino-core/src/processing/app/helpers/FileUtils.java index 186141cf5..4f99f48e2 100644 --- a/arduino-core/src/processing/app/helpers/FileUtils.java +++ b/arduino-core/src/processing/app/helpers/FileUtils.java @@ -1,5 +1,7 @@ package processing.app.helpers; +import org.apache.commons.compress.utils.IOUtils; + import java.io.*; import java.util.ArrayList; import java.util.Arrays; @@ -49,12 +51,8 @@ public class FileUtils { fos.write(buf, 0, readBytes); } } finally { - if (fis != null) { - fis.close(); - } - if (fos != null) { - fos.close(); - } + IOUtils.closeQuietly(fis); + IOUtils.closeQuietly(fos); } } @@ -185,13 +183,7 @@ public class FileUtils { } return sb.toString(); } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - // noop - } - } + IOUtils.closeQuietly(reader); } } diff --git a/arduino-core/src/processing/app/helpers/NetUtils.java b/arduino-core/src/processing/app/helpers/NetUtils.java index 17dc52c86..e482e5247 100644 --- a/arduino-core/src/processing/app/helpers/NetUtils.java +++ b/arduino-core/src/processing/app/helpers/NetUtils.java @@ -1,5 +1,7 @@ package processing.app.helpers; +import org.apache.commons.compress.utils.IOUtils; + import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -43,13 +45,7 @@ public abstract class NetUtils { } catch (IOException e) { return false; } finally { - if (socket != null) { - try { - socket.close(); - } catch (IOException e) { - // noop - } - } + IOUtils.closeQuietly(socket); } } } diff --git a/arduino-core/src/processing/app/helpers/PreferencesMap.java b/arduino-core/src/processing/app/helpers/PreferencesMap.java index 185e1bee4..2e3bf83b3 100644 --- a/arduino-core/src/processing/app/helpers/PreferencesMap.java +++ b/arduino-core/src/processing/app/helpers/PreferencesMap.java @@ -21,6 +21,7 @@ */ package processing.app.helpers; +import org.apache.commons.compress.utils.IOUtils; import processing.app.legacy.PApplet; import java.io.*; @@ -72,9 +73,7 @@ public class PreferencesMap extends LinkedHashMap { fileInputStream = new FileInputStream(file); load(fileInputStream); } finally { - if (fileInputStream != null) { - fileInputStream.close(); - } + IOUtils.closeQuietly(fileInputStream); } } diff --git a/arduino-core/src/processing/app/legacy/PApplet.java b/arduino-core/src/processing/app/legacy/PApplet.java index e89955263..2a9abc56e 100644 --- a/arduino-core/src/processing/app/legacy/PApplet.java +++ b/arduino-core/src/processing/app/legacy/PApplet.java @@ -1,5 +1,7 @@ package processing.app.legacy; +import org.apache.commons.compress.utils.IOUtils; + import java.io.*; import java.text.NumberFormat; import java.util.ArrayList; @@ -272,13 +274,7 @@ public class PApplet { if (is != null) return loadStrings(is); return null; } finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - // noop - } - } + IOUtils.closeQuietly(is); } } @@ -312,14 +308,7 @@ public class PApplet { e.printStackTrace(); //throw new RuntimeException("Error inside loadStrings()"); } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - //ignore - } - } - + IOUtils.closeQuietly(reader); } return null; } @@ -335,27 +324,25 @@ public class PApplet { outputStream = createOutput(file); saveStrings(outputStream, strings); } finally { - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException e) { - //noop - } - } + IOUtils.closeQuietly(outputStream); } } static public void saveStrings(OutputStream output, String strings[]) { - PrintWriter writer = createWriter(output); - if (writer == null) { - return; + PrintWriter writer = null; + try { + writer = createWriter(output); + if (writer == null) { + return; + } + for (String string : strings) { + writer.println(string); + } + writer.flush(); + } finally { + IOUtils.closeQuietly(writer); } - for (String string : strings) { - writer.println(string); - } - writer.flush(); - writer.close(); }