]> gitweb.fperrin.net Git - GpsPrune.git/commitdiff
Download tiles in chunks
authorFrédéric Perrin <frederic.perrin@att.com>
Thu, 30 Jan 2020 11:14:46 +0000 (11:14 +0000)
committerFrédéric Perrin <frederic.perrin@att.com>
Thu, 30 Jan 2020 14:09:36 +0000 (14:09 +0000)
Reading byte-per-byte makes the download CPU-limited, while that change
makes it closer to bandwidth-limited

src/tim/prune/function/srtm/Srtm3Source.java
src/tim/prune/function/srtm/SrtmGl1Source.java
src/tim/prune/function/srtm/SrtmSource.java
src/tim/prune/function/srtm/SrtmViewfinderSource.java

index 14fad598948ef796c702bcd633048b97d0d121be..cc0da79b1c790f85e4a21ca473ea06b8bd457c9d 100644 (file)
@@ -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)
                {
index 51e6d770cee72fcef87eb19afdf1da633fc31234..f8639b7837a2bdf3ebae19b6837ddb71f759c87e 100644 (file)
@@ -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)
                {
index 44ceccfab52a13a5ced20108ad539cafbf2c6fb2..6e49ad18383060541e8378c854df365db86c0144 100644 (file)
@@ -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
        {
index 17790ae2f2b1cc3cee89bc4d7cad52fcb3104ac2..a25e9c4e95b55b68341a520df62ad19f54aac00e 100644 (file)
@@ -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)
                {