From 991420c6b1ebc1f041818ffeaee65e5dcaa1581d Mon Sep 17 00:00:00 2001 From: Andrej Shadura Date: Wed, 26 Dec 2018 20:22:36 +0100 Subject: Import Upstream version 1.9.0+dfsg --- .../main/java/morfologik/util/ResourceUtils.java | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 morfologik-fsa/src/main/java/morfologik/util/ResourceUtils.java (limited to 'morfologik-fsa/src/main/java/morfologik/util/ResourceUtils.java') diff --git a/morfologik-fsa/src/main/java/morfologik/util/ResourceUtils.java b/morfologik-fsa/src/main/java/morfologik/util/ResourceUtils.java new file mode 100644 index 0000000..2c7bd23 --- /dev/null +++ b/morfologik-fsa/src/main/java/morfologik/util/ResourceUtils.java @@ -0,0 +1,58 @@ +package morfologik.util; + +import java.io.*; +import java.net.*; + +/** + * Resource management utilities. + */ +public final class ResourceUtils { + /** + * No instances. + */ + private ResourceUtils() { + } + + /** + * Returns an input stream to the resource. + * + * @param resource + * The path leading to the resource. Can be an URL, a path + * leading to a class resource or a {@link File}. + * + * @return InputStream instance. + * @throws IOException + * If the resource could not be found or opened. + */ + public static InputStream openInputStream(String resource) + throws IOException { + try { + // See if the resource is an URL first. + final URL url = new URL(resource); + // success, load the resource. + return url.openStream(); + } catch (MalformedURLException e) { + // No luck. Fallback to class loader paths. + } + + // Try current thread's class loader first. + final ClassLoader ldr = Thread.currentThread().getContextClassLoader(); + + InputStream is; + if (ldr != null && (is = ldr.getResourceAsStream(resource)) != null) { + return is; + } else if ((is = ResourceUtils.class.getResourceAsStream(resource)) != null) { + return is; + } else if ((is = ClassLoader.getSystemResourceAsStream(resource)) != null) { + return is; + } + + // Try file path + final File f = new File(resource); + if (f.exists() && f.isFile() && f.canRead()) { + return new FileInputStream(f); + } + + throw new IOException("Could not locate resource: " + resource); + } +} -- cgit v1.2.3