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;
68 public static URL
getResource(@Nullable
final File dir, @NotNull
final String fileName)
throws FileNotFoundException {
70 final File file =
new File(dir, fileName);
72 return file.toURI().toURL();
74 }
catch (
final MalformedURLException ignored) {
78 final File file =
new File(fileName);
80 return file.toURI().toURL();
82 }
catch (
final MalformedURLException ignored) {
85 final URI currentDir =
new File(System.getProperty(
"user.dir")).toURI();
86 final String relWithDir = currentDir.relativize(
new File(dir, fileName).toURI()).toString();
87 final String relPlain = currentDir.relativize(
new File(fileName).toURI()).toString();
88 final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
90 @Nullable
final URL url1 = contextClassLoader.getResource(relWithDir);
95 @Nullable
final URL url2 = contextClassLoader.getResource(relPlain);
100 @Nullable
final URL url3 = ClassLoader.getSystemResource(relWithDir);
105 @Nullable
final URL url4 = ClassLoader.getSystemResource(relPlain);
109 throw new FileNotFoundException(
"couldn't find '" +
new File(fileName) +
"'.");
124 public static File
getFile(@Nullable
final File dir, @NotNull
final String fileName)
throws IOException {
126 final String urlString = url.toString();
127 if (urlString.startsWith(
"file:")) {
128 final File file =
new File(urlString.substring(5));
134 final File tmpFile = File.createTempFile(
"gridarta", null);
135 tmpFile.deleteOnExit();
136 try (InputStream inputStream = url.openStream()) {
137 try (InputStream bufferedInputStream =
new BufferedInputStream(inputStream)) {
138 try (OutputStream outputStream =
new FileOutputStream(tmpFile)) {
139 final byte[] buf =
new byte[65536];
141 final int len = bufferedInputStream.read(buf);
146 outputStream.write(buf, 0, len);
162 public static File
findPathFile(@NotNull
final String name)
throws IOException {
163 final File absoluteFile =
new File(name);
164 if (absoluteFile.isAbsolute()) {
168 final String pathSpec = System.getenv(
"PATH");
169 if (pathSpec == null) {
170 throw new IOException(
"environment variable PATH is undefined");
173 final String[] tmp = pathSpec.split(Pattern.quote(File.pathSeparator), -1);
174 for (
final String path : tmp) {
175 final File dir =
new File(path.isEmpty() ?
"." : path);
176 final File file =
new File(dir, name);
181 throw new IOException(
"'" + name +
"' not found in " + pathSpec);
194 path = file.getCanonicalPath();
195 }
catch (
final IOException ignored) {
196 path = file.getAbsolutePath();
198 return path.replace(File.separatorChar,
'/');
210 return file.getCanonicalFile();
211 }
catch (
final IOException ignored) {
212 return file.getAbsoluteFile();
static File findPathFile(@NotNull final String name)
Searches for.
static final String MAP_ENCODING
Encoding to use for maps and other data.
IOUtils()
Utility class - do not instantiate.
static File getCanonicalFile(@NotNull final File file)
Calls File#getCanonicalFile().
static String getCanonicalPath(@NotNull final File file)
Calls File#getCanonicalPath().
Utility-class for Gridarta's I/O.
static File getFile(@Nullable final File dir, @NotNull final String fileName)
Returns a File instance for a resource that is a regular file on the file system. ...
static URL getResource(@Nullable final File dir, @NotNull final String fileName)
Get the URL of a resource.