/** * Download a file at the provided url and save it as the given downloadFile if the downloadFile does not exist. */ private void downloadFile(File downloadFile, String url) throws IOException { if (!downloadFile.exists()) { int max = 3; for (int trial = 0; trial < max; trial++) { try { downloader.downloadFile(url, downloadFile.getAbsolutePath()); return; } catch (SocketTimeoutException ex) { if (trial >= max - 1) throw new RuntimeException(ex); try { Thread.sleep(sleep); } catch (InterruptedException ignored) { } } } } }
private byte[] getByteArrayFromFile(double lat, double lon) throws InterruptedException, IOException { String zippedURL = baseUrl + getDownloadURL(lat, lon); File file = new File(cacheDir, new File(zippedURL).getName()); InputStream is; // get zip file if not already in cacheDir if (!file.exists()) for (int i = 0; i < 3; i++) { try { downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; } catch (SocketTimeoutException ex) { // just try again after a little nap Thread.sleep(2000); } catch (FileNotFoundException ex) { if (zippedURL.contains(".hgt.zip")) { zippedURL = zippedURL.replace(".hgt.zip", "hgt.zip"); } else { throw ex; } } } return readFile(file); }
/** * Download a file at the provided url and save it as the given downloadFile if the downloadFile does not exist. */ private void downloadFile(File downloadFile, String url) throws IOException { if (!downloadFile.exists()) { int max = 3; for (int trial = 0; trial < max; trial++) { try { downloader.downloadFile(url, downloadFile.getAbsolutePath()); return; } catch (SocketTimeoutException ex) { if (trial >= max - 1) throw new RuntimeException(ex); try { Thread.sleep(sleep); } catch (InterruptedException ignored) { } } } } }
downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; } catch (SocketTimeoutException ex)
private byte[] getByteArrayFromFile(double lat, double lon) throws InterruptedException, IOException { String zippedURL = baseUrl + getDownloadURL(lat, lon); File file = new File(cacheDir, new File(zippedURL).getName()); InputStream is; // get zip file if not already in cacheDir if (!file.exists()) for (int i = 0; i < 3; i++) { try { downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; } catch (SocketTimeoutException ex) { // just try again after a little nap Thread.sleep(2000); } catch (FileNotFoundException ex) { if (zippedURL.contains(".hgt.zip")) { zippedURL = zippedURL.replace(".hgt.zip", "hgt.zip"); } else { throw ex; } } } return readFile(file); }
private byte[] getByteArrayFromZipFile(String fileDetails) throws InterruptedException, FileNotFoundException, IOException { String zippedURL = baseUrl + "/" + fileDetails + ".hgt.zip"; File file = new File(cacheDir, new File(zippedURL).getName()); InputStream is; // get zip file if not already in cacheDir if (!file.exists()) for (int i = 0; i < 3; i++) { try { downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; } catch (SocketTimeoutException ex) { // just try again after a little nap Thread.sleep(2000); continue; } } is = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(is); zis.getNextEntry(); BufferedInputStream buff = new BufferedInputStream(zis); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[0xFFFF]; int len; while ((len = buff.read(buffer)) > 0) { os.write(buffer, 0, len); } os.flush(); Helper.close(buff); return os.toByteArray(); }
for (int trial = 0; trial < max; trial++) { try { downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; } catch (SocketTimeoutException ex) {
downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; } catch (SocketTimeoutException ex)