]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - src/tim/prune/function/srtm/Srtm3Source.java
Refactor and add comments
[GpsPrune.git] / src / tim / prune / function / srtm / Srtm3Source.java
index cc0da79b1c790f85e4a21ca473ea06b8bd457c9d..7e869f1d81f24cb3b2d1ea8f479fc228df12f650 100644 (file)
@@ -1,14 +1,11 @@
 package tim.prune.function.srtm;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.HttpURLConnection;
 
-import tim.prune.GpsPrune;
 import tim.prune.I18nManager;
 
 public class Srtm3Source extends SrtmSource {
@@ -59,7 +56,7 @@ public class Srtm3Source extends SrtmSource {
                                return buffer;
                        }
                }
-               catch (java.io.IOException e) {
+               catch (IOException e) {
                        System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
                }
                finally
@@ -111,37 +108,8 @@ public class Srtm3Source 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
-                       InputStream inStream = null;
-
-                       conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
-
-                       int status = conn.getResponseCode();
-                       if (status == 200)
-                       {
-                               inStream = conn.getInputStream();
-                       }
-                       else if (status == 404)
-                       {
-                               throw new SrtmSourceException("Tile not found: "+conn.getURL());
-                       }
-                       else
-                       {
-                               throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
-                       }
-                       return downloadToFile(inStream, outputFile);
-               }
-               catch (IOException e)
-               {
-                       throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
-               }
+               InputStream inStream = getStreamToUrl(tileUrl);
+               return readToFile(inStream, getCacheFileName(inTile));
        }
 
        public int getRowSize(SrtmTile inTile)