1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03:00

Faster library list downloading by downloading gzipped version

This commit is contained in:
Federico Fissore
2015-05-21 17:01:36 +02:00
parent 365b0bdc94
commit 94b16a550e
6 changed files with 189 additions and 4 deletions

View File

@ -0,0 +1,50 @@
package cc.arduino.contributions;
import cc.arduino.utils.Progress;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipUtils;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.net.URL;
public class GZippedJsonDownloader {
private final DownloadableContributionsDownloader downloader;
private final URL url;
private final URL gzippedUrl;
public GZippedJsonDownloader(DownloadableContributionsDownloader downloader, URL url, URL gzippedUrl) {
this.downloader = downloader;
this.url = url;
this.gzippedUrl = gzippedUrl;
}
public void download(File tmpFile, Progress progress, String statusText) throws Exception {
try {
new JsonDownloader(downloader, gzippedUrl).download(tmpFile, progress, statusText);
File gzipTmpFile = new File(tmpFile.getParentFile(), GzipUtils.getCompressedFilename(tmpFile.getName()));
tmpFile.renameTo(gzipTmpFile);
decompress(gzipTmpFile, tmpFile);
} catch (Exception e) {
new JsonDownloader(downloader, url).download(tmpFile, progress, statusText);
}
}
private void decompress(File gzipTmpFile, File tmpFile) throws IOException {
OutputStream os = null;
GzipCompressorInputStream gzipIs = null;
try {
os = new FileOutputStream(tmpFile);
gzipIs = new GzipCompressorInputStream(new FileInputStream(gzipTmpFile));
final byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = gzipIs.read(buffer))) {
os.write(buffer, 0, n);
}
} finally {
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(gzipIs);
}
}
}

View File

@ -0,0 +1,26 @@
package cc.arduino.contributions;
import cc.arduino.utils.Progress;
import java.io.File;
import java.net.URL;
public class JsonDownloader {
private final DownloadableContributionsDownloader downloader;
private final URL url;
public JsonDownloader(DownloadableContributionsDownloader downloader, URL url) {
this.downloader = downloader;
this.url = url;
}
public void download(File tmpFile, Progress progress, String statusText) throws Exception {
try {
downloader.download(url, tmpFile, progress, statusText);
} catch (InterruptedException e) {
// Download interrupted... just exit
return;
}
}
}

View File

@ -29,6 +29,7 @@
package cc.arduino.contributions.libraries;
import cc.arduino.contributions.DownloadableContributionsDownloader;
import cc.arduino.contributions.GZippedJsonDownloader;
import cc.arduino.utils.ArchiveExtractor;
import cc.arduino.utils.MultiStepProgress;
import cc.arduino.utils.Progress;
@ -45,6 +46,7 @@ import static processing.app.I18n._;
public class LibraryInstaller {
private static final String LIBRARY_INDEX_URL;
private static final String LIBRARY_INDEX_URL_GZ;
static {
String externalLibraryIndexUrl = System.getProperty("LIBRARY_INDEX_URL");
@ -53,6 +55,7 @@ public class LibraryInstaller {
} else {
LIBRARY_INDEX_URL = "http://downloads.arduino.cc/libraries/library_index.json";
}
LIBRARY_INDEX_URL_GZ = "http://downloads.arduino.cc/libraries/library_index.json.gz";
}
private final LibrariesIndexer indexer;
@ -77,8 +80,8 @@ public class LibraryInstaller {
File outputFile = indexer.getIndexFile();
File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp");
try {
downloader.download(url, tmpFile, progress,
_("Downloading libraries index..."));
GZippedJsonDownloader gZippedJsonDownloader = new GZippedJsonDownloader(downloader, new URL(LIBRARY_INDEX_URL), new URL(LIBRARY_INDEX_URL_GZ));
gZippedJsonDownloader.download(tmpFile, progress, _("Downloading libraries index..."));
} catch (InterruptedException e) {
// Download interrupted... just exit
return;
@ -91,8 +94,7 @@ public class LibraryInstaller {
if (outputFile.exists())
outputFile.delete();
if (!tmpFile.renameTo(outputFile))
throw new Exception(
_("An error occurred while updating libraries index!"));
throw new Exception(_("An error occurred while updating libraries index!"));
// Step 2: Rescan index
rescanLibraryIndex(progress);