Gridarta Editor
IOUtils.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
22 import java.io.BufferedInputStream;
23 import java.io.File;
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;
30 import java.net.URI;
31 import java.net.URL;
32 import java.util.regex.Pattern;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
40 public class IOUtils {
41 
51  @NotNull
52  public static final String MAP_ENCODING = "iso-8859-1";
53 
57  private IOUtils() {
58  }
59 
68  @NotNull
69  public static URL getResource(@NotNull final File dir, @NotNull final String fileName) throws FileNotFoundException {
70  try { // 1st try normal file relative to specified directory
71  final File file = new File(dir, fileName);
72  if (file.exists()) {
73  return file.toURI().toURL();
74  }
75  } catch (final MalformedURLException ignored) {
76  // ignore
77  }
78  try { // 2nd try binary file relative to user's current working directory
79  final File file = new File(fileName);
80  if (file.exists()) {
81  return file.toURI().toURL();
82  }
83  } catch (final MalformedURLException ignored) {
84  // ignore
85  }
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();
89  // 3rd try context class loader relative to specified directory
90  @Nullable final URL url1 = contextClassLoader.getResource(relWithDir);
91  if (url1 != null) {
92  return url1;
93  }
94  final String relPlain = currentDir.relativize(new File(fileName).toURI()).toString();
95  // 4th try context class loader relative to class loader's top level directory
96  @Nullable final URL url2 = contextClassLoader.getResource(relPlain);
97  if (url2 != null) {
98  return url2;
99  }
100  // 5th try system class loader relative to specified directory
101  @Nullable final URL url3 = ClassLoader.getSystemResource(relWithDir);
102  if (url3 != null) {
103  return url3;
104  }
105  // 6th try system class loader relative to class loader's top level directory
106  @Nullable final URL url4 = ClassLoader.getSystemResource(relPlain);
107  if (url4 != null) {
108  return url4;
109  }
110  throw new FileNotFoundException("couldn't find '" + new File(fileName) + "'.");
111  }
112 
124  @NotNull
125  public static File getFile(@NotNull final File dir, @NotNull final String fileName) throws IOException {
126  final URL url = getResource(dir, fileName);
127  final String urlString = url.toString();
128  if (urlString.startsWith("file:")) {
129  final File file = new File(urlString.substring(5));
130  if (file.exists()) {
131  return file;
132  }
133  }
134 
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];
141  while (true) {
142  final int len = bufferedInputStream.read(buf);
143  if (len == -1) {
144  break;
145  }
146 
147  outputStream.write(buf, 0, len);
148  }
149  }
150  }
151  }
152  return tmpFile;
153  }
154 
162  @NotNull
163  public static File findPathFile(@NotNull final String name) throws IOException {
164  final File absoluteFile = new File(name);
165  if (absoluteFile.isAbsolute()) {
166  return absoluteFile;
167  }
168 
169  final String pathSpec = System.getenv("PATH");
170  if (pathSpec == null) {
171  throw new IOException("environment variable PATH is undefined");
172  }
173 
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);
178  if (file.exists()) {
179  return file;
180  }
181  }
182  throw new IOException("'" + name + "' not found in " + pathSpec);
183  }
184 
191  @NotNull
192  public static String getCanonicalPath(@NotNull final File file) {
193  String path;
194  try {
195  path = file.getCanonicalPath();
196  } catch (final IOException ignored) {
197  path = file.getAbsolutePath();
198  }
199  return path.replace(File.separatorChar, '/');
200  }
201 
208  @NotNull
209  public static File getCanonicalFile(@NotNull final File file) {
210  try {
211  return file.getCanonicalFile();
212  } catch (final IOException ignored) {
213  return file.getAbsoluteFile();
214  }
215  }
216 
217 }
net.sf.gridarta.utils.IOUtils.getResource
static URL getResource(@NotNull final File dir, @NotNull final String fileName)
Get the URL of a resource.
Definition: IOUtils.java:69
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.utils.IOUtils.IOUtils
IOUtils()
Utility class - do not instantiate.
Definition: IOUtils.java:57
net.sf.gridarta.utils.IOUtils.getFile
static File getFile(@NotNull final File dir, @NotNull final String fileName)
Returns a File instance for a resource that is a regular file on the file system.
Definition: IOUtils.java:125
net.sf.gridarta.utils.IOUtils.findPathFile
static File findPathFile(@NotNull final String name)
Searches for.
Definition: IOUtils.java:163
net.sf.gridarta.utils.IOUtils.getCanonicalFile
static File getCanonicalFile(@NotNull final File file)
Calls File#getCanonicalFile().
Definition: IOUtils.java:209
net.sf.gridarta.utils.IOUtils
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
net.sf.gridarta.utils.IOUtils.getCanonicalPath
static String getCanonicalPath(@NotNull final File file)
Calls File#getCanonicalPath().
Definition: IOUtils.java:192
net.sf.gridarta.utils.IOUtils.MAP_ENCODING
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:52