Skip to content

Commit 207128d

Browse files
author
Mattia Bertorello
committed
fix the misleading exception throw on windows
- The file will be close before delete it - Some refactoring of the downloader cache
1 parent 934df81 commit 207128d

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

arduino-core/src/cc/arduino/utils/network/FileDownloader.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,9 @@ private void downloadFile(boolean noResume) throws InterruptedException {
148148

149149
final File settingsFolder = BaseNoGui.getPlatform().getSettingsFolder();
150150
final String cacheFolder = Paths.get(settingsFolder.getPath(), "cache").toString();
151-
final FileDownloaderCache fileDownloaderCache =
152-
new FileDownloaderCache(cacheFolder, downloadUrl);
151+
final FileDownloaderCache fileDownloaderCache = FileDownloaderCache.getFileCached(cacheFolder, downloadUrl);
153152

154-
final boolean isChanged = fileDownloaderCache.checkIfTheFileIsChanged();
155-
156-
if (!isChanged) {
153+
if (!fileDownloaderCache.isChange()) {
157154
try {
158155
final Optional<File> fileFromCache =
159156
fileDownloaderCache.getFileFromCache();
@@ -165,28 +162,26 @@ private void downloadFile(boolean noResume) throws InterruptedException {
165162
} catch (Exception e) {
166163
log.warn(
167164
"Cannot get the file from the cache, will be downloaded a new one ", e.getCause());
168-
169165
}
170166
}
171167

172-
// Open file and seek to the end of it
173-
randomAccessOutputFile = new RandomAccessFile(outputFile, "rw");
174-
initialSize = randomAccessOutputFile.length();
175-
168+
initialSize = outputFile.length();
176169
if (noResume && initialSize > 0) {
177170
// delete file and restart downloading
178-
Files.delete(outputFile.toPath());
171+
Files.deleteIfExists(outputFile.toPath());
179172
initialSize = 0;
180173
}
181-
174+
// Open file and seek to the end of it
175+
randomAccessOutputFile = new RandomAccessFile(outputFile, "rw");
182176
randomAccessOutputFile.seek(initialSize);
183177

184178
final HttpURLConnection connection = new HttpConnectionManager(downloadUrl)
185179
.makeConnection((c) -> setDownloaded(0));
186180
final int resp = connection.getResponseCode();
187181

188182
if (resp < 200 || resp >= 300) {
189-
Files.delete(outputFile.toPath());
183+
IOUtils.closeQuietly(randomAccessOutputFile);
184+
Files.deleteIfExists(outputFile.toPath());
190185
throw new IOException("Received invalid http status code from server: " + resp);
191186
}
192187

arduino-core/src/cc/arduino/utils/network/FileDownloaderCache.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,28 @@
1919

2020
public class FileDownloaderCache {
2121
private static Logger log = LoggerFactory.getLogger(FileDownloaderCache.class);
22-
private final URL remoteURL;
2322
private final Path cacheFilePath;
24-
// Will be initialized by call the checkIfTheFileIsChanged function
25-
private String eTag;
23+
private final String remoteETag;
24+
private final String preferencesDataKey;
2625

2726
// BaseNoGui.getSettingsFolder()
28-
public FileDownloaderCache(String cacheFolder, URL remoteURL) {
29-
this.remoteURL = remoteURL;
30-
String[] splitPath = remoteURL.getPath().split("/");
27+
private FileDownloaderCache(Path cacheFilePath, String remoteETag, String preferencesDataKey) {
28+
this.cacheFilePath = cacheFilePath;
29+
this.remoteETag = remoteETag;
30+
this.preferencesDataKey = preferencesDataKey;
31+
}
32+
33+
public static FileDownloaderCache getFileCached(String cacheFolder, URL remoteURL)
34+
throws IOException, NoSuchMethodException, ScriptException, URISyntaxException {
35+
36+
final String[] splitPath = remoteURL.getPath().split("/");
37+
final String preferencesDataKey = "cache.file." + remoteURL.getPath();
38+
final Path cacheFilePath;
3139
if (splitPath.length > 0) {
32-
this.cacheFilePath = Paths.get(cacheFolder, splitPath);
40+
cacheFilePath = Paths.get(cacheFolder, splitPath);
3341
} else {
34-
this.cacheFilePath = null;
42+
cacheFilePath = null;
3543
}
36-
}
37-
38-
public boolean checkIfTheFileIsChanged()
39-
throws NoSuchMethodException, ScriptException, IOException,
40-
URISyntaxException {
4144

4245
final HttpURLConnection headRequest = new HttpConnectionManager(remoteURL)
4346
.makeConnection((connection) -> {
@@ -52,20 +55,30 @@ public boolean checkIfTheFileIsChanged()
5255
// Something bad is happening return a conservative true to try to download the file
5356
if (responseCode < 200 || responseCode >= 300) {
5457
log.warn("The head request return a bad response code " + responseCode);
55-
return true;
58+
// if something bad happend
59+
return new FileDownloaderCache(cacheFilePath, null, preferencesDataKey);
5660
}
5761

5862
final String remoteETag = headRequest.getHeaderField("ETag");
59-
final String localETag = PreferencesData.get(getPreferencesDataKey());
63+
String remoteETagClean = null;
64+
if (remoteETag != null) {
65+
remoteETagClean = remoteETag.trim().replace("\"", "");
66+
}
67+
68+
return new FileDownloaderCache(cacheFilePath, remoteETagClean, preferencesDataKey);
69+
}
70+
71+
public boolean isChange() {
72+
73+
final String localETag = PreferencesData.get(preferencesDataKey);
6074

6175
// If the header doesn't exist or the local cache doesn't exist you need to download the file
62-
if (remoteETag == null || localETag == null) {
76+
if (cacheFilePath == null || remoteETag == null || localETag == null) {
6377
return true;
6478
}
65-
eTag = remoteETag.trim().replace("\"", "");
6679

6780
// If are different means that the file is change
68-
return !eTag.equals(localETag);
81+
return !remoteETag.equals(localETag);
6982
}
7083

7184
public Optional<File> getFileFromCache() {
@@ -77,10 +90,10 @@ public Optional<File> getFileFromCache() {
7790
}
7891

7992
public void fillCache(File fileToCache) throws Exception {
80-
if (Optional.ofNullable(eTag).isPresent() &&
93+
if (Optional.ofNullable(remoteETag).isPresent() &&
8194
Optional.ofNullable(cacheFilePath).isPresent()) {
8295

83-
PreferencesData.set(getPreferencesDataKey(), eTag);
96+
PreferencesData.set(preferencesDataKey, remoteETag);
8497
// If the cache directory does not exist create it
8598
if (!Files.exists(cacheFilePath.getParent())) {
8699
Files.createDirectories(cacheFilePath.getParent());
@@ -89,7 +102,4 @@ public void fillCache(File fileToCache) throws Exception {
89102
}
90103
}
91104

92-
private String getPreferencesDataKey() {
93-
return "cache.file." + remoteURL.getPath();
94-
}
95105
}

0 commit comments

Comments
 (0)