1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.start;
20
21 import java.io.Closeable;
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.FileSystems;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.attribute.FileTime;
28 import java.util.Locale;
29
30 public class FS
31 {
32 public static boolean canReadDirectory(Path path)
33 {
34 return Files.exists(path) && Files.isDirectory(path) && Files.isReadable(path);
35 }
36
37 public static boolean canReadFile(Path path)
38 {
39 return Files.exists(path) && Files.isRegularFile(path) && Files.isReadable(path);
40 }
41
42 public static boolean canWrite(Path path)
43 {
44 return Files.isWritable(path);
45 }
46
47 public static void close(Closeable c)
48 {
49 if (c == null)
50 {
51 return;
52 }
53
54 try
55 {
56 c.close();
57 }
58 catch (IOException ignore)
59 {
60
61 }
62 }
63
64 public static boolean createNewFile(Path path) throws IOException
65 {
66 Path ret = Files.createFile(path);
67 return Files.exists(ret);
68 }
69
70 public static void ensureDirectoryExists(Path dir) throws IOException
71 {
72 if (exists(dir))
73 {
74
75 return;
76 }
77 Files.createDirectories(dir);
78 }
79
80 public static void ensureDirectoryWritable(Path dir) throws IOException
81 {
82 if (!Files.exists(dir))
83 {
84 throw new IOException("Path does not exist: " + dir.toAbsolutePath());
85 }
86 if (!Files.isDirectory(dir))
87 {
88 throw new IOException("Directory does not exist: " + dir.toAbsolutePath());
89 }
90 if (!Files.isWritable(dir))
91 {
92 throw new IOException("Unable to write to directory: " + dir.toAbsolutePath());
93 }
94 }
95
96 public static boolean exists(Path path)
97 {
98 return Files.exists(path);
99 }
100
101 public static boolean isValidDirectory(Path path)
102 {
103 if (!Files.exists(path))
104 {
105
106 return false;
107 }
108
109 if (!Files.isDirectory(path))
110 {
111
112 StartLog.warn("Not a directory: " + path);
113 return false;
114 }
115
116 return true;
117 }
118
119 public static boolean isXml(String filename)
120 {
121 return filename.toLowerCase(Locale.ENGLISH).endsWith(".xml");
122 }
123
124 public static String toRelativePath(File baseDir, File path)
125 {
126 return baseDir.toURI().relativize(path.toURI()).toASCIIString();
127 }
128
129 public static boolean isPropertyFile(String filename)
130 {
131 return filename.toLowerCase(Locale.ENGLISH).endsWith(".properties");
132 }
133
134 public static String separators(String path)
135 {
136 StringBuilder ret = new StringBuilder();
137 for (char c : path.toCharArray())
138 {
139 if ((c == '/') || (c == '\\'))
140 {
141 ret.append(File.separatorChar);
142 }
143 else
144 {
145 ret.append(c);
146 }
147 }
148 return ret.toString();
149 }
150
151 public static Path toPath(String path)
152 {
153 return FileSystems.getDefault().getPath(FS.separators(path));
154 }
155
156 public static void touch(Path path) throws IOException
157 {
158 FileTime now = FileTime.fromMillis(System.currentTimeMillis());
159 Files.setLastModifiedTime(path,now);
160 }
161
162 public static Path toRealPath(Path path) throws IOException
163 {
164 return path.toRealPath();
165 }
166 }