mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-16 11:21:18 +03:00
Lots of unclosed input and output streams now properly closed. They were preventing Boards Manager from working on Windows
This commit is contained in:
@ -52,23 +52,41 @@ public class GPGDetachedSignatureVerifier {
|
||||
public boolean verify(File signedFile, File signature, File publicKey) throws IOException, PGPException {
|
||||
PGPPublicKey pgpPublicKey = readPublicKey(publicKey, keyId);
|
||||
|
||||
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(new FileInputStream(signature), new BcKeyFingerprintCalculator());
|
||||
FileInputStream signatureInputStream = null;
|
||||
FileInputStream signedFileInputStream = null;
|
||||
try {
|
||||
signatureInputStream = new FileInputStream(signature);
|
||||
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(signatureInputStream, new BcKeyFingerprintCalculator());
|
||||
|
||||
PGPSignatureList pgpSignatureList = (PGPSignatureList) pgpObjectFactory.nextObject();
|
||||
assert pgpSignatureList.size() == 1;
|
||||
PGPSignature pgpSignature = pgpSignatureList.get(0);
|
||||
PGPSignatureList pgpSignatureList = (PGPSignatureList) pgpObjectFactory.nextObject();
|
||||
assert pgpSignatureList.size() == 1;
|
||||
PGPSignature pgpSignature = pgpSignatureList.get(0);
|
||||
|
||||
pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), pgpPublicKey);
|
||||
pgpSignature.update(IOUtils.toByteArray(new FileInputStream(signedFile)));
|
||||
pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), pgpPublicKey);
|
||||
signedFileInputStream = new FileInputStream(signedFile);
|
||||
pgpSignature.update(IOUtils.toByteArray(signedFileInputStream));
|
||||
|
||||
return pgpSignature.verify();
|
||||
return pgpSignature.verify();
|
||||
} finally {
|
||||
if (signatureInputStream != null) {
|
||||
signatureInputStream.close();
|
||||
}
|
||||
if (signedFileInputStream != null) {
|
||||
signedFileInputStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PGPPublicKey readPublicKey(File file, String keyId) throws IOException, PGPException {
|
||||
InputStream keyIn = new BufferedInputStream(new FileInputStream(file));
|
||||
PGPPublicKey pubKey = readPublicKey(keyIn, keyId);
|
||||
keyIn.close();
|
||||
return pubKey;
|
||||
InputStream keyIn = null;
|
||||
try {
|
||||
keyIn = new BufferedInputStream(new FileInputStream(file));
|
||||
return readPublicKey(keyIn, keyId);
|
||||
} finally {
|
||||
if (keyIn != null) {
|
||||
keyIn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PGPPublicKey readPublicKey(InputStream input, String keyId) throws IOException, PGPException {
|
||||
|
@ -70,17 +70,24 @@ public class LibrariesIndexer {
|
||||
}
|
||||
|
||||
private void parseIndex(File indexFile) throws IOException {
|
||||
InputStream indexIn = new FileInputStream(indexFile);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModule(new MrBeanModule());
|
||||
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
index = mapper.readValue(indexIn, LibrariesIndex.class);
|
||||
InputStream indexIn = null;
|
||||
try {
|
||||
indexIn = new FileInputStream(indexFile);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModule(new MrBeanModule());
|
||||
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
index = mapper.readValue(indexIn, LibrariesIndex.class);
|
||||
|
||||
for (ContributedLibrary library : index.getLibraries()) {
|
||||
if (library.getCategory() == null || "".equals(library.getCategory())) {
|
||||
library.setCategory("Uncategorized");
|
||||
for (ContributedLibrary library : index.getLibraries()) {
|
||||
if (library.getCategory() == null || "".equals(library.getCategory())) {
|
||||
library.setCategory("Uncategorized");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (indexIn != null) {
|
||||
indexIn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -277,10 +277,12 @@ public class ContributionInstaller {
|
||||
|
||||
// Replace old index with the updated one
|
||||
if (outputFile.exists()) {
|
||||
outputFile.delete();
|
||||
if (!outputFile.delete()) {
|
||||
throw new Exception("An error occurred while updating platforms index! I can't delete file " + outputFile);
|
||||
}
|
||||
}
|
||||
if (!tmpFile.renameTo(outputFile)) {
|
||||
throw new Exception("An error occurred while updating platforms index!");
|
||||
throw new Exception("An error occurred while updating platforms index! I can't rename file " + tmpFile);
|
||||
}
|
||||
|
||||
return outputFile;
|
||||
|
@ -158,13 +158,20 @@ public class ContributionsIndexer {
|
||||
}
|
||||
|
||||
private ContributionsIndex parseIndex(File indexFile) throws IOException {
|
||||
InputStream indexIn = new FileInputStream(indexFile);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModule(new MrBeanModule());
|
||||
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper.readValue(indexIn, ContributionsIndex.class);
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(indexFile);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModule(new MrBeanModule());
|
||||
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper.readValue(inputStream, ContributionsIndex.class);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void syncWithFilesystem(File hardwareFolder) throws IOException {
|
||||
|
@ -91,19 +91,13 @@ public class ArchiveExtractor {
|
||||
|
||||
// Create an ArchiveInputStream with the correct archiving algorithm
|
||||
if (archiveFile.getName().endsWith("tar.bz2")) {
|
||||
InputStream fin = new FileInputStream(archiveFile);
|
||||
fin = new BZip2CompressorInputStream(fin);
|
||||
in = new TarArchiveInputStream(fin);
|
||||
in = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(archiveFile)));
|
||||
} else if (archiveFile.getName().endsWith("zip")) {
|
||||
InputStream fin = new FileInputStream(archiveFile);
|
||||
in = new ZipArchiveInputStream(fin);
|
||||
in = new ZipArchiveInputStream(new FileInputStream(archiveFile));
|
||||
} else if (archiveFile.getName().endsWith("tar.gz")) {
|
||||
InputStream fin = new FileInputStream(archiveFile);
|
||||
fin = new GzipCompressorInputStream(fin);
|
||||
in = new TarArchiveInputStream(fin);
|
||||
in = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(archiveFile)));
|
||||
} else if (archiveFile.getName().endsWith("tar")) {
|
||||
InputStream fin = new FileInputStream(archiveFile);
|
||||
in = new TarArchiveInputStream(fin);
|
||||
in = new TarArchiveInputStream(new FileInputStream(archiveFile));
|
||||
} else {
|
||||
throw new IOException("Archive format not supported.");
|
||||
}
|
||||
@ -276,8 +270,9 @@ public class ArchiveExtractor {
|
||||
}
|
||||
|
||||
private static void copyStreamToFile(InputStream in, long size, File outputFile) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(outputFile);
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(outputFile);
|
||||
// if size is not available, copy until EOF...
|
||||
if (size == -1) {
|
||||
byte buffer[] = new byte[4096];
|
||||
@ -299,7 +294,9 @@ public class ArchiveExtractor {
|
||||
size -= length;
|
||||
}
|
||||
} finally {
|
||||
fos.close();
|
||||
if (fos != null) {
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,19 +40,18 @@ public class FileHash {
|
||||
* Calculate a message digest of a file using the algorithm specified. The
|
||||
* result is a string containing the algorithm name followed by ":" and by the
|
||||
* resulting hash in hex.
|
||||
*
|
||||
*
|
||||
* @param file
|
||||
* @param algorithm
|
||||
* For example "SHA-256"
|
||||
* @param algorithm For example "SHA-256"
|
||||
* @return The algorithm followed by ":" and the hash, for example:<br />
|
||||
* "SHA-256:ee6796513086080cca078cbb383f543c5e508b647a71c9d6f39b7bca41071883"
|
||||
* "SHA-256:ee6796513086080cca078cbb383f543c5e508b647a71c9d6f39b7bca41071883"
|
||||
* @throws IOException
|
||||
* @throws NoSuchAlgorithmException
|
||||
*/
|
||||
public static String hash(File file, String algorithm) throws IOException,
|
||||
NoSuchAlgorithmException {
|
||||
FileInputStream in = new FileInputStream(file);
|
||||
public static String hash(File file, String algorithm) throws IOException, NoSuchAlgorithmException {
|
||||
FileInputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream(file);
|
||||
byte buff[] = new byte[10240];
|
||||
MessageDigest digest = MessageDigest.getInstance(algorithm);
|
||||
while (in.available() > 0) {
|
||||
@ -69,7 +68,9 @@ public class FileHash {
|
||||
}
|
||||
return algorithm + ":" + res;
|
||||
} finally {
|
||||
in.close();
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -233,13 +233,6 @@ public class BaseNoGui {
|
||||
return librariesFolders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an InputStream for a file inside the Processing lib folder.
|
||||
*/
|
||||
static public InputStream getLibStream(String filename) throws IOException {
|
||||
return new FileInputStream(new File(getContentFile("lib"), filename));
|
||||
}
|
||||
|
||||
static public Platform getPlatform() {
|
||||
return platform;
|
||||
}
|
||||
@ -624,13 +617,17 @@ public class BaseNoGui {
|
||||
if (defaultLibraryJsonFile.isFile()) {
|
||||
FileUtils.copyFile(defaultLibraryJsonFile, librariesIndexFile);
|
||||
} else {
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
// Otherwise create an empty packages index
|
||||
FileOutputStream out = new FileOutputStream(librariesIndexFile);
|
||||
out = new FileOutputStream(librariesIndexFile);
|
||||
out.write("{ \"libraries\" : [ ] }".getBytes());
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ public class PreferencesData {
|
||||
// start by loading the defaults, in case something
|
||||
// important was deleted from the user prefs
|
||||
try {
|
||||
prefs.load(BaseNoGui.getLibStream(PREFS_FILE));
|
||||
prefs.load(new File(BaseNoGui.getContentFile("lib"), PREFS_FILE));
|
||||
} catch (IOException e) {
|
||||
BaseNoGui.showError(null, _("Could not read default settings.\n" +
|
||||
"You'll need to reinstall Arduino."), e);
|
||||
@ -94,41 +94,6 @@ public class PreferencesData {
|
||||
}
|
||||
|
||||
|
||||
static public String[] loadStrings(InputStream input) {
|
||||
try {
|
||||
BufferedReader reader =
|
||||
new BufferedReader(new InputStreamReader(input, "UTF-8"));
|
||||
|
||||
String lines[] = new String[100];
|
||||
int lineCount = 0;
|
||||
String line = null;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (lineCount == lines.length) {
|
||||
String temp[] = new String[lineCount << 1];
|
||||
System.arraycopy(lines, 0, temp, 0, lineCount);
|
||||
lines = temp;
|
||||
}
|
||||
lines[lineCount++] = line;
|
||||
}
|
||||
reader.close();
|
||||
|
||||
if (lineCount == lines.length) {
|
||||
return lines;
|
||||
}
|
||||
|
||||
// resize array to appropriate amount for these lines
|
||||
String output[] = new String[lineCount];
|
||||
System.arraycopy(lines, 0, output, 0, lineCount);
|
||||
return output;
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
//throw new RuntimeException("Error inside loadStrings()");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
static protected void save() {
|
||||
if (!doSave)
|
||||
return;
|
||||
@ -139,19 +104,25 @@ public class PreferencesData {
|
||||
if (preferencesFile == null) return;
|
||||
|
||||
// Fix for 0163 to properly use Unicode when writing preferences.txt
|
||||
PrintWriter writer = PApplet.createWriter(preferencesFile);
|
||||
PrintWriter writer = null;
|
||||
try {
|
||||
writer = PApplet.createWriter(preferencesFile);
|
||||
|
||||
String[] keys = prefs.keySet().toArray(new String[0]);
|
||||
Arrays.sort(keys);
|
||||
for (String key: keys) {
|
||||
if (key.startsWith("runtime."))
|
||||
continue;
|
||||
writer.println(key + "=" + prefs.get(key));
|
||||
String[] keys = prefs.keySet().toArray(new String[0]);
|
||||
Arrays.sort(keys);
|
||||
for (String key : keys) {
|
||||
if (key.startsWith("runtime."))
|
||||
continue;
|
||||
writer.println(key + "=" + prefs.get(key));
|
||||
}
|
||||
|
||||
writer.flush();
|
||||
} finally {
|
||||
if (writer != null) {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
||||
try {
|
||||
BaseNoGui.getPlatform().fixPrefsFilePermissions(preferencesFile);
|
||||
} catch (Exception e) {
|
||||
|
@ -1210,12 +1210,12 @@ public class Compiler implements MessageConsumer {
|
||||
// 2. run preproc on that code using the sugg class name
|
||||
// to create a single .java file and write to buildpath
|
||||
|
||||
FileOutputStream outputStream = null;
|
||||
try {
|
||||
// Output file
|
||||
File streamFile = new File(buildPath, sketch.getName() + ".cpp");
|
||||
FileOutputStream outputStream = new FileOutputStream(streamFile);
|
||||
outputStream = new FileOutputStream(streamFile);
|
||||
preprocessor.write(outputStream);
|
||||
outputStream.close();
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
fnfe.printStackTrace();
|
||||
String msg = _("Build folder disappeared or could not be written");
|
||||
@ -1230,6 +1230,14 @@ public class Compiler implements MessageConsumer {
|
||||
System.err.println(I18n.format(_("Uncaught exception type: {0}"), ex.getClass()));
|
||||
ex.printStackTrace();
|
||||
throw new RunnerException(ex.toString());
|
||||
} finally {
|
||||
if (outputStream != null) {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
//noop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// grab the imports from the code just preproc'd
|
||||
|
@ -21,18 +21,14 @@
|
||||
*/
|
||||
package processing.app.helpers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import processing.app.legacy.PApplet;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import processing.app.legacy.PApplet;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class PreferencesMap extends LinkedHashMap<String, String> {
|
||||
|
||||
@ -71,7 +67,15 @@ public class PreferencesMap extends LinkedHashMap<String, String> {
|
||||
* @throws IOException
|
||||
*/
|
||||
public void load(File file) throws IOException {
|
||||
load(new FileInputStream(file));
|
||||
FileInputStream fileInputStream = null;
|
||||
try {
|
||||
fileInputStream = new FileInputStream(file);
|
||||
load(fileInputStream);
|
||||
} finally {
|
||||
if (fileInputStream != null) {
|
||||
fileInputStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected String processPlatformSuffix(String key, String suffix, boolean isCurrentPlatform) {
|
||||
|
@ -266,9 +266,20 @@ public class PApplet {
|
||||
}
|
||||
|
||||
static public String[] loadStrings(File file) {
|
||||
InputStream is = createInput(file);
|
||||
if (is != null) return loadStrings(is);
|
||||
return null;
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = createInput(file);
|
||||
if (is != null) return loadStrings(is);
|
||||
return null;
|
||||
} finally {
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static public String[] loadStrings(InputStream input) {
|
||||
@ -319,14 +330,29 @@ public class PApplet {
|
||||
|
||||
|
||||
static public void saveStrings(File file, String strings[]) {
|
||||
saveStrings(createOutput(file), strings);
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
outputStream = createOutput(file);
|
||||
saveStrings(outputStream, strings);
|
||||
} finally {
|
||||
if (outputStream != null) {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
//noop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static public void saveStrings(OutputStream output, String strings[]) {
|
||||
PrintWriter writer = createWriter(output);
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
writer.println(strings[i]);
|
||||
if (writer == null) {
|
||||
return;
|
||||
}
|
||||
for (String string : strings) {
|
||||
writer.println(string);
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
Reference in New Issue
Block a user