1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.utils;
19
20 import java.io.Closeable;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.Comparator;
30 import java.util.UUID;
31 import java.util.stream.Stream;
32
33 import org.apache.commons.io.FilenameUtils;
34 import org.apache.commons.lang3.StringUtils;
35 import org.apache.commons.lang3.SystemUtils;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42
43
44
45
46 public final class FileUtils {
47
48
49
50
51 private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
52
53
54
55 private static final String BIT_BUCKET_UNIX = "/dev/null";
56
57
58
59
60 private static final String BIT_BUCKET_WIN = "NUL";
61
62
63
64
65 private FileUtils() {
66 }
67
68
69
70
71
72
73
74 @Nullable
75 public static String getFileExtension(@NotNull String fileName) {
76 @Nullable
77 final String fileExt = FilenameUtils.getExtension(fileName);
78 return StringUtils.isNoneEmpty(fileExt) ? StringUtils.lowerCase(fileExt) : null;
79 }
80
81
82
83
84
85
86
87
88 public static boolean delete(@Nullable File file) {
89 if (file == null) {
90 LOGGER.warn("cannot delete null File");
91 return false;
92 }
93
94 try (Stream<Path> paths = Files.walk(file.toPath())) {
95 paths.sorted(Comparator.reverseOrder())
96 .map(Path::toFile)
97 .forEach(File::delete);
98 } catch (IOException ex) {
99 LOGGER.trace(ex.getMessage(), ex);
100 LOGGER.debug("Failed to delete file: {} (error message: {}); attempting to delete on exit.", file.getPath(), ex.getMessage());
101 file.deleteOnExit();
102 return false;
103 }
104
105 return true;
106 }
107
108
109
110
111
112
113
114
115
116 @NotNull
117 public static File createTempDirectory(@Nullable final File base) throws IOException {
118 final File tempDir = new File(base, "dctemp" + UUID.randomUUID());
119 if (tempDir.exists()) {
120 return createTempDirectory(base);
121 }
122 if (!tempDir.mkdirs()) {
123 throw new IOException("Could not create temp directory `" + tempDir.getAbsolutePath() + "`");
124 }
125 LOGGER.debug("Temporary directory is `{}`", tempDir.getAbsolutePath());
126 return tempDir;
127 }
128
129
130
131
132
133
134
135 @NotNull
136 public static String getBitBucket() {
137 return SystemUtils.IS_OS_WINDOWS ? BIT_BUCKET_WIN : BIT_BUCKET_UNIX;
138 }
139
140
141
142
143
144
145
146 public static void close(@Nullable final Closeable closeable) {
147 if (null != closeable) {
148 try {
149 closeable.close();
150 } catch (IOException ex) {
151 LOGGER.trace("", ex);
152 }
153 }
154 }
155
156
157
158
159
160
161
162
163 @Nullable
164 public static InputStream getResourceAsStream(@NotNull String resource) throws FileNotFoundException {
165 final ClassLoader classLoader = FileUtils.class.getClassLoader();
166 final InputStream inputStream = classLoader != null
167 ? classLoader.getResourceAsStream(resource)
168 : ClassLoader.getSystemResourceAsStream(resource);
169
170 if (inputStream == null) {
171 return new FileInputStream(resource);
172 }
173 return inputStream;
174 }
175
176
177
178
179
180
181
182
183 public static File getResourceAsFile(final String resource) {
184 final ClassLoader classLoader = FileUtils.class.getClassLoader();
185 String path = null;
186 if (classLoader != null) {
187 final URL url = classLoader.getResource(resource);
188 if (url != null) {
189 path = url.getFile();
190 }
191 } else {
192 path = ClassLoader.getSystemResource(resource).getFile();
193 }
194
195 if (path == null) {
196 return new File(resource);
197 }
198 return new File(path);
199 }
200 }