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

Lines changed: 4 additions & 9 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 6 additions & 9 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 14 additions & 10 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 2 additions & 3 deletions
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
}

0 commit comments

Comments
 (0)