File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\fs\FileUtils.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.EOFException;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import java.nio.ByteBuffer;
14 import java.nio.channels.FileChannel;
15 import java.util.List;
16 import org.h2.constant.ErrorCode;
17 import org.h2.message.DbException;
18 import org.h2.util.IOUtils;
19 import org.h2.util.New;
20
21 /**
22 * This utility class contains utility functions that use the file system
23 * abstraction.
24 */
25 public class FileUtils {
26
27 /**
28 * Checks if a file exists.
29 * This method is similar to Java 7 <code>java.nio.file.Path.exists</code>.
30 *
31 * @param fileName the file name
32 * @return true if it exists
33 */
34 public static boolean exists(String fileName) {
35 return FilePath.get(fileName).exists();
36 }
37
38 /**
39 * Create a directory (all required parent directories must already exist).
40 * This method is similar to Java 7
41 * <code>java.nio.file.Path.createDirectory</code>.
42 *
43 * @param directoryName the directory name
44 */
45 public static void createDirectory(String directoryName) {
46 FilePath.get(directoryName).createDirectory();
47 }
48
49 /**
50 * Create a new file.
51 * This method is similar to Java 7
52 * <code>java.nio.file.Path.createFile</code>,
53 * but returns false instead of throwing a exception if the file already existed.
54 *
55 * @param fileName the file name
56 * @return true if creating was successful
57 */
58 public static boolean createFile(String fileName) {
59 return FilePath.get(fileName).createFile();
60 }
61
62 /**
63 * Delete a file or directory if it exists.
64 * Directories may only be deleted if they are empty.
65 * This method is similar to Java 7
66 * <code>java.nio.file.Path.deleteIfExists</code>.
67 *
68 * @param path the file or directory name
69 */
70 public static void delete(String path) {
71 FilePath.get(path).delete();
72 }
73
74 /**
75 * Get the canonical file or directory name.
76 * This method is similar to Java 7 <code>java.nio.file.Path.toRealPath</code>.
77 *
78 * @param fileName the file name
79 * @return the normalized file name
80 */
81 public static String toRealPath(String fileName) {
82 return FilePath.get(fileName).toRealPath().toString();
83 }
84
85 /**
86 * Get the parent directory of a file or directory.
87 * This method returns null if there is no parent.
88 * This method is similar to Java 7 <code>java.nio.file.Path.getParent</code>.
89 *
90 * @param fileName the file or directory name
91 * @return the parent directory name
92 */
93 public static String getParent(String fileName) {
94 FilePath p = FilePath.get(fileName).getParent();
95 return p == null ? null : p.toString();
96 }
97
98 /**
99 * Check if the file name includes a path.
100 * This method is similar to Java 7 <code>java.nio.file.Path.isAbsolute</code>.
101 *
102 * @param fileName the file name
103 * @return if the file name is absolute
104 */
105 public static boolean isAbsolute(String fileName) {
106 return FilePath.get(fileName).isAbsolute();
107 }
108
109 /**
110 * Rename a file if this is allowed.
111 * This method is similar to Java 7 <code>java.nio.file.Path.moveTo</code>.
112 *
113 * @param oldName the old fully qualified file name
114 * @param newName the new fully qualified file name
115 */
116 public static void moveTo(String oldName, String newName) {
117 FilePath.get(oldName).moveTo(FilePath.get(newName));
118 }
119
120 /**
121 * Get the file or directory name (the last element of the path).
122 * This method is similar to Java 7 <code>java.nio.file.Path.getName</code>.
123 *
124 * @param path the directory and file name
125 * @return just the file name
126 */
127 public static String getName(String path) {
128 return FilePath.get(path).getName();
129 }
130
131 /**
132 * List the files and directories in the given directory.
133 * This method is similar to Java 7
134 * <code>java.nio.file.Path.newDirectoryStream</code>.
135 *
136 * @param path the directory
137 * @return the list of fully qualified file names
138 */
139 public static List<String> newDirectoryStream(String path) {
140 List<FilePath> list = FilePath.get(path).newDirectoryStream();
141 int len = list.size();
142 List<String> result = New.arrayList(len);
143 for (int i = 0; i < len; i++) {
144 result.add(list.get(i).toString());
145 }
146 return result;
147 }
148
149 /**
150 * Get the last modified date of a file.
151 * This method is similar to Java 7
152 * <code>java.nio.file.attribute.Attributes.
153 * readBasicFileAttributes(file).lastModified().toMillis()</code>
154 *
155 * @param fileName the file name
156 * @return the last modified date
157 */
158 public static long lastModified(String fileName) {
159 return FilePath.get(fileName).lastModified();
160 }
161
162 /**
163 * Get the size of a file in bytes
164 * This method is similar to Java 7
165 * <code>java.nio.file.attribute.Attributes.
166 * readBasicFileAttributes(file).size()</code>
167 *
168 * @param fileName the file name
169 * @return the size in bytes
170 */
171 public static long size(String fileName) {
172 return FilePath.get(fileName).size();
173 }
174
175 /**
176 * Check if it is a file or a directory.
177 * <code>java.nio.file.attribute.Attributes.
178 * readBasicFileAttributes(file).isDirectory()</code>
179 *
180 * @param fileName the file or directory name
181 * @return true if it is a directory
182 */
183 public static boolean isDirectory(String fileName) {
184 return FilePath.get(fileName).isDirectory();
185 }
186
187 /**
188 * Open a random access file object.
189 * This method is similar to Java 7
190 * <code>java.nio.channels.FileChannel.open</code>.
191 *
192 * @param fileName the file name
193 * @param mode the access mode. Supported are r, rw, rws, rwd
194 * @return the file object
195 */
196 public static FileChannel open(String fileName, String mode) throws IOException {
197 return FilePath.get(fileName).open(mode);
198 }
199
200 /**
201 * Create an input stream to read from the file.
202 * This method is similar to Java 7
203 * <code>java.nio.file.Path.newInputStream</code>.
204 *
205 * @param fileName the file name
206 * @return the input stream
207 */
208 public static InputStream newInputStream(String fileName) throws IOException {
209 return FilePath.get(fileName).newInputStream();
210 }
211
212 /**
213 * Create an output stream to write into the file.
214 * This method is similar to Java 7
215 * <code>java.nio.file.Path.newOutputStream</code>.
216 *
217 * @param fileName the file name
218 * @param append if true, the file will grow, if false, the file will be
219 * truncated first
220 * @return the output stream
221 */
222 public static OutputStream newOutputStream(String fileName, boolean append) {
223 return FilePath.get(fileName).newOutputStream(append);
224 }
225
226 /**
227 * Check if the file is writable.
228 * This method is similar to Java 7
229 * <code>java.nio.file.Path.checkAccess(AccessMode.WRITE)</code>
230 *
231 * @param fileName the file name
232 * @return if the file is writable
233 */
234 public static boolean canWrite(String fileName) {
235 return FilePath.get(fileName).canWrite();
236 }
237
238 // special methods =======================================
239
240 /**
241 * Disable the ability to write. The file can still be deleted afterwards.
242 *
243 * @param fileName the file name
244 * @return true if the call was successful
245 */
246 public static boolean setReadOnly(String fileName) {
247 return FilePath.get(fileName).setReadOnly();
248 }
249
250 /**
251 * Get the unwrapped file name (without wrapper prefixes if wrapping /
252 * delegating file systems are used).
253 *
254 * @param fileName the file name
255 * @return the unwrapped
256 */
257 public static String unwrap(String fileName) {
258 return FilePath.get(fileName).unwrap().toString();
259 }
260
261 // utility methods =======================================
262
263 /**
264 * Delete a directory or file and all subdirectories and files.
265 *
266 * @param path the path
267 * @param tryOnly whether errors should be ignored
268 */
269 public static void deleteRecursive(String path, boolean tryOnly) {
270 if (exists(path)) {
271 if (isDirectory(path)) {
272 for (String s : newDirectoryStream(path)) {
273 deleteRecursive(s, tryOnly);
274 }
275 }
276 if (tryOnly) {
277 tryDelete(path);
278 } else {
279 delete(path);
280 }
281 }
282 }
283
284 /**
285 * Create the directory and all required parent directories.
286 *
287 * @param dir the directory name
288 */
289 public static void createDirectories(String dir) {
290 if (dir != null) {
291 if (exists(dir)) {
292 if (!isDirectory(dir)) {
293 DbException.get(ErrorCode.FILE_CREATION_FAILED_1,
294 "Could not create directory, " +
295 "because a file with the same name already exists: " + dir);
296 }
297 } else {
298 String parent = getParent(dir);
299 createDirectories(parent);
300 createDirectory(dir);
301 }
302 }
303 }
304
305 /**
306 * Copy a file from one directory to another, or to another file.
307 *
308 * @param original the original file name
309 * @param copy the file name of the copy
310 */
311 public static void copy(String original, String copy) throws IOException {
312 InputStream in = newInputStream(original);
313 OutputStream out = newOutputStream(copy, false);
314 IOUtils.copyAndClose(in, out);
315 }
316
317 /**
318 * Try to delete a file (ignore errors).
319 *
320 * @param fileName the file name
321 * @return true if it worked
322 */
323 public static boolean tryDelete(String fileName) {
324 try {
325 FilePath.get(fileName).delete();
326 return true;
327 } catch (Exception e) {
328 return false;
329 }
330 }
331
332 /**
333 * Create a new temporary file.
334 *
335 * @param prefix the prefix of the file name (including directory name if
336 * required)
337 * @param suffix the suffix
338 * @param deleteOnExit if the file should be deleted when the virtual
339 * machine exists
340 * @param inTempDir if the file should be stored in the temporary directory
341 * @return the name of the created file
342 */
343 public static String createTempFile(String prefix, String suffix, boolean deleteOnExit, boolean inTempDir)
344 throws IOException {
345 return FilePath.get(prefix).createTempFile(suffix, deleteOnExit, inTempDir).toString();
346 }
347
348 /**
349 * Fully read from the file. This will read all remaining bytes,
350 * or throw an EOFException if not successful.
351 *
352 * @param channel the file channel
353 * @param dst the byte buffer
354 */
355 public static void readFully(FileChannel channel, ByteBuffer dst) throws IOException {
356 do {
357 int r = channel.read(dst);
358 if (r < 0) {
359 throw new EOFException();
360 }
361 } while (dst.remaining() > 0);
362 }
363
364 /**
365 * Fully write to the file. This will write all remaining bytes.
366 *
367 * @param channel the file channel
368 * @param src the byte buffer
369 */
370 public static void writeFully(FileChannel channel, ByteBuffer src) throws IOException {
371 do {
372 channel.write(src);
373 } while (src.remaining() > 0);
374 }
375
376 }
377