diff options
Diffstat (limited to 'src/ru/olamedia/asset')
-rw-r--r-- | src/ru/olamedia/asset/Asset.java | 29 | ||||
-rw-r--r-- | src/ru/olamedia/asset/AssetManager.java | 28 | ||||
-rw-r--r-- | src/ru/olamedia/asset/AssetNotFoundException.java | 22 | ||||
-rw-r--r-- | src/ru/olamedia/asset/package-info.java | 8 |
4 files changed, 87 insertions, 0 deletions
diff --git a/src/ru/olamedia/asset/Asset.java b/src/ru/olamedia/asset/Asset.java new file mode 100644 index 0000000..a420c10 --- /dev/null +++ b/src/ru/olamedia/asset/Asset.java @@ -0,0 +1,29 @@ +package ru.olamedia.asset; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +public class Asset { + protected URL url; + + public boolean inJar() { + return url.toString().startsWith("file:jar:"); + } + + public Asset(URL url) { + this.url = url; + } + + public URL getURL() { + return url; + } + + public InputStream getInputStream() throws IOException { + return url.openStream(); + } + + public String getFile() { + return url.getFile(); + } +} diff --git a/src/ru/olamedia/asset/AssetManager.java b/src/ru/olamedia/asset/AssetManager.java new file mode 100644 index 0000000..d1de11b --- /dev/null +++ b/src/ru/olamedia/asset/AssetManager.java @@ -0,0 +1,28 @@ +package ru.olamedia.asset; + +import java.net.URL; + +public class AssetManager { + + public static URL getBaseURL() { + return AssetManager.class.getResource(AssetManager.class.getSimpleName() + ".class"); + } + + public boolean inJar() { + // file:jar:c:/path/to/jar/somejar.jar! + return getBaseURL().toString().startsWith("file:jar:"); + // return getBaseURL().toString().indexOf(".jar!") > 0; + } + + public static URL getURL(String path) throws AssetNotFoundException { + URL url = AssetManager.class.getClassLoader().getResource(path); + if (null == url) { + throw new AssetNotFoundException(path); + } + return url; + } + + public static Asset getAsset(String path) throws AssetNotFoundException { + return new Asset(getURL(path)); + } +} diff --git a/src/ru/olamedia/asset/AssetNotFoundException.java b/src/ru/olamedia/asset/AssetNotFoundException.java new file mode 100644 index 0000000..29afdf0 --- /dev/null +++ b/src/ru/olamedia/asset/AssetNotFoundException.java @@ -0,0 +1,22 @@ +package ru.olamedia.asset; + +public class AssetNotFoundException extends Exception { + + public AssetNotFoundException() { + super(); + } + + public AssetNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public AssetNotFoundException(String message) { + super(message); + } + + public AssetNotFoundException(Throwable cause) { + super(cause); + } + + private static final long serialVersionUID = 2197816222986044998L; +} diff --git a/src/ru/olamedia/asset/package-info.java b/src/ru/olamedia/asset/package-info.java new file mode 100644 index 0000000..d2ed4df --- /dev/null +++ b/src/ru/olamedia/asset/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author olamedia + * + */ +package ru.olamedia.asset;
\ No newline at end of file |