Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / resourcesstorage / FilesResourcesStorage.java @ 1991

History | View | Annotate | Download (4.1 KB)

1
package org.gvsig.tools.resourcesstorage;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.io.OutputStream;
9
import java.net.MalformedURLException;
10
import java.net.URL;
11
import java.util.ArrayList;
12
import java.util.List;
13
import org.apache.commons.io.IOUtils;
14
import org.gvsig.tools.util.HasAFile;
15

    
16
/**
17
 *
18
 * @author jjdelcerro
19
 */
20
public class FilesResourcesStorage extends AbstractResourcesStorage {
21

    
22
    public static class FileResource implements ResourcesStorage.Resource, HasAFile {
23

    
24
        private class ResourceInputStream extends InputStream {
25

    
26
            @Override
27
            public int read() throws IOException {
28
                return in.read();
29
            }
30

    
31
            @Override
32
            public void close() throws IOException {
33
                FileResource.this.close();
34
            }
35
        }
36

    
37
        private class ResourceOutputStream extends OutputStream {
38

    
39
            @Override
40
            public void write(int b) throws IOException {
41
                out.write(b);
42
            }
43

    
44
            @Override
45
            public void flush() throws IOException {
46
                out.flush();
47
            }
48

    
49
            @Override
50
            public void close() throws IOException {
51
                FileResource.this.close();
52
            }
53
        }
54

    
55
        private final File resource;
56
        private FileInputStream in;
57
        private FileOutputStream out;
58

    
59
        public FileResource(File resource) {
60
            this.resource = resource;
61
        }
62

    
63
        @Override
64
        public boolean isReadOnly() {
65
            return false;
66
        }
67

    
68
        @Override
69
        public URL getURL() {
70
            try {
71
                return this.resource.toURI().toURL();
72
            } catch (MalformedURLException ex) {
73
                return null;
74
            }
75
        }
76

    
77
        @Override
78
        public File getFile() {
79
            return this.resource;
80
        }
81

    
82
        @Override
83
        public boolean exists() {
84
            return this.resource.exists();
85
        }
86

    
87
        @Override
88
        public InputStream asInputStream() throws java.io.FileNotFoundException {
89
            if (this.out != null || this.in != null) {
90
                throw new IllegalStateException("Input already open");
91
            }
92
            this.in = new FileInputStream(this.resource);
93
            return new ResourceInputStream();
94
        }
95

    
96
        @Override
97
        public OutputStream asOutputStream() throws java.io.FileNotFoundException {
98
            if (this.out != null || this.in != null) {
99
                throw new IllegalStateException("Input already open");
100
            }
101
            this.out = new FileOutputStream(this.resource);
102
            return new ResourceOutputStream();
103
        }
104

    
105
        @Override
106
        public void close() {
107
            IOUtils.closeQuietly(out);
108
            IOUtils.closeQuietly(in);
109
            this.in = null;
110
            this.out = null;
111
        }
112

    
113
        @Override
114
        public void setFile(File file) {
115
            throw new UnsupportedOperationException("Not supported yet.");
116
        }
117

    
118
    }
119

    
120
    private final List<String> resourcesPaths;
121

    
122
    public FilesResourcesStorage(String fullPathName) {
123
        this.resourcesPaths = new ArrayList<>();
124
        this.resourcesPaths.add(fullPathName);
125
    }
126

    
127
    @Override
128
    public String getSeparator() {
129
        return ".";
130
    }
131

    
132
    @Override
133
    public Resource getResource(String resourceName) {
134
        File fres;
135
        for (String pathName : resourcesPaths) {
136
            File folder = new File(pathName);
137
            if( folder.isDirectory() ) {
138
                fres = new File(folder,resourceName);
139
            } else {
140
                fres = new File(pathName + this.getSeparator() + resourceName);
141
            }
142
            if( fres.exists() ) {
143
                return new FileResource(fres);
144
            }
145
        }
146
        String pathName = this.resourcesPaths.get(0);
147
        fres = new File(pathName + this.getSeparator() + resourceName);
148
        return new FileResource(fres);
149
    }
150

    
151
    public List<String> getPaths() {
152
        return this.resourcesPaths;
153
    }
154
}