Search in sources :

Example 1 with PermissionDeniedException

use of net.technicpack.launchercore.exception.PermissionDeniedException in project LauncherV3 by TechnicPack.

the class Download method run.

@Override
@SuppressWarnings("unused")
public void run() {
    try {
        HttpURLConnection conn = Utils.openHttpConnection(url);
        int response = conn.getResponseCode();
        int responseFamily = response / 100;
        if (responseFamily == 3) {
            String redirUrlText = conn.getHeaderField("Location");
            if (redirUrlText != null && !redirUrlText.isEmpty()) {
                URL redirectUrl = null;
                try {
                    redirectUrl = new URL(redirUrlText);
                } catch (MalformedURLException ex) {
                    throw new DownloadException("Invalid Redirect URL: " + url, ex);
                }
                conn = Utils.openHttpConnection(redirectUrl);
                response = conn.getResponseCode();
                responseFamily = response / 100;
            }
        }
        if (responseFamily != 2) {
            throw new DownloadException("The server issued a " + response + " response code.");
        }
        InputStream in = getConnectionInputStream(conn);
        size = conn.getContentLengthLong();
        outFile = new File(outPath);
        outFile.delete();
        try (ReadableByteChannel rbc = Channels.newChannel(in);
            FileOutputStream fos = new FileOutputStream(outFile)) {
            fos.getChannel().lock();
            stateChanged();
            Thread progress = new MonitorThread(Thread.currentThread(), rbc);
            progress.start();
            fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
            in.close();
            rbc.close();
            progress.interrupt();
            synchronized (timeoutLock) {
                if (isTimedOut) {
                    return;
                }
            }
            if (size > 0) {
                long fileLength = outFile.length();
                if (size == fileLength) {
                    result = Result.SUCCESS;
                } else {
                    throw new DownloadException("File size doesn't match. Expected " + size + ", got " + fileLength);
                }
            } else {
                result = Result.SUCCESS;
            }
        }
    } catch (ClosedByInterruptException ex) {
        result = Result.FAILURE;
        return;
    } catch (PermissionDeniedException e) {
        exception = e;
        result = Result.PERMISSION_DENIED;
    } catch (OverlappingFileLockException e) {
        exception = e;
        result = Result.LOCK_FAILED;
    } catch (DownloadException e) {
        exception = e;
        result = Result.FAILURE;
    } catch (Exception e) {
        exception = e;
        e.printStackTrace();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) PermissionDeniedException(net.technicpack.launchercore.exception.PermissionDeniedException) DownloadException(net.technicpack.launchercore.exception.DownloadException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) DownloadException(net.technicpack.launchercore.exception.DownloadException) PermissionDeniedException(net.technicpack.launchercore.exception.PermissionDeniedException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Aggregations

ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 DownloadException (net.technicpack.launchercore.exception.DownloadException)1 PermissionDeniedException (net.technicpack.launchercore.exception.PermissionDeniedException)1