1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.util;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.UnsupportedEncodingException;
22 import java.net.MalformedURLException;
23 import java.net.URI;
24 import java.net.URL;
25 import java.net.URLDecoder;
26 import java.nio.charset.StandardCharsets;
27 import java.util.Objects;
28
29 import org.apache.logging.log4j.Logger;
30 import org.apache.logging.log4j.status.StatusLogger;
31
32
33
34
35 public final class FileUtils {
36
37
38 private static final String PROTOCOL_FILE = "file";
39
40 private static final String JBOSS_FILE = "vfsfile";
41
42 private static final Logger LOGGER = StatusLogger.getLogger();
43
44 private FileUtils() {
45 }
46
47
48
49
50
51
52
53 public static File fileFromUri(URI uri) {
54
55 if (uri == null
56 || (uri.getScheme() != null && (!PROTOCOL_FILE.equals(uri.getScheme()) && !JBOSS_FILE.equals(uri
57 .getScheme())))) {
58 return null;
59 }
60 if (uri.getScheme() == null) {
61 File file = new File(uri.toString());
62 if (file.exists()) {
63 return file;
64 }
65 try {
66 final String path = uri.getPath();
67 file = new File(path);
68 if (file.exists()) {
69 return file;
70 }
71 uri = new File(path).toURI();
72 } catch (final Exception ex) {
73 LOGGER.warn("Invalid URI {}", uri);
74 return null;
75 }
76 }
77 final String charsetName = StandardCharsets.UTF_8.name();
78 try {
79 String fileName = uri.toURL().getFile();
80 if (new File(fileName).exists()) {
81 return new File(fileName);
82 }
83 fileName = URLDecoder.decode(fileName, charsetName);
84 return new File(fileName);
85 } catch (final MalformedURLException ex) {
86 LOGGER.warn("Invalid URL {}", uri, ex);
87 } catch (final UnsupportedEncodingException uee) {
88 LOGGER.warn("Invalid encoding: {}", charsetName, uee);
89 }
90 return null;
91 }
92
93 public static boolean isFile(final URL url) {
94 return url != null && (url.getProtocol().equals(PROTOCOL_FILE) || url.getProtocol().equals(JBOSS_FILE));
95 }
96
97 public static String getFileExtension(final File file) {
98 final String fileName = file.getName();
99 if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
100 return fileName.substring(fileName.lastIndexOf(".") + 1);
101 }
102 return null;
103 }
104
105
106
107
108
109
110
111
112 public static void mkdir(final File dir, final boolean createDirectoryIfNotExisting) throws IOException {
113
114 if (!dir.exists()) {
115 if (!createDirectoryIfNotExisting) {
116 throw new IOException("The directory " + dir.getAbsolutePath() + " does not exist.");
117 }
118 if (!dir.mkdirs()) {
119 throw new IOException("Could not create directory " + dir.getAbsolutePath());
120 }
121 }
122 if (!dir.isDirectory()) {
123 throw new IOException("File " + dir + " exists and is not a directory. Unable to create directory.");
124 }
125 }
126
127
128
129
130
131
132
133 public static void makeParentDirs(final File file) throws IOException {
134 File parent = Objects.requireNonNull(file, "file").getCanonicalFile().getParentFile();
135 if (parent != null) {
136 mkdir(parent, true);
137 }
138 }
139
140 }