Skip to content

Commit 365b0bd

Browse files
author
Federico Fissore
committed
Closing streams using IOUtils.closeQuietly
Fixed badly handled stream found in the meanwhile
1 parent a5ad02f commit 365b0bd

26 files changed

+113
-193
lines changed

app/src/processing/app/Base.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.google.common.base.Predicate;
4040
import com.google.common.base.Predicates;
4141
import com.google.common.collect.Collections2;
42+
import org.apache.commons.compress.utils.IOUtils;
4243
import org.apache.commons.lang3.StringUtils;
4344
import processing.app.debug.TargetBoard;
4445
import processing.app.debug.TargetPackage;
@@ -2517,9 +2518,7 @@ static public byte[] loadBytesRaw(File file) throws IOException {
25172518
}
25182519
return buffer;
25192520
} finally {
2520-
if (input != null) {
2521-
input.close();
2522-
}
2521+
IOUtils.closeQuietly(input);
25232522
}
25242523
}
25252524

@@ -2567,12 +2566,8 @@ static public void copyFile(File sourceFile,
25672566
}
25682567
to.flush();
25692568
} finally {
2570-
if (from != null) {
2571-
from.close(); // ??
2572-
}
2573-
if (to != null) {
2574-
to.close(); // ??
2575-
}
2569+
IOUtils.closeQuietly(from);
2570+
IOUtils.closeQuietly(to);
25762571
}
25772572

25782573
targetFile.setLastModified(sourceFile.lastModified());

app/src/processing/app/Editor.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.common.base.Predicate;
2929
import com.jcraft.jsch.JSchException;
3030
import jssc.SerialPortException;
31+
import org.apache.commons.compress.utils.IOUtils;
3132
import processing.app.debug.*;
3233
import processing.app.forms.PasswordAuthorizationDialog;
3334
import processing.app.helpers.OSUtils;
@@ -965,11 +966,7 @@ protected String findClassInZipFile(String base, File file) {
965966
//System.err.println("Ignoring " + filename + " (" + e.getMessage() + ")");
966967
e.printStackTrace();
967968
} finally {
968-
if (zipFile != null)
969-
try {
970-
zipFile.close();
971-
} catch (IOException e) {
972-
}
969+
IOUtils.closeQuietly(zipFile);
973970
}
974971
return null;
975972
}

app/src/processing/app/EditorConsoleStream.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package processing.app;
22

33
import cc.arduino.files.DeleteFilesOnShutdown;
4+
import org.apache.commons.compress.utils.IOUtils;
45

56
import static processing.app.I18n._;
67

@@ -82,17 +83,13 @@ public static void quit() {
8283
System.setErr(systemErr);
8384

8485
// close the PrintStream
85-
consoleOut.close();
86-
consoleErr.close();
86+
IOUtils.closeQuietly(consoleOut);
87+
IOUtils.closeQuietly(consoleErr);
8788

8889
// also have to close the original FileOutputStream
8990
// otherwise it won't be shut down completely
90-
try {
91-
stdoutFile.close();
92-
stderrFile.close();
93-
} catch (IOException e) {
94-
e.printStackTrace();
95-
}
91+
IOUtils.closeQuietly(stdoutFile);
92+
IOUtils.closeQuietly(stderrFile);
9693

9794
outFile.delete();
9895
errFile.delete();
@@ -149,4 +146,4 @@ static public void setCurrent(EditorConsole console) {
149146
currentConsole = console;
150147
}
151148

152-
}
149+
}

app/src/processing/app/UpdateCheck.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package processing.app;
2424

25+
import org.apache.commons.compress.utils.IOUtils;
2526
import processing.app.legacy.PApplet;
2627

2728
import javax.swing.*;
@@ -133,9 +134,7 @@ protected int readInt(String filename) throws IOException {
133134
reader = new BufferedReader(new InputStreamReader(url.openStream()));
134135
return Integer.parseInt(reader.readLine());
135136
} finally {
136-
if (reader != null) {
137-
reader.close();
138-
}
137+
IOUtils.closeQuietly(reader);
139138
}
140139
}
141140
}

app/src/processing/app/syntax/PdeKeywords.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package processing.app.syntax;
2626

2727
import cc.arduino.contributions.libraries.ContributedLibrary;
28+
import org.apache.commons.compress.utils.IOUtils;
2829
import org.fife.ui.rsyntaxtextarea.TokenMap;
2930
import org.fife.ui.rsyntaxtextarea.TokenTypes;
3031
import processing.app.Base;
@@ -126,9 +127,7 @@ private void parseKeywordsTxt(File input) throws Exception {
126127

127128
fillMissingTokenType();
128129
} finally {
129-
if (reader != null) {
130-
reader.close();
131-
}
130+
IOUtils.closeQuietly(reader);
132131
}
133132

