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
9
* Alexander Kurtakov (Red Hat) - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.linuxtools.internal.rpm.core.utils;
13
import java.io.BufferedInputStream;
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;
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;
32
* Eclipse job to ease downloading remote files.
35
public class DownloadJob extends Job {
37
private URLConnection content;
38
private boolean fileOverride;
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.
46
public DownloadJob(IFile file, URLConnection content, boolean override) {
47
super(NLS.bind(Messages.DownloadJob_0, file.getName()));
49
this.content = content;
50
this.fileOverride = override;
54
* Creates the download job.
55
* @param file The file to store the remote content.
56
* @param content URLConnection to the remote file.
58
public DownloadJob(IFile file, URLConnection content) {
59
this(file, content, false);
63
public IStatus run(IProgressMonitor monitor) {
65
NLS.bind(Messages.DownloadJob_0,
66
file.getName()), content.getContentLength());
68
File tempFile = File.createTempFile(file.getName(), ""); //$NON-NLS-1$
69
FileOutputStream fos = new FileOutputStream(tempFile);
70
InputStream is = new BufferedInputStream(content.getInputStream());
72
boolean canceled = false;
73
while ((b = is.read()) != -1) {
74
if (monitor.isCanceled()) {
85
file.setContents(new FileInputStream(tempFile), true,
88
file.create(new FileInputStream(tempFile), true, monitor);
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;
101
return Status.OK_STATUS;
b'\\ No newline at end of file'