Skip to content

Commit cd49d29

Browse files
author
Federico Fissore
committed
Lots of unclosed input and output streams now properly closed. They were preventing Boards Manager from working on Windows
1 parent 71106ed commit cd49d29

File tree

15 files changed

+262
-211
lines changed

15 files changed

+262
-211
lines changed

app/src/processing/app/Base.java

+34-32
Original file line numberDiff line numberDiff line change
@@ -2406,14 +2406,6 @@ static public Image getLibImage(String name, Component who) {
24062406
}
24072407

24082408

2409-
/**
2410-
* Return an InputStream for a file inside the Processing lib folder.
2411-
*/
2412-
static public InputStream getLibStream(String filename) throws IOException {
2413-
return BaseNoGui.getLibStream(filename);
2414-
}
2415-
2416-
24172409
// ...................................................................
24182410

24192411

@@ -2431,17 +2423,22 @@ static public int countLines(String what) {
24312423
*/
24322424
static public byte[] loadBytesRaw(File file) throws IOException {
24332425
int size = (int) file.length();
2434-
FileInputStream input = new FileInputStream(file);
2435-
byte buffer[] = new byte[size];
2436-
int offset = 0;
2437-
int bytesRead;
2438-
while ((bytesRead = input.read(buffer, offset, size - offset)) != -1) {
2439-
offset += bytesRead;
2440-
if (bytesRead == 0) break;
2426+
FileInputStream input = null;
2427+
try {
2428+
input = new FileInputStream(file);
2429+
byte buffer[] = new byte[size];
2430+
int offset = 0;
2431+
int bytesRead;
2432+
while ((bytesRead = input.read(buffer, offset, size - offset)) != -1) {
2433+
offset += bytesRead;
2434+
if (bytesRead == 0) break;
2435+
}
2436+
return buffer;
2437+
} finally {
2438+
if (input != null) {
2439+
input.close();
2440+
}
24412441
}
2442-
input.close(); // weren't properly being closed
2443-
input = null;
2444-
return buffer;
24452442
}
24462443

24472444

@@ -2476,20 +2473,25 @@ static public HashMap<String, String> readSettings(File inputFile) {
24762473

24772474
static public void copyFile(File sourceFile,
24782475
File targetFile) throws IOException {
2479-
InputStream from =
2480-
new BufferedInputStream(new FileInputStream(sourceFile));
2481-
OutputStream to =
2482-
new BufferedOutputStream(new FileOutputStream(targetFile));
2483-
byte[] buffer = new byte[16 * 1024];
2484-
int bytesRead;
2485-
while ((bytesRead = from.read(buffer)) != -1) {
2486-
to.write(buffer, 0, bytesRead);
2487-
}
2488-
to.flush();
2489-
from.close(); // ??
2490-
from = null;
2491-
to.close(); // ??
2492-
to = null;
2476+
InputStream from = null;
2477+
OutputStream to = null;
2478+
try {
2479+
from = new BufferedInputStream(new FileInputStream(sourceFile));
2480+
to = new BufferedOutputStream(new FileOutputStream(targetFile));
2481+
byte[] buffer = new byte[16 * 1024];
2482+
int bytesRead;
2483+
while ((bytesRead = from.read(buffer)) != -1) {
2484+
to.write(buffer, 0, bytesRead);
2485+
}
2486+
to.flush();
2487+
} finally {
2488+
if (from != null) {
2489+
from.close(); // ??
2490+
}
2491+
if (to != null) {
2492+
to.close(); // ??
2493+
}
2494+
}
24932495

24942496
targetFile.setLastModified(sourceFile.lastModified());
24952497
}

app/src/processing/app/Theme.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.awt.Color;
2727
import java.awt.Font;
2828
import java.awt.SystemColor;
29+
import java.io.File;
2930

3031
import processing.app.helpers.PreferencesHelper;
3132
import processing.app.helpers.PreferencesMap;
@@ -45,7 +46,7 @@ public class Theme {
4546

4647
static protected void init() {
4748
try {
48-
table.load(Base.getLibStream("theme/theme.txt"));
49+
table.load(new File(BaseNoGui.getContentFile("lib"), "theme/theme.txt"));
4950
} catch (Exception te) {
5051
Base.showError(null, _("Could not read color theme settings.\n" +
5152
"You'll need to reinstall Arduino."), te);

app/src/processing/app/UpdateCheck.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222

2323
package processing.app;
2424

25+
import processing.app.legacy.PApplet;
26+
27+
import javax.swing.*;
2528
import java.io.BufferedReader;
26-
import java.io.InputStream;
29+
import java.io.IOException;
2730
import java.io.InputStreamReader;
2831
import java.net.URL;
2932
import java.net.URLEncoder;
3033
import java.util.Random;
3134

32-
import javax.swing.JOptionPane;
33-
34-
import processing.app.legacy.PApplet;
3535
import static processing.app.I18n._;
3636

3737

@@ -126,11 +126,16 @@ public void run() {
126126
}
127127

128128

129-
protected int readInt(String filename) throws Exception {
129+
protected int readInt(String filename) throws IOException {
130130
URL url = new URL(filename);
131-
InputStream stream = url.openStream();
132-
InputStreamReader isr = new InputStreamReader(stream);
133-
BufferedReader reader = new BufferedReader(isr);
134-
return Integer.parseInt(reader.readLine());
131+
BufferedReader reader = null;
132+
try {
133+
reader = new BufferedReader(new InputStreamReader(url.openStream()));
134+
return Integer.parseInt(reader.readLine());
135+
} finally {
136+
if (reader != null) {
137+
reader.close();
138+
}
139+
}
135140
}
136141
}

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

+48-43
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ static public KeywordMap getKeywords() {
6161
try {
6262
keywordColoring = new KeywordMap(false);
6363
keywordToReference = new Hashtable();
64-
getKeywords(Base.getLibStream("keywords.txt"));
64+
getKeywords(new File(BaseNoGui.getContentFile("lib"), "keywords.txt"));
6565
for (ContributedLibrary lib : Base.getLibraries()) {
6666
File keywords = new File(lib.getInstalledFolder(), "keywords.txt");
67-
if (keywords.exists()) getKeywords(new FileInputStream(keywords));
67+
if (keywords.exists()) getKeywords(keywords);
6868
}
6969
} catch (Exception e) {
7070
Base.showError("Problem loading keywords",
@@ -76,51 +76,56 @@ static public KeywordMap getKeywords() {
7676
return keywordColoring;
7777
}
7878

79-
static private void getKeywords(InputStream input) throws Exception {
80-
InputStreamReader isr = new InputStreamReader(input);
81-
BufferedReader reader = new BufferedReader(isr);
82-
83-
String line = null;
84-
while ((line = reader.readLine()) != null) {
85-
//System.out.println("line is " + line);
86-
// in case there's any garbage on the line
87-
//if (line.trim().length() == 0) continue;
88-
89-
String pieces[] = PApplet.split(line, '\t');
90-
if (pieces.length >= 2) {
91-
//int tab = line.indexOf('\t');
92-
// any line with no tab is ignored
93-
// meaning that a comment is any line without a tab
94-
//if (tab == -1) continue;
95-
96-
String keyword = pieces[0].trim();
97-
//String keyword = line.substring(0, tab).trim();
98-
//String second = line.substring(tab + 1);
99-
//tab = second.indexOf('\t');
100-
//String coloring = second.substring(0, tab).trim();
101-
//String htmlFilename = second.substring(tab + 1).trim();
102-
String coloring = pieces[1].trim();
103-
104-
if (coloring.length() > 0 && Character.isDigit(coloring.charAt(coloring.length() - 1))) {
105-
// text will be KEYWORD or LITERAL
106-
boolean isKey = (coloring.charAt(0) == 'K');
107-
// KEYWORD1 -> 0, KEYWORD2 -> 1, etc
108-
int num = coloring.charAt(coloring.length() - 1) - '1';
109-
byte id = (byte)
110-
((isKey ? Token.KEYWORD1 : Token.LITERAL1) + num);
111-
//System.out.println("got " + (isKey ? "keyword" : "literal") +
112-
// (num+1) + " for " + keyword);
113-
keywordColoring.add(keyword, id);
114-
}
115-
if (pieces.length >= 3) {
116-
String htmlFilename = pieces[2].trim();
117-
if (htmlFilename.length() > 0) {
118-
keywordToReference.put(keyword, htmlFilename);
79+
static private void getKeywords(File input) throws IOException {
80+
BufferedReader reader = null;
81+
try {
82+
reader = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
83+
84+
String line = null;
85+
while ((line = reader.readLine()) != null) {
86+
//System.out.println("line is " + line);
87+
// in case there's any garbage on the line
88+
//if (line.trim().length() == 0) continue;
89+
90+
String pieces[] = PApplet.split(line, '\t');
91+
if (pieces.length >= 2) {
92+
//int tab = line.indexOf('\t');
93+
// any line with no tab is ignored
94+
// meaning that a comment is any line without a tab
95+
//if (tab == -1) continue;
96+
97+
String keyword = pieces[0].trim();
98+
//String keyword = line.substring(0, tab).trim();
99+
//String second = line.substring(tab + 1);
100+
//tab = second.indexOf('\t');
101+
//String coloring = second.substring(0, tab).trim();
102+
//String htmlFilename = second.substring(tab + 1).trim();
103+
String coloring = pieces[1].trim();
104+
105+
if (coloring.length() > 0 && Character.isDigit(coloring.charAt(coloring.length() - 1))) {
106+
// text will be KEYWORD or LITERAL
107+
boolean isKey = (coloring.charAt(0) == 'K');
108+
// KEYWORD1 -> 0, KEYWORD2 -> 1, etc
109+
int num = coloring.charAt(coloring.length() - 1) - '1';
110+
byte id = (byte)
111+
((isKey ? Token.KEYWORD1 : Token.LITERAL1) + num);
112+
//System.out.println("got " + (isKey ? "keyword" : "literal") +
113+
// (num+1) + " for " + keyword);
114+
keywordColoring.add(keyword, id);
115+
}
116+
if (pieces.length >= 3) {
117+
String htmlFilename = pieces[2].trim();
118+
if (htmlFilename.length() > 0) {
119+
keywordToReference.put(keyword, htmlFilename);
120+
}
119121
}
120122
}
121123
}
124+
} finally {
125+
if (reader != null) {
126+
reader.close();
127+
}
122128
}
123-
reader.close();
124129
}
125130

126131

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

+32-14
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,41 @@ public GPGDetachedSignatureVerifier(String keyId) {
5252
public boolean verify(File signedFile, File signature, File publicKey) throws IOException, PGPException {
5353
PGPPublicKey pgpPublicKey = readPublicKey(publicKey, keyId);
5454

55-
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(new FileInputStream(signature), new BcKeyFingerprintCalculator());
56-
57-
PGPSignatureList pgpSignatureList = (PGPSignatureList) pgpObjectFactory.nextObject();
58-
assert pgpSignatureList.size() == 1;
59-
PGPSignature pgpSignature = pgpSignatureList.get(0);
60-
61-
pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), pgpPublicKey);
62-
pgpSignature.update(IOUtils.toByteArray(new FileInputStream(signedFile)));
63-
64-
return pgpSignature.verify();
55+
FileInputStream signatureInputStream = null;
56+
FileInputStream signedFileInputStream = null;
57+
try {
58+
signatureInputStream = new FileInputStream(signature);
59+
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(signatureInputStream, new BcKeyFingerprintCalculator());
60+
61+
PGPSignatureList pgpSignatureList = (PGPSignatureList) pgpObjectFactory.nextObject();
62+
assert pgpSignatureList.size() == 1;
63+
PGPSignature pgpSignature = pgpSignatureList.get(0);
64+
65+
pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), pgpPublicKey);
66+
signedFileInputStream = new FileInputStream(signedFile);
67+
pgpSignature.update(IOUtils.toByteArray(signedFileInputStream));
68+
69+
return pgpSignature.verify();
70+
} finally {
71+
if (signatureInputStream != null) {
72+
signatureInputStream.close();
73+
}
74+
if (signedFileInputStream != null) {
75+
signedFileInputStream.close();
76+
}
77+
}
6578
}
6679

6780
private PGPPublicKey readPublicKey(File file, String keyId) throws IOException, PGPException {
68-
InputStream keyIn = new BufferedInputStream(new FileInputStream(file));
69-
PGPPublicKey pubKey = readPublicKey(keyIn, keyId);
70-
keyIn.close();
71-
return pubKey;
81+
InputStream keyIn = null;
82+
try {
83+
keyIn = new BufferedInputStream(new FileInputStream(file));
84+
return readPublicKey(keyIn, keyId);
85+
} finally {
86+
if (keyIn != null) {
87+
keyIn.close();
88+
}
89+
}
7290
}
7391

7492
private PGPPublicKey readPublicKey(InputStream input, String keyId) throws IOException, PGPException {

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

+18-11
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,24 @@ public void parseIndex() throws IOException {
7070
}
7171

7272
private void parseIndex(File indexFile) throws IOException {
73-
InputStream indexIn = new FileInputStream(indexFile);
74-
ObjectMapper mapper = new ObjectMapper();
75-
mapper.registerModule(new MrBeanModule());
76-
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
77-
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
78-
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
79-
index = mapper.readValue(indexIn, LibrariesIndex.class);
80-
81-
for (ContributedLibrary library : index.getLibraries()) {
82-
if (library.getCategory() == null || "".equals(library.getCategory())) {
83-
library.setCategory("Uncategorized");
73+
InputStream indexIn = null;
74+
try {
75+
indexIn = new FileInputStream(indexFile);
76+
ObjectMapper mapper = new ObjectMapper();
77+
mapper.registerModule(new MrBeanModule());
78+
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
79+
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
80+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
81+
index = mapper.readValue(indexIn, LibrariesIndex.class);
82+
83+
for (ContributedLibrary library : index.getLibraries()) {
84+
if (library.getCategory() == null || "".equals(library.getCategory())) {
85+
library.setCategory("Uncategorized");
86+
}
87+
}
88+
} finally {
89+
if (indexIn != null) {
90+
indexIn.close();
8491
}
8592
}
8693
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,12 @@ private File download(MultiStepProgress progress, String packageIndexUrl) throws
277277

278278
// Replace old index with the updated one
279279
if (outputFile.exists()) {
280-
outputFile.delete();
280+
if (!outputFile.delete()) {
281+
throw new Exception("An error occurred while updating platforms index! I can't delete file " + outputFile);
282+
}
281283
}
282284
if (!tmpFile.renameTo(outputFile)) {
283-
throw new Exception("An error occurred while updating platforms index!");
285+
throw new Exception("An error occurred while updating platforms index! I can't rename file " + tmpFile);
284286
}
285287

286288
return outputFile;

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,20 @@ private boolean isSigned(File indexFile) {
158158
}
159159

160160
private ContributionsIndex parseIndex(File indexFile) throws IOException {
161-
InputStream indexIn = new FileInputStream(indexFile);
162-
ObjectMapper mapper = new ObjectMapper();
163-
mapper.registerModule(new MrBeanModule());
164-
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
165-
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
166-
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
167-
return mapper.readValue(indexIn, ContributionsIndex.class);
161+
InputStream inputStream = null;
162+
try {
163+
inputStream = new FileInputStream(indexFile);
164+
ObjectMapper mapper = new ObjectMapper();
165+
mapper.registerModule(new MrBeanModule());
166+
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
167+
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
168+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
169+
return mapper.readValue(inputStream, ContributionsIndex.class);
170+
} finally {
171+
if (inputStream != null) {
172+
inputStream.close();
173+
}
174+
}
168175
}
169176

170177
public void syncWithFilesystem(File hardwareFolder) throws IOException {

0 commit comments

Comments
 (0)