134133
}

app/src/processing/app/syntax/SketchTextArea.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
package processing.app.syntax;
3232

33+
import org.apache.commons.compress.utils.IOUtils;
3334
import org.fife.ui.rsyntaxtextarea.*;
3435
import org.fife.ui.rsyntaxtextarea.Theme;
3536
import org.fife.ui.rsyntaxtextarea.Token;
@@ -102,9 +103,7 @@ public void setTheme(String name) throws IOException {
102103
Theme theme = Theme.load(defaultXmlInputStream);
103104
theme.apply(this);
104105
} finally {
105-
if (defaultXmlInputStream != null) {
106-
defaultXmlInputStream.close();
107-
}
106+
IOUtils.closeQuietly(defaultXmlInputStream);
108107
}
109108

110109
setForeground(processing.app.Theme.getColor("editor.fgcolor"));

app/src/processing/app/tools/Archiver.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package processing.app.tools;
2525

26+
import org.apache.commons.compress.utils.IOUtils;
2627
import processing.app.Base;
2728
import processing.app.Editor;
2829
import processing.app.Sketch;
@@ -124,22 +125,21 @@ public void run() {
124125
if (filename != null) {
125126
newbie = new File(directory, filename);
126127

128+
ZipOutputStream zos = null;
127129
try {
128130
//System.out.println(newbie);
129-
FileOutputStream zipOutputFile = new FileOutputStream(newbie);
130-
ZipOutputStream zos = new ZipOutputStream(zipOutputFile);
131+
zos = new ZipOutputStream(new FileOutputStream(newbie));
131132

132133
// recursively fill the zip file
133134
buildZip(location, name, zos);
134135

135136
// close up the jar file
136137
zos.flush();
137-
zos.close();
138-
139138
editor.statusNotice("Created archive " + newbie.getName() + ".");
140-
141139
} catch (IOException e) {
142140
e.printStackTrace();
141+
} finally {
142+
IOUtils.closeQuietly(zos);
143143
}
144144
} else {
145145
editor.statusNotice(_("Archive sketch canceled."));

app/src/processing/app/tools/FixEncoding.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import javax.swing.JOptionPane;
3131

32+
import org.apache.commons.compress.utils.IOUtils;
3233
import processing.app.*;
3334
import static processing.app.I18n._;
3435

@@ -83,16 +84,19 @@ public void run() {
8384

8485
protected String loadWithLocalEncoding(File file) throws IOException {
8586
// FileReader uses the default encoding, which is what we want.
86-
FileReader fr = new FileReader(file);
87-
BufferedReader reader = new BufferedReader(fr);
88-
89-
StringBuffer buffer = new StringBuffer();
90-
String line = null;
91-
while ((line = reader.readLine()) != null) {
92-
buffer.append(line);
93-
buffer.append('\n');
87+
BufferedReader reader = null;
88+
try {
89+
reader = new BufferedReader(new FileReader(file));
90+
91+
StringBuffer buffer = new StringBuffer();
92+
String line;
93+
while ((line = reader.readLine()) != null) {
94+
buffer.append(line);
95+
buffer.append('\n');
96+
}
97+
return buffer.toString();
98+
} finally {
99+
IOUtils.closeQuietly(reader);
94100
}
95-
reader.close();
96-
return buffer.toString();
97101
}
98102
}

app/src/processing/app/tools/ZipDeflater.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.zip.ZipException;
1111
import java.util.zip.ZipFile;
1212

13+
import org.apache.commons.compress.utils.IOUtils;
1314
import processing.app.helpers.FileUtils;
1415

1516
public class ZipDeflater {
@@ -54,12 +55,8 @@ public void deflate() throws IOException {
5455
fos.write(buffer, 0, len);
5556
}
5657
} finally {
57-
if (fos != null) {
58-
fos.close();
59-
}
60-
if (zipInputStream != null) {
61-
zipInputStream.close();
62-
}
58+
IOUtils.closeQuietly(fos);
59+
IOUtils.closeQuietly(zipInputStream);
6360
}
6461
}
6562
}

app/test/processing/app/I18NTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package processing.app;
3131

32+
import org.apache.commons.compress.utils.IOUtils;
3233
import org.junit.Ignore;
3334
import org.junit.Test;
3435

@@ -63,9 +64,7 @@ private Properties loadProperties(File file) throws IOException {
6364
is = new FileInputStream(file);
6465
properties.load(is);
6566
} finally {
66-
if (is != null) {
67-
is.close();
68-
}
67+
IOUtils.closeQuietly(is);
6968
}
7069
return properties;
7170
}

