Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / resourcesstorage / URLResource.java @ 1993

History | View | Annotate | Download (1.33 KB)

1
package org.gvsig.tools.resourcesstorage;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.OutputStream;
6
import java.net.URL;
7
import org.apache.commons.io.IOUtils;
8

    
9
/**
10
 *
11
 * @author jjdelcerro
12
 */
13
@SuppressWarnings("UseSpecificCatch")
14
public class URLResource implements ResourcesStorage.Resource {
15

    
16
    private final URL url;
17
    private InputStream is;
18
    
19
    public URLResource(URL url) {
20
        this.url = url;
21
        this.is = null;
22
    }
23
    
24
    @Override
25
    public boolean isReadOnly() {
26
        return true;
27
    }
28

    
29
    @Override
30
    public URL getURL() {
31
        return this.url;
32
    }
33

    
34
    @Override
35
    public boolean exists() {
36
        if( this.is!=null ) {
37
            return true;
38
        }
39
        try {
40
            this.is = this.url.openStream();
41
            return true;
42
        } catch (Exception ex) {
43
            return false;
44
        }
45
    }
46

    
47
    @Override
48
    public InputStream asInputStream() throws IOException {
49
        if( this.is!=null ) {
50
            throw new RuntimeException("Stream already open");
51
        }
52
        this.is = this.url.openStream();
53
        return this.is;
54
    }
55

    
56
    @Override
57
    public OutputStream asOutputStream() throws IOException {
58
        return null;
59
    }
60

    
61
    @Override
62
    public void close() {
63
        IOUtils.closeQuietly(this.is);
64
        this.is = null;
65
    }
66
    
67
}