~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to rpm/org.eclipse.linuxtools.rpm.core/src/org/eclipse/linuxtools/internal/rpm/core/utils/DownloadJob.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2010 Red Hat Inc. and others.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *      Alexander Kurtakov (Red Hat) - initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.linuxtools.internal.rpm.core.utils;
 
12
 
 
13
import java.io.BufferedInputStream;
 
14
import java.io.File;
 
15
import java.io.FileInputStream;
 
16
import java.io.FileOutputStream;
 
17
import java.io.IOException;
 
18
import java.io.InputStream;
 
19
import java.net.URLConnection;
 
20
 
 
21
import org.eclipse.core.resources.IFile;
 
22
import org.eclipse.core.runtime.CoreException;
 
23
import org.eclipse.core.runtime.IProgressMonitor;
 
24
import org.eclipse.core.runtime.IStatus;
 
25
import org.eclipse.core.runtime.Status;
 
26
import org.eclipse.core.runtime.jobs.Job;
 
27
import org.eclipse.linuxtools.internal.rpm.core.RPMCorePlugin;
 
28
import org.eclipse.linuxtools.rpm.core.IRPMConstants;
 
29
import org.eclipse.osgi.util.NLS;
 
30
 
 
31
/**
 
32
 * Eclipse job to ease downloading remote files.
 
33
 *
 
34
 */
 
35
public class DownloadJob extends Job {
 
36
        private IFile file;
 
37
        private URLConnection content;
 
38
        private boolean fileOverride;
 
39
 
 
40
        /**
 
41
         * Creates the download job.
 
42
         * @param file The file to store the remote content.
 
43
         * @param content The URLConnection to the remote file.
 
44
         * @param override Flag to override file if it exists.
 
45
         */
 
46
        public DownloadJob(IFile file, URLConnection content, boolean override) {
 
47
                super(NLS.bind(Messages.DownloadJob_0, file.getName()));
 
48
                this.file = file;
 
49
                this.content = content;
 
50
                this.fileOverride = override;
 
51
        }
 
52
 
 
53
        /**
 
54
         * Creates the download job.
 
55
         * @param file The file to store the remote content.
 
56
         * @param content URLConnection to the remote file.
 
57
         */
 
58
        public DownloadJob(IFile file, URLConnection content) {
 
59
                this(file, content, false);
 
60
        }
 
61
 
 
62
        @Override
 
63
        public IStatus run(IProgressMonitor monitor) {
 
64
                monitor.beginTask(
 
65
                                NLS.bind(Messages.DownloadJob_0,
 
66
                                                file.getName()), content.getContentLength());
 
67
                try {
 
68
                        File tempFile = File.createTempFile(file.getName(), ""); //$NON-NLS-1$
 
69
                        FileOutputStream fos = new FileOutputStream(tempFile);
 
70
                        InputStream is = new BufferedInputStream(content.getInputStream());
 
71
                        int b;
 
72
                        boolean canceled = false;
 
73
                        while ((b = is.read()) != -1) {
 
74
                                if (monitor.isCanceled()) {
 
75
                                        canceled = true;
 
76
                                        break;
 
77
                                }
 
78
                                fos.write(b);
 
79
                                monitor.worked(1);
 
80
                        }
 
81
                        is.close();
 
82
                        fos.close();
 
83
                        if (!canceled) {
 
84
                                if (fileOverride) {
 
85
                                        file.setContents(new FileInputStream(tempFile), true,
 
86
                                                        false, monitor);
 
87
                                } else {
 
88
                                        file.create(new FileInputStream(tempFile), true, monitor);
 
89
 
 
90
                                }
 
91
                        }
 
92
                        tempFile.delete();
 
93
                } catch (CoreException e) {
 
94
                        RPMCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR, IRPMConstants.RPM_CORE_ID, e.getMessage(), e));
 
95
                        return Status.CANCEL_STATUS;
 
96
                } catch (IOException e) {
 
97
                        RPMCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR, IRPMConstants.RPM_CORE_ID, e.getMessage(), e));
 
98
                        return Status.CANCEL_STATUS;
 
99
                }
 
100
                monitor.done();
 
101
                return Status.OK_STATUS;
 
102
        }
 
103
}
 
 
b'\\ No newline at end of file'