public boolean downloadIndex()

in remoting/ide/local/src/org/netbeans/modules/jackpot30/remoting/local/LocalServerImpl.java [100:169]


    public boolean downloadIndex(RemoteIndex idx, ProgressContributor progress) throws IOException {
        try {
            progress.progress("Downloading index from " + idx.remote.toURI() + " subindex " + idx.remoteSegment);
        } catch (URISyntaxException ex) {
            Exceptions.printStackTrace(ex);
        }
        
        URL url = new URL(idx.remote.toExternalForm() + "/downloadable/index?path=" + idx.remoteSegment);
        URLConnection c = url.openConnection();
        String totalUnpackedSizeString = c.getHeaderField("NB-Total-Unpacked-Size");
        long totalUnpackedSize = -1;
        try {
            totalUnpackedSize = Long.parseLong(totalUnpackedSizeString);
        } catch (NumberFormatException ex) {
            Logger.getLogger(LocalServerImpl.class.getName()).log(Level.FINE, null, ex);
        }
        InputStream in = url.openStream();
        JarInputStream jis = new JarInputStream(in);
        File cacheDir = Places.getCacheSubdirectory(CACHE_PATH);
        File newTarget = new File(cacheDir, idx.remoteSegment + ".new");
        final byte[] BUFFER = new byte[4096];
        long written = 0;

        progress.start(TOTAL_WORK);

        try {
        ZipEntry ze;

        while ((ze = jis.getNextEntry()) != null) {
            if (ze.isDirectory()) continue;

            File targetFile = new File(newTarget, ze.getName());

            targetFile.getParentFile().mkdirs();

            OutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile));

            int read;

            while ((read = jis.read(BUFFER)) != (-1)) {
                out.write(BUFFER, 0, read);
                written += read;
                if (totalUnpackedSize > 0) {
                    progress.progress(Math.min(TOTAL_WORK, (int) (((double) written / totalUnpackedSize) * TOTAL_WORK)));
                }
            }

            out.close();
        }

        File target = new File(cacheDir, idx.remoteSegment);
        File old = new File(cacheDir, idx.remoteSegment + ".old");

        target.renameTo(old);
        newTarget.renameTo(target);

        FileObject oldFO = FileUtil.toFileObject(old);

        if (oldFO != null) oldFO.delete();

        if (serverPort > 0) {
            new URL("http://localhost:" + serverPort + "/index/internal/indexUpdated").openStream().close();
        }

        } finally {
        progress.finish();
        }
        
        return true;
    }