-
Notifications
You must be signed in to change notification settings - Fork 244
/
FTPHelper.java
60 lines (50 loc) · 1.55 KB
/
FTPHelper.java
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
package htsjdk.tribble.util;
import htsjdk.samtools.util.ftp.FTPClient;
import htsjdk.samtools.util.ftp.FTPStream;
import htsjdk.samtools.util.ftp.FTPUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
/**
* @author jacob
* @date 2013-Apr-11
*/
public class FTPHelper implements URLHelper {
private URL url;
public FTPHelper(URL url) {
if (!url.getProtocol().toLowerCase().equals("ftp")) {
throw new IllegalArgumentException("FTPHelper can only be used with ftp protocol, not " + url.getProtocol());
}
this.url = url;
}
@Override
public URL getUrl() {
return this.url;
}
@Override
public long getContentLength() throws IOException {
return FTPUtils.getContentLength(this.url);
}
@Override
public InputStream openInputStream() throws IOException {
String file = null;
try {
file = url.toURI().getPath();
} catch (URISyntaxException e) {
throw new IOException(e);
}
FTPClient ftp = FTPUtils.connect(url.getHost(), url.getUserInfo(), null);
ftp.pasv();
ftp.retr(file);
return new FTPStream(ftp);
}
@Override
public InputStream openInputStreamForRange(long start, long end) throws IOException {
throw new UnsupportedOperationException("Cannot perform range operations over FTP");
}
@Override
public boolean exists() throws IOException {
return FTPUtils.resourceAvailable(this.url);
}
}