1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
/*******************************************************************************
* Copyright (c) 2004 - 2006 Mylar committers and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.mylar.tasks.core;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.mylar.core.MylarStatusHandler;
/**
* @author Steffen Pingel
*/
public abstract class AbstractAttachmentHandler implements IAttachmentHandler {
protected static final int BUFFER_SIZE = 1024;
public void downloadAttachment(TaskRepository repository, RepositoryAttachment attachment, OutputStream out,
IProgressMonitor monitor) throws CoreException {
monitor.beginTask("Downloading attachment", IProgressMonitor.UNKNOWN);
try {
InputStream in = new BufferedInputStream(getAttachmentAsStream(repository, attachment, new SubProgressMonitor(monitor, 1)));
try {
byte[] buffer = new byte[BUFFER_SIZE];
while (true) {
int count = in.read(buffer);
if (count == -1) {
return;
}
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
out.write(buffer, 0, count);
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
}
} catch (IOException e) {
throw new CoreException(RepositoryStatus.createStatus(repository, IStatus.ERROR, "org.eclipse.mylar.tasks.core", "IO error reading attachment: " + e.getMessage()));
} finally {
try {
in.close();
} catch (IOException e) {
MylarStatusHandler.fail(e, "Error closing attachment stream", false);
}
}
} finally {
monitor.done();
}
}
}
|