Logo Search packages:      
Sourcecode: icedtea-web version File versions  Download package

void net::sourceforge::jnlp::cache::ResourceTracker::downloadResource ( Resource  resource  )  [inline, private]

Downloads a resource to a file, uncompressing it if required

Parameters:
resource the resource to download

Definition at line 621 of file ResourceTracker.java.

References net::sourceforge::jnlp::cache::Resource::changeStatus(), net::sourceforge::jnlp::cache::Resource::downloadVersion, net::sourceforge::jnlp::cache::Resource::fireDownloadEvent(), getCacheFile(), getVersionedResourceURL(), net::sourceforge::jnlp::cache::Resource::location, lock, and net::sourceforge::jnlp::cache::Resource::transferred.

Referenced by processResource().

                                                     {
        resource.fireDownloadEvent(); // fire DOWNLOADING

        try {
            // create out second in case in does not exist
            URLConnection con = getVersionedResourceURL(resource).openConnection();
            con.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip");

            con.connect();

            /*
             * We dont really know what we are downloading. If we ask for
             * foo.jar, the server might send us foo.jar.pack.gz or foo.jar.gz
             * instead. So we save the file with the appropriate extension
             */
            URL downloadLocation = resource.location;

            String contentEncoding = con.getContentEncoding();

            if (JNLPRuntime.isDebug()) {
                System.err.println("Content encoding for " + resource.location + ": "
                        + contentEncoding);
            }

            if (contentEncoding != null) {
                if (contentEncoding.equals("gzip")) {
                    downloadLocation = new URL(downloadLocation.toString() + ".gz");
                } else if (contentEncoding.equals("pack200-gzip")) {
                    downloadLocation = new URL(downloadLocation.toString() + ".pack.gz");
                }
            }

            InputStream in = new BufferedInputStream(con.getInputStream());
            OutputStream out = CacheUtil.getOutputStream(downloadLocation, resource.downloadVersion);
            byte buf[] = new byte[1024];
            int rlen;

            while (-1 != (rlen = in.read(buf))) {
                resource.transferred += rlen;
                out.write(buf, 0, rlen);
            }

            in.close();
            out.close();

            // explicitly close the URLConnection.
            if (con instanceof HttpURLConnection)
                ((HttpURLConnection)con).disconnect();

            /*
             * If the file was compressed, uncompress it.
             */
            if (contentEncoding != null) {
                if (contentEncoding.equals("gzip")) {
                    GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream(CacheUtil
                            .getCacheFile(downloadLocation, resource.downloadVersion)));
                    InputStream inputStream = new BufferedInputStream(gzInputStream);

                    BufferedOutputStream outputStream = new BufferedOutputStream(
                            new FileOutputStream(CacheUtil.getCacheFile(resource.location,
                                    resource.downloadVersion)));

                    while (-1 != (rlen = inputStream.read(buf))) {
                        outputStream.write(buf, 0, rlen);
                    }

                    outputStream.close();
                    inputStream.close();
                    gzInputStream.close();

                } else if (contentEncoding.equals("pack200-gzip")) {
                    GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream(
                            CacheUtil.getCacheFile(downloadLocation, resource.downloadVersion)));
                    InputStream inputStream = new BufferedInputStream(gzInputStream);

                    JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(
                            CacheUtil.getCacheFile(resource.location, resource.downloadVersion)));

                    Unpacker unpacker = Pack200.newUnpacker();
                    unpacker.unpack(inputStream, outputStream);

                    outputStream.close();
                    inputStream.close();
                    gzInputStream.close();
                }
            }

            resource.changeStatus(DOWNLOADING, DOWNLOADED);
            synchronized(lock) {
                lock.notifyAll(); // wake up wait's to check for completion
            }
            resource.fireDownloadEvent(); // fire DOWNLOADED
        }
        catch (Exception ex) {
            if (JNLPRuntime.isDebug())
                ex.printStackTrace();

            resource.changeStatus(0, ERROR);
            synchronized(lock) {
                lock.notifyAll(); // wake up wait's to check for completion
            }
            resource.fireDownloadEvent(); // fire ERROR
        }
    }


Generated by  Doxygen 1.6.0   Back to index