1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.store.fs; |
7 | |
8 | import java.io.IOException; |
9 | import java.io.InputStream; |
10 | import java.io.OutputStream; |
11 | import java.nio.channels.FileChannel; |
12 | import java.util.List; |
13 | |
14 | /** |
15 | * The base class for wrapping / delegating file systems such as |
16 | * the split file system. |
17 | */ |
18 | public abstract class FilePathWrapper extends FilePath { |
19 | |
20 | private FilePath base; |
21 | |
22 | @Override |
23 | public FilePathWrapper getPath(String path) { |
24 | return create(path, unwrap(path)); |
25 | } |
26 | |
27 | /** |
28 | * Create a wrapped path instance for the given base path. |
29 | * |
30 | * @param base the base path |
31 | * @return the wrapped path |
32 | */ |
33 | public FilePathWrapper wrap(FilePath base) { |
34 | return base == null ? null : create(getPrefix() + base.name, base); |
35 | } |
36 | |
37 | @Override |
38 | public FilePath unwrap() { |
39 | return unwrap(name); |
40 | } |
41 | |
42 | private FilePathWrapper create(String path, FilePath base) { |
43 | try { |
44 | FilePathWrapper p = getClass().newInstance(); |
45 | p.name = path; |
46 | p.base = base; |
47 | return p; |
48 | } catch (Exception e) { |
49 | throw new IllegalArgumentException("Path: " + path, e); |
50 | } |
51 | } |
52 | |
53 | protected String getPrefix() { |
54 | return getScheme() + ":"; |
55 | } |
56 | |
57 | /** |
58 | * Get the base path for the given wrapped path. |
59 | * |
60 | * @param path the path including the scheme prefix |
61 | * @return the base file path |
62 | */ |
63 | protected FilePath unwrap(String path) { |
64 | return FilePath.get(path.substring(getScheme().length() + 1)); |
65 | } |
66 | |
67 | protected FilePath getBase() { |
68 | return base; |
69 | } |
70 | |
71 | @Override |
72 | public boolean canWrite() { |
73 | return base.canWrite(); |
74 | } |
75 | |
76 | @Override |
77 | public void createDirectory() { |
78 | base.createDirectory(); |
79 | } |
80 | |
81 | @Override |
82 | public boolean createFile() { |
83 | return base.createFile(); |
84 | } |
85 | |
86 | @Override |
87 | public void delete() { |
88 | base.delete(); |
89 | } |
90 | |
91 | @Override |
92 | public boolean exists() { |
93 | return base.exists(); |
94 | } |
95 | |
96 | @Override |
97 | public FilePath getParent() { |
98 | return wrap(base.getParent()); |
99 | } |
100 | |
101 | @Override |
102 | public boolean isAbsolute() { |
103 | return base.isAbsolute(); |
104 | } |
105 | |
106 | @Override |
107 | public boolean isDirectory() { |
108 | return base.isDirectory(); |
109 | } |
110 | |
111 | @Override |
112 | public long lastModified() { |
113 | return base.lastModified(); |
114 | } |
115 | |
116 | @Override |
117 | public FilePath toRealPath() { |
118 | return wrap(base.toRealPath()); |
119 | } |
120 | |
121 | @Override |
122 | public List<FilePath> newDirectoryStream() { |
123 | List<FilePath> list = base.newDirectoryStream(); |
124 | for (int i = 0, len = list.size(); i < len; i++) { |
125 | list.set(i, wrap(list.get(i))); |
126 | } |
127 | return list; |
128 | } |
129 | |
130 | @Override |
131 | public void moveTo(FilePath newName, boolean atomicReplace) { |
132 | base.moveTo(((FilePathWrapper) newName).base, atomicReplace); |
133 | } |
134 | |
135 | @Override |
136 | public InputStream newInputStream() throws IOException { |
137 | return base.newInputStream(); |
138 | } |
139 | |
140 | @Override |
141 | public OutputStream newOutputStream(boolean append) throws IOException { |
142 | return base.newOutputStream(append); |
143 | } |
144 | |
145 | @Override |
146 | public FileChannel open(String mode) throws IOException { |
147 | return base.open(mode); |
148 | } |
149 | |
150 | @Override |
151 | public boolean setReadOnly() { |
152 | return base.setReadOnly(); |
153 | } |
154 | |
155 | @Override |
156 | public long size() { |
157 | return base.size(); |
158 | } |
159 | |
160 | @Override |
161 | public FilePath createTempFile(String suffix, boolean deleteOnExit, |
162 | boolean inTempDir) throws IOException { |
163 | return wrap(base.createTempFile(suffix, deleteOnExit, inTempDir)); |
164 | } |
165 | |
166 | } |