Search in sources :

Example 6 with ConverterException

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);
}
Also used : ConverterException(ru.playsoftware.j2meloader.util.ConverterException) MalformedURLException(java.net.MalformedURLException) DataInputStream(java.io.DataInputStream) ZipInputStream(net.lingala.zip4j.io.inputstream.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Uri(android.net.Uri) ConverterException(ru.playsoftware.j2meloader.util.ConverterException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) FileOutputStream(java.io.FileOutputStream)

Aggregations

ConverterException (ru.playsoftware.j2meloader.util.ConverterException)6 File (java.io.File)4 IOException (java.io.IOException)4 JarFile (java.util.jar.JarFile)4 ZipFile (net.lingala.zip4j.ZipFile)4 DataInputStream (java.io.DataInputStream)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 ZipInputStream (net.lingala.zip4j.io.inputstream.ZipInputStream)3 Uri (android.net.Uri)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 AppItem (ru.playsoftware.j2meloader.applist.AppItem)1 Descriptor (ru.woesss.j2me.jar.Descriptor)1