20 package net.sf.gridarta.utils;
22 import java.io.BufferedInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.net.MalformedURLException;
32 import java.util.regex.Pattern;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
69 public static URL
getResource(@NotNull
final File dir, @NotNull
final String fileName)
throws FileNotFoundException {
71 final File file =
new File(dir, fileName);
73 return file.toURI().toURL();
75 }
catch (
final MalformedURLException ignored) {
79 final File file =
new File(fileName);
81 return file.toURI().toURL();
83 }
catch (
final MalformedURLException ignored) {
86 final URI currentDir =
new File(System.getProperty(
"user.dir")).toURI();
87 final String relWithDir = currentDir.relativize(
new File(dir, fileName).toURI()).toString();
88 final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
90 @Nullable
final URL url1 = contextClassLoader.getResource(relWithDir);
94 final String relPlain = currentDir.relativize(
new File(fileName).toURI()).toString();
96 @Nullable
final URL url2 = contextClassLoader.getResource(relPlain);
101 @Nullable
final URL url3 = ClassLoader.getSystemResource(relWithDir);
106 @Nullable
final URL url4 = ClassLoader.getSystemResource(relPlain);
110 throw new FileNotFoundException(
"couldn't find '" +
new File(fileName) +
"'.");
125 public static File
getFile(@NotNull
final File dir, @NotNull
final String fileName)
throws IOException {
127 final String urlString = url.toString();
128 if (urlString.startsWith(
"file:")) {
129 final File file =
new File(urlString.substring(5));
135 final File tmpFile = File.createTempFile(
"gridarta",
null);
136 tmpFile.deleteOnExit();
137 try (InputStream inputStream = url.openStream()) {
138 try (InputStream bufferedInputStream =
new BufferedInputStream(inputStream)) {
139 try (OutputStream outputStream =
new FileOutputStream(tmpFile)) {
140 final byte[] buf =
new byte[65536];
142 final int len = bufferedInputStream.read(buf);
147 outputStream.write(buf, 0, len);
164 final File absoluteFile =
new File(
name);
165 if (absoluteFile.isAbsolute()) {
169 final String pathSpec = System.getenv(
"PATH");
170 if (pathSpec ==
null) {
171 throw new IOException(
"environment variable PATH is undefined");
174 final String[] tmp = pathSpec.split(Pattern.quote(File.pathSeparator), -1);
175 for (
final String path : tmp) {
176 final File dir =
new File(path.isEmpty() ?
"." : path);
177 final File file =
new File(dir,
name);
182 throw new IOException(
"'" +
name +
"' not found in " + pathSpec);
195 path = file.getCanonicalPath();
196 }
catch (
final IOException ignored) {
197 path = file.getAbsolutePath();
199 return path.replace(File.separatorChar,
'/');
211 return file.getCanonicalFile();
212 }
catch (
final IOException ignored) {
213 return file.getAbsoluteFile();