1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-17 22:23:10 +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:
Federico Fissore
2015-05-04 15:44:34 +02:00
parent 71106edbf8
commit cd49d29e52
15 changed files with 252 additions and 201 deletions

View File

@ -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 {

View File

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

View File

@ -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;

View File

@ -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 {

View File

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

View File

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