io.github.jeremgamer.preview.actions.Download.java Source code

Java tutorial

Introduction

Here is the source code for io.github.jeremgamer.preview.actions.Download.java

Source

/**
 *   Copyright 2014/2015 JeremGamer
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. * 
 */

package io.github.jeremgamer.preview.actions;

import io.github.jeremgamer.editor.Editor;
import io.github.jeremgamer.editor.files.GeneralSave;
import io.github.jeremgamer.preview.components.CustomCheckBox;
import io.github.jeremgamer.preview.components.CustomComboBox;
import io.github.jeremgamer.preview.components.CustomProgressBar;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.swing.JComponent;

import org.apache.commons.io.IOUtils;

public class Download extends Action {

    private String url;
    private JComponent parent;
    private ArrayList<CustomProgressBar> barList = new ArrayList<CustomProgressBar>();
    private long totalBytes = 0;
    private int progressValue = 0;
    private long bytesCopied = 0;
    private short step = 1;
    private FileOutputStream fos;
    private String name;

    public Download() {
    }

    public Download(String url, JComponent parent) {
        this.url = url;
        this.parent = parent;
    }

    public void addBar(CustomProgressBar bar) {
        barList.add(bar);
    }

    @Override
    public void perform() {
        for (Action a : getChildrens()) {
            a.perform();
        }
        if (parent instanceof CustomCheckBox) {
            if (isChildren()) {
                if (((CustomCheckBox) parent).isSelected()) {
                    download();
                }
            }
        } else if (parent instanceof CustomComboBox) {
            if (isChildren()) {
                if (((CustomComboBox) parent).getSelectedItem().equals(getItemCondition())) {
                    download();
                }
            } else {
                download();
            }
        } else {
            download();
        }
    }

    private void download() {

        GeneralSave gs = new GeneralSave();
        try {
            gs.load(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        name = gs.getString("name");
        new Thread(new Runnable() {

            @Override
            public void run() {
                if (url == null) {

                } else {
                    File archive = new File(System.getProperty("user.home") + "/AppData/Roaming/.rocketbuilder/"
                            + name + "/data.zip");
                    File outputFolder = new File(
                            System.getProperty("user.home") + "/AppData/Roaming/.rocketbuilder/" + name);

                    new File(System.getProperty("user.home") + "/AppData/Roaming/.rocketbuilder/" + name).mkdirs();
                    URL webFile;
                    try {
                        webFile = new URL(url);
                        ReadableByteChannel rbc = Channels.newChannel(webFile.openStream());
                        fos = new FileOutputStream(System.getProperty("user.home")
                                + "/AppData/Roaming/.rocketbuilder/" + name + "/data.zip");
                        HttpURLConnection httpConn = (HttpURLConnection) webFile.openConnection();
                        totalBytes = httpConn.getContentLength();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    while (bytesCopied < totalBytes) {
                                        for (CustomProgressBar bar : barList) {
                                            bytesCopied = fos.getChannel().size();
                                            progressValue = (int) (100 * bytesCopied / totalBytes);
                                            bar.setValue(progressValue);
                                            if (bar.isStringPainted()) {
                                                bar.setString(progressValue + "%     " + bytesCopied / 1000 + "/"
                                                        + totalBytes / 1000 + "Kb     tape " + step + "/2");
                                            }
                                        }
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                        }).start();
                        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                        fos.close();
                        step = 2;
                        for (CustomProgressBar bar : barList) {
                            if (bar.isStringPainted()) {
                                bar.setString("tape " + step + "/2 : Extraction");
                            }
                        }

                        for (int timeout = 100; timeout > 0; timeout--) {
                            RandomAccessFile ran = null;

                            try {
                                ran = new RandomAccessFile(archive, "rw");
                                break;
                            } catch (Exception ex) {
                            } finally {
                                if (ran != null)
                                    try {
                                        ran.close();
                                    } catch (IOException ex) {
                                    }

                                ran = null;
                            }

                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException ex) {
                            }
                        }

                        ZipFile zipFile = new ZipFile(archive, Charset.forName("Cp437"));
                        Enumeration<? extends ZipEntry> entries = zipFile.entries();
                        while (entries.hasMoreElements()) {
                            ZipEntry entry = entries.nextElement();
                            File entryDestination = new File(outputFolder, entry.getName());
                            entryDestination.getParentFile().mkdirs();
                            if (entry.isDirectory())
                                entryDestination.mkdirs();
                            else {
                                InputStream in = zipFile.getInputStream(entry);
                                OutputStream out = new FileOutputStream(entryDestination);
                                IOUtils.copy(in, out);
                                IOUtils.closeQuietly(in);
                                IOUtils.closeQuietly(out);
                                in.close();
                                out.close();
                            }
                        }

                        for (CustomProgressBar bar : barList) {
                            bar.setString("");
                        }

                        zipFile.close();
                        archive.delete();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }).start();
    }

}