From: Frédéric Perrin Date: Thu, 30 Jan 2020 11:14:46 +0000 (+0000) Subject: Download tiles in chunks X-Git-Tag: v19.2.fp6~1^2~2 X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=commitdiff_plain;h=860ae36b24e22ff49b23cc5e3b51e9bd5520712b Download tiles in chunks Reading byte-per-byte makes the download CPU-limited, while that change makes it closer to bandwidth-limited --- diff --git a/src/tim/prune/function/srtm/Srtm3Source.java b/src/tim/prune/function/srtm/Srtm3Source.java index 14fad59..cc0da79 100644 --- a/src/tim/prune/function/srtm/Srtm3Source.java +++ b/src/tim/prune/function/srtm/Srtm3Source.java @@ -110,7 +110,6 @@ public class Srtm3Source extends SrtmSource { public boolean downloadTile(SrtmTile inTile) throws SrtmSourceException { - int redirects = 5; URL tileUrl = buildUrl(inTile); File outputFile = getCacheFileName(inTile); System.out.println("Download: Need to download: " + tileUrl); @@ -120,7 +119,6 @@ public class Srtm3Source extends SrtmSource { HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection(); // Define streams - FileOutputStream outStream = null; InputStream inStream = null; conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER); @@ -138,18 +136,7 @@ public class Srtm3Source extends SrtmSource { { throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent()); } - - outStream = new FileOutputStream(outputFile); - - int c; - while ((c = inStream.read()) != -1) - { - outStream.write(c); - } - // Make sure streams are closed - try {inStream.close();} catch (Exception e) {} - try {outStream.close();} catch (Exception e) {} - return true; + return downloadToFile(inStream, outputFile); } catch (IOException e) { diff --git a/src/tim/prune/function/srtm/SrtmGl1Source.java b/src/tim/prune/function/srtm/SrtmGl1Source.java index 51e6d77..f8639b7 100644 --- a/src/tim/prune/function/srtm/SrtmGl1Source.java +++ b/src/tim/prune/function/srtm/SrtmGl1Source.java @@ -88,10 +88,7 @@ public class SrtmGl1Source extends SrtmSource { try { HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection(); - long fileLength = 0L; - // Define streams - FileOutputStream outStream = null; InputStream inStream = null; // Documentation about HTTP interface at: @@ -114,7 +111,6 @@ public class SrtmGl1Source extends SrtmSource { { // Found the tile, we're good inStream = conn.getInputStream(); - fileLength = conn.getContentLengthLong(); break; } else if (status == 302) @@ -133,22 +129,7 @@ public class SrtmGl1Source extends SrtmSource { } } - // _progress.setValue(t * 10 + 1); - outStream = new FileOutputStream(outputFile); - - // Copy all the bytes to the file - int c; - long written = 0L; - while ((c = inStream.read()) != -1) - { - outStream.write(c); - written++; - // _progress.setValue(t * 10 + 1 + (int) ((10 * written) / fileLength)); - } - // Make sure streams are closed - try {inStream.close();} catch (Exception e) {} - try {outStream.close();} catch (Exception e) {} - return true; + return downloadToFile(inStream, outputFile); } catch (IOException e) { diff --git a/src/tim/prune/function/srtm/SrtmSource.java b/src/tim/prune/function/srtm/SrtmSource.java index 44ceccf..6e49ad1 100644 --- a/src/tim/prune/function/srtm/SrtmSource.java +++ b/src/tim/prune/function/srtm/SrtmSource.java @@ -3,11 +3,13 @@ package tim.prune.function.srtm; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.InputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public abstract class SrtmSource { + // methods implemented by each source public abstract String getName(); public abstract boolean isReadyToUse(); public abstract boolean downloadTile(SrtmTile inTile) @@ -15,6 +17,24 @@ public abstract class SrtmSource { public abstract int getRowSize(SrtmTile inTile); protected abstract String getSourceExtension(); + protected boolean downloadToFile(InputStream inStream, File outputFile) + throws IOException + { + FileOutputStream outStream = new FileOutputStream(outputFile); + + byte[] buffer = new byte[4096]; + int read = 0; + while ((read = inStream.read(buffer)) != -1) + { + outStream.write(buffer, 0, read); + } + // Make sure streams are closed + try {inStream.close();} catch (Exception e) {} + try {outStream.close();} catch (Exception e) {} + return true; + + } + protected int[] slurpTileHeigths(ZipInputStream inStream, int tileSize) throws IOException { diff --git a/src/tim/prune/function/srtm/SrtmViewfinderSource.java b/src/tim/prune/function/srtm/SrtmViewfinderSource.java index 17790ae..a25e9c4 100644 --- a/src/tim/prune/function/srtm/SrtmViewfinderSource.java +++ b/src/tim/prune/function/srtm/SrtmViewfinderSource.java @@ -130,15 +130,12 @@ public class SrtmViewfinderSource extends SrtmSource { throws SrtmSourceException { URL tileUrl = buildUrl(inTile); - File outputFile = getCacheFileName(inTile); System.out.println("Download: Need to download: " + tileUrl); - try { HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection(); // Define streams - FileOutputStream outStream = null; InputStream inStream = null; conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER); @@ -157,17 +154,7 @@ public class SrtmViewfinderSource extends SrtmSource { throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent()); } - outStream = new FileOutputStream(outputFile); - - int c; - while ((c = inStream.read()) != -1) - { - outStream.write(c); - } - // Make sure streams are closed - try {inStream.close();} catch (Exception e) {} - try {outStream.close();} catch (Exception e) {} - return true; + return downloadToFile(inStream, getCacheFileName(inTile)); } catch (IOException e) {