1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03: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

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