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-2015 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  public static final String MAP_ENCODING = "iso-8859-1";
52 
56  private IOUtils() {
57  }
58 
67  @NotNull
68  public static URL getResource(@Nullable final File dir, @NotNull final String fileName) throws FileNotFoundException {
69  try { // 1st try normal file relative to specified directory
70  final File file = new File(dir, fileName);
71  if (file.exists()) {
72  return file.toURI().toURL();
73  }
74  } catch (final MalformedURLException ignored) {
75  // ignore
76  }
77  try { // 2nd try binary file relative to user's current working directory
78  final File file = new File(fileName);
79  if (file.exists()) {
80  return file.toURI().toURL();
81  }
82  } catch (final MalformedURLException ignored) {
83  // ignore
84  }
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();
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  // 4th try context class loader relative to class loader's top level directory
95  @Nullable final URL url2 = contextClassLoader.getResource(relPlain);
96  if (url2 != null) {
97  return url2;
98  }
99  // 5th try system class loader relative to specified directory
100  @Nullable final URL url3 = ClassLoader.getSystemResource(relWithDir);
101  if (url3 != null) {
102  return url3;
103  }
104  // 6th try system class loader relative to class loader's top level directory
105  @Nullable final URL url4 = ClassLoader.getSystemResource(relPlain);
106  if (url4 != null) {
107  return url4;
108  }
109  throw new FileNotFoundException("couldn't find '" + new File(fileName) + "'.");
110  }
111 
123  @NotNull
124  public static File getFile(@Nullable final File dir, @NotNull final String fileName) throws IOException {
125  final URL url = getResource(dir, fileName);
126  final String urlString = url.toString();
127  if (urlString.startsWith("file:")) {
128  final File file = new File(urlString.substring(5));
129  if (file.exists()) {
130  return file;
131  }
132  }
133 
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];
140  while (true) {
141  final int len = bufferedInputStream.read(buf);
142  if (len == -1) {
143  break;
144  }
145 
146  outputStream.write(buf, 0, len);
147  }
148  }
149  }
150  }
151  return tmpFile;
152  }
153 
161  @NotNull
162  public static File findPathFile(@NotNull final String name) throws IOException {
163  final File absoluteFile = new File(name);
164  if (absoluteFile.isAbsolute()) {
165  return absoluteFile;
166  }
167 
168  final String pathSpec = System.getenv("PATH");
169  if (pathSpec == null) {
170  throw new IOException("environment variable PATH is undefined");
171  }
172 
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);
177  if (file.exists()) {
178  return file;
179  }
180  }
181  throw new IOException("'" + name + "' not found in " + pathSpec);
182  }
183 
190  @NotNull
191  public static String getCanonicalPath(@NotNull final File file) {
192  String path;
193  try {
194  path = file.getCanonicalPath();
195  } catch (final IOException ignored) {
196  path = file.getAbsolutePath();
197  }
198  return path.replace(File.separatorChar, '/');
199  }
200 
207  @NotNull
208  public static File getCanonicalFile(@NotNull final File file) {
209  try {
210  return file.getCanonicalFile();
211  } catch (final IOException ignored) {
212  return file.getAbsoluteFile();
213  }
214  }
215 
216 }
static File findPathFile(@NotNull final String name)
Searches for.
Definition: IOUtils.java:162
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:51
IOUtils()
Utility class - do not instantiate.
Definition: IOUtils.java:56
static File getCanonicalFile(@NotNull final File file)
Calls File#getCanonicalFile().
Definition: IOUtils.java:208
static String getCanonicalPath(@NotNull final File file)
Calls File#getCanonicalPath().
Definition: IOUtils.java:191
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
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. ...
Definition: IOUtils.java:124
static URL getResource(@Nullable final File dir, @NotNull final String fileName)
Get the URL of a resource.
Definition: IOUtils.java:68