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);
HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
// Define streams
- FileOutputStream outStream = null;
InputStream inStream = null;
conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
{
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)
{
try
{
HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
- long fileLength = 0L;
-
// Define streams
- FileOutputStream outStream = null;
InputStream inStream = null;
// Documentation about HTTP interface at:
{
// Found the tile, we're good
inStream = conn.getInputStream();
- fileLength = conn.getContentLengthLong();
break;
}
else if (status == 302)
}
}
- // _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)
{
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)
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
{
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);
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)
{