arduino-core/src/cc/arduino/contributions/GPGDetachedSignatureVerifier.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,8 @@ public boolean verify(File signedFile, File signature, File publicKey) throws IO
7777

7878
return pgpSignature.verify();
7979
} finally {
80-
if (signatureInputStream != null) {
81-
signatureInputStream.close();
82-
}
83-
if (signedFileInputStream != null) {
84-
signedFileInputStream.close();
85-
}
80+
IOUtils.closeQuietly(signatureInputStream);
81+
IOUtils.closeQuietly(signedFileInputStream);
8682
}
8783
}
8884

@@ -92,9 +88,7 @@ private PGPPublicKey readPublicKey(File file, String keyId) throws IOException,
9288
keyIn = new BufferedInputStream(new FileInputStream(file));
9389
return readPublicKey(keyIn, keyId);
9490
} finally {
95-
if (keyIn != null) {
96-
keyIn.close();
97-
}
91+
IOUtils.closeQuietly(keyIn);
9892
}
9993
}
10094

arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.fasterxml.jackson.module.mrbean.MrBeanModule;
3838
import com.google.common.base.Function;
3939
import com.google.common.collect.FluentIterable;
40+
import org.apache.commons.compress.utils.IOUtils;
4041
import processing.app.BaseNoGui;
4142
import processing.app.I18n;
4243
import processing.app.helpers.FileUtils;
@@ -96,9 +97,7 @@ private void parseIndex(File indexFile) throws IOException {
9697
}
9798
}
9899
} finally {
99-
if (indexIn != null) {
100-
indexIn.close();
101-
}
100+
IOUtils.closeQuietly(indexIn);
102101
}
103102
}
104103

arduino-core/src/cc/arduino/contributions/packages/ContributionsIndexer.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.google.common.collect.ImmutableListMultimap;
4545
import com.google.common.collect.Iterables;
4646
import com.google.common.collect.Multimaps;
47+
import org.apache.commons.compress.utils.IOUtils;
4748
import processing.app.BaseNoGui;
4849
import processing.app.debug.TargetPackage;
4950
import processing.app.debug.TargetPlatform;
@@ -172,9 +173,7 @@ private ContributionsIndex parseIndex(File indexFile) throws IOException {
172173
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
173174
return mapper.readValue(inputStream, ContributionsIndex.class);
174175
} finally {
175-
if (inputStream != null) {
176-
inputStream.close();
177-
}
176+
IOUtils.closeQuietly(inputStream);
178177
}
179178
}
180179

arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import cc.arduino.packages.Discovery;
3434
import cc.arduino.packages.discoverers.network.BoardReachabilityFilter;
3535
import cc.arduino.packages.discoverers.network.NetworkChecker;
36+
import org.apache.commons.compress.utils.IOUtils;
3637
import processing.app.BaseNoGui;
3738
import processing.app.helpers.PreferencesMap;
3839
import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter;
@@ -199,12 +200,6 @@ public void inetAddressAdded(InetAddress address) {
199200
@Override
200201
public void inetAddressRemoved(InetAddress address) {
201202
JmDNS jmDNS = mappedJmDNSs.remove(address);
202-
if (jmDNS != null) {
203-
try {
204-
jmDNS.close();
205-
} catch (IOException e) {
206-
e.printStackTrace();
207-
}
208-
}
203+
IOUtils.closeQuietly(jmDNS);
209204
}
210205
}

arduino-core/src/cc/arduino/packages/ssh/SCP.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.jcraft.jsch.Channel;
3333
import com.jcraft.jsch.ChannelExec;
3434
import com.jcraft.jsch.Session;
35+
import org.apache.commons.compress.utils.IOUtils;
3536

3637
import java.io.*;
3738

@@ -61,12 +62,8 @@ public void open() throws IOException {
6162
}
6263

6364
public void close() throws IOException {
64-
if (out != null) {
65-
out.close();
66-
}
67-
if (in != null) {
68-
in.close();
69-
}
65+
IOUtils.closeQuietly(out);
66+
IOUtils.closeQuietly(in);
7067
if (channel != null) {
7168
channel.disconnect();
7269
}
@@ -118,9 +115,7 @@ public void sendFile(File localFile, String remoteFile) throws IOException {
118115
buf[0] = 0;
119116
out.write(buf, 0, 1);
120117
} finally {
121-
if (fis != null) {
122-
fis.close();
123-
}
118+
IOUtils.closeQuietly(fis);
124119
}
125120

126121
ensureAcknowledged();

0 commit comments

Comments
 (0)