1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Closing streams using IOUtils.closeQuietly

Fixed badly handled stream found in the meanwhile
This commit is contained in:
Federico Fissore 2015-05-21 16:47:50 +02:00
parent a5ad02f818
commit 365b0bdc94
26 changed files with 112 additions and 192 deletions

View File

@ -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());

View File

@ -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;
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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"));

View File

@ -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."));

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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 {

View File

@ -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<String, String> 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

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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<String, String> {
fileInputStream = new FileInputStream(file);
load(fileInputStream);
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
IOUtils.closeQuietly(fileInputStream);
}
}

View File

@ -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();
}