File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\fs\FilePath.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.store.fs;
8
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.nio.channels.FileChannel;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Map;
16 import org.h2.util.MathUtils;
17 import org.h2.util.New;
18 import org.h2.util.StringUtils;
19
20 /**
21 * A path to a file. It similar to the Java 7 <code>java.nio.file.Path</code>,
22 * but simpler, and works with older versions of Java. It also implements the
23 * relevant methods found in <code>java.nio.file.FileSystem</code> and
24 * <code>FileSystems</code>
25 */
26 public abstract class FilePath {
27
28 private static final FilePath DEFAULT = new FilePathDisk();
29
30 private static Map<String, FilePath> providers;
31
32 /**
33 * The prefix for temporary files.
34 */
35 private static String tempRandom;
36 private static long tempSequence;
37
38 /**
39 * The complete path (which may be absolute or relative, depending on the
40 * file system).
41 */
42 protected String name;
43
44 /**
45 * Get the file path object for the given path.
46 * Windows-style '\' is replaced with '/'.
47 *
48 * @param path the path
49 * @return the file path object
50 */
51 public static FilePath get(String path) {
52 path = path.replace('\\', '/');
53 int index = path.indexOf(':');
54 if (index < 2) {
55 // use the default provider if no prefix or
56 // only a single character (drive name)
57 return DEFAULT.getPath(path);
58 }
59 String scheme = path.substring(0, index);
60 registerDefaultProviders();
61 FilePath p = providers.get(scheme);
62 if (p == null) {
63 // provider not found - use the default
64 p = DEFAULT;
65 }
66 return p.getPath(path);
67 }
68
69 private static void registerDefaultProviders() {
70 if (providers == null) {
71 Map<String, FilePath> map = Collections.synchronizedMap(New.<String, FilePath>hashMap());
72 for (String c : new String[] {
73 "org.h2.store.fs.FilePathMem",
74 "org.h2.store.fs.FilePathMemLZF",
75 "org.h2.store.fs.FilePathSplit",
76 "org.h2.store.fs.FilePathNio",
77 "org.h2.store.fs.FilePathNioMapped",
78 "org.h2.store.fs.FilePathZip"
79 }) {
80 try {
81 FilePath p = (FilePath) Class.forName(c).newInstance();
82 map.put(p.getScheme(), p);
83 } catch (Exception e) {
84 // ignore - the files may be excluded in purpose
85 }
86 }
87 providers = map;
88 }
89 }
90
91 /**
92 * Register a file provider.
93 *
94 * @param provider the file provider
95 */
96 public static void register(FilePath provider) {
97 registerDefaultProviders();
98 providers.put(provider.getScheme(), provider);
99 }
100
101 /**
102 * Unregister a file provider.
103 *
104 * @param provider the file provider
105 */
106 public static void unregister(FilePath provider) {
107 registerDefaultProviders();
108 providers.remove(provider.getScheme());
109 }
110
111 /**
112 * Get the size of a file in bytes
113 *
114 * @return the size in bytes
115 */
116 public abstract long size();
117
118 /**
119 * Rename a file if this is allowed.
120 *
121 * @param newName the new fully qualified file name
122 */
123 public abstract void moveTo(FilePath newName);
124
125 /**
126 * Create a new file.
127 *
128 * @return true if creating was successful
129 */
130 public abstract boolean createFile();
131
132 /**
133 * Checks if a file exists.
134 *
135 * @return true if it exists
136 */
137 public abstract boolean exists();
138
139 /**
140 * Delete a file or directory if it exists.
141 * Directories may only be deleted if they are empty.
142 */
143 public abstract void delete();
144
145 /**
146 * List the files and directories in the given directory.
147 *
148 * @return the list of fully qualified file names
149 */
150 public abstract List<FilePath> newDirectoryStream();
151
152 /**
153 * Normalize a file name.
154 *
155 * @return the normalized file name
156 */
157 public abstract FilePath toRealPath();
158
159 /**
160 * Get the parent directory of a file or directory.
161 *
162 * @return the parent directory name
163 */
164 public abstract FilePath getParent();
165
166 /**
167 * Check if it is a file or a directory.
168 *
169 * @return true if it is a directory
170 */
171 public abstract boolean isDirectory();
172
173 /**
174 * Check if the file name includes a path.
175 *
176 * @return if the file name is absolute
177 */
178 public abstract boolean isAbsolute();
179
180 /**
181 * Get the last modified date of a file
182 *
183 * @return the last modified date
184 */
185 public abstract long lastModified();
186
187 /**
188 * Check if the file is writable.
189 *
190 * @return if the file is writable
191 */
192 public abstract boolean canWrite();
193
194 /**
195 * Create a directory (all required parent directories already exist).
196 */
197 public abstract void createDirectory();
198
199 /**
200 * Get the file or directory name (the last element of the path).
201 *
202 * @return the last element of the path
203 */
204 public String getName() {
205 int idx = Math.max(name.indexOf(':'), name.lastIndexOf('/'));
206 return idx < 0 ? name : name.substring(idx + 1);
207 }
208
209 /**
210 * Create an output stream to write into the file.
211 *
212 * @param append if true, the file will grow, if false, the file will be
213 * truncated first
214 * @return the output stream
215 */
216 public abstract OutputStream newOutputStream(boolean append);
217
218 /**
219 * Open a random access file object.
220 *
221 * @param mode the access mode. Supported are r, rw, rws, rwd
222 * @return the file object
223 */
224 public abstract FileChannel open(String mode) throws IOException;
225
226 /**
227 * Create an input stream to read from the file.
228 *
229 * @return the input stream
230 */
231 public abstract InputStream newInputStream() throws IOException;
232
233 /**
234 * Disable the ability to write.
235 *
236 * @return true if the call was successful
237 */
238 public abstract boolean setReadOnly();
239
240 /**
241 * Create a new temporary file.
242 *
243 * @param suffix the suffix
244 * @param deleteOnExit if the file should be deleted when the virtual
245 * machine exists
246 * @param inTempDir if the file should be stored in the temporary directory
247 * @return the name of the created file
248 */
249 public FilePath createTempFile(String suffix, boolean deleteOnExit, boolean inTempDir) throws IOException {
250 while (true) {
251 FilePath p = getPath(name + getNextTempFileNamePart(false) + suffix);
252 if (p.exists() || !p.createFile()) {
253 // in theory, the random number could collide
254 getNextTempFileNamePart(true);
255 continue;
256 }
257 p.open("rw").close();
258 return p;
259 }
260 }
261
262 /**
263 * Get the next temporary file name part (the part in the middle).
264 *
265 * @param newRandom if the random part of the filename should change
266 * @return the file name part
267 */
268 protected static synchronized String getNextTempFileNamePart(boolean newRandom) {
269 if (newRandom || tempRandom == null) {
270 byte[] prefix = new byte[8];
271 MathUtils.randomBytes(prefix);
272 tempRandom = StringUtils.convertBytesToHex(prefix) + ".";
273 }
274 return tempRandom + tempSequence++;
275 }
276
277 /**
278 * Get the string representation. The returned string can be used to
279 * construct a new object.
280 *
281 * @return the path as a string
282 */
283 public String toString() {
284 return name;
285 }
286
287 /**
288 * Get the scheme (prefix) for this file provider.
289 * This is similar to
290 * <code>java.nio.file.spi.FileSystemProvider.getScheme</code>.
291 *
292 * @return the scheme
293 */
294 public abstract String getScheme();
295
296 /**
297 * Convert a file to a path. This is similar to
298 * <code>java.nio.file.spi.FileSystemProvider.getPath</code>, but may
299 * return an object even if the scheme doesn't match in case of the the
300 * default file provider.
301 *
302 * @param path the path
303 * @return the file path object
304 */
305 public abstract FilePath getPath(String path);
306
307 /**
308 * Get the unwrapped file name (without wrapper prefixes if wrapping /
309 * delegating file systems are used).
310 *
311 * @return the unwrapped path
312 */
313 public FilePath unwrap() {
314 return this;
315 }
316
317 }
318