use of ru.playsoftware.j2meloader.util.ConverterException in project J2ME-Loader by nikita36078.
the class AppInstaller method downloadJar.
private void downloadJar() throws ConverterException {
Uri jarUri = Uri.parse(newDesc.getJarUrl());
if (jarUri.getScheme() == null) {
String schemeOfJadSource = this.uri.getScheme();
if ("http".equals(schemeOfJadSource) || "https".equals(schemeOfJadSource)) {
List<String> pathSegments = uri.getPathSegments();
StringBuilder path = new StringBuilder(pathSegments.get(0));
for (int i = 1; i < pathSegments.size() - 1; i++) {
path.append('/').append(pathSegments.get(i));
}
path.append('/').append(jarUri.getPath());
jarUri = uri.buildUpon().path(path.toString()).build();
} else {
jarUri = jarUri.buildUpon().scheme("http").build();
}
}
String url = jarUri.toString();
Log.d(TAG, "Downloading " + url);
Exception exception;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setInstanceFollowRedirects(true);
connection.setReadTimeout(3 * 60 * 1000);
connection.setConnectTimeout(15000);
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_MOVED_TEMP) {
String urlStr = connection.getHeaderField("Location");
connection.disconnect();
connection = (HttpURLConnection) new URL(urlStr).openConnection();
connection.setInstanceFollowRedirects(true);
connection.setReadTimeout(3 * 60 * 1000);
connection.setConnectTimeout(15000);
}
try (InputStream inputStream = connection.getInputStream();
OutputStream outputStream = new FileOutputStream(srcJar)) {
byte[] buffer = new byte[2048];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
}
connection.disconnect();
Log.d(TAG, "Download complete");
return;
} catch (MalformedURLException e) {
exception = e;
} catch (FileNotFoundException e) {
exception = e;
} catch (IOException e) {
exception = e;
} finally {
if (connection != null) {
connection.disconnect();
}
}
deleteTemp();
throw new ConverterException("Can't download jar", exception);
}
Aggregations