Skip to content

Commit

Permalink
ImageAPI: added methods to get renderer from jars
Browse files Browse the repository at this point in the history
  • Loading branch information
SrRapero720 committed Dec 30, 2024
1 parent dfcf89c commit d66d0bd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
34 changes: 32 additions & 2 deletions src/main/java/org/watermedia/api/image/ImageAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static ImageCache createCache(ImageRenderer renderer) {

/**
* Creates a custom ImageCache instance
* WARNING: this instnace is NOT catched internally, this requires your own catching system
* WARNING: this instance is NOT cached internally, this requires your own caching system
* @param image BufferedImage instance to store in the Renderer
* @param absolute if the renderer should be released running {@link ImageRenderer#release()} nor {@link ImageRenderer#flush()}
* @return ImageCache instance with the renderer and no fetch
Expand All @@ -129,6 +129,36 @@ public static void reloadCache() {
for (ImageCache imageCache: ImageCache.CACHE.values()) { imageCache.reload(); }
}

/**
* Creates an instance of an ImageRenderer only for static pictures
* It reads the specified path located in your jar such as "pictures/image.png".
* Throws an exception if it can't read it
* @param jarPath path of the picture
* @param classLoader classloader of your jar, some module system uses isolated classloaders
* @return ImageRenderer instance
*/
public static ImageRenderer renderer(String jarPath, ClassLoader classLoader) {
return renderer(jarPath, classLoader, false);
}

/**
* Creates an instance of an ImageRenderer only for static pictures
* It reads the specified path located in your jar such as "pictures/image.png".
* Throws an exception if it can't read it
* @param jarPath path of the picture
* @param classLoader classloader of your jar, some module system uses isolated classloaders
* @param absolute disables flushing and release methods, by default false
* @return ImageRenderer instance
*/
public static ImageRenderer renderer(String jarPath, ClassLoader classLoader, boolean absolute) {
if (jarPath.endsWith(".gif")) {
return renderer(JarTool.readGif(jarPath, classLoader), absolute);
} else {
return renderer(JarTool.readImage(jarPath, classLoader), absolute);
}
}


/**
* Creates an instance of an ImageRenderer only for static pictures
* @param image image to use
Expand All @@ -141,7 +171,7 @@ public static ImageRenderer renderer(BufferedImage image) {
/**
* Creates an instance of an ImageRenderer only for static pictures
* @param image image to use
* @param absolute disabled flush and release methods, by default false
* @param absolute disables flushing and release methods, by default false
* @return built instance
*/
public static ImageRenderer renderer(BufferedImage image, boolean absolute) {
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/watermedia/core/tools/JarTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public class JarTool {
static final Marker IT = MarkerManager.getMarker("Tools");
private static final Gson GSON = new Gson();

@Deprecated
public static String readString(ClassLoader loader, String source) {
public static String readString(String source, ClassLoader loader) {
try {
byte[] bytes = DataTool.readAllBytes(readResourceAsStream$byClassLoader(source, loader));
return new String(bytes, Charset.defaultCharset());
Expand All @@ -34,8 +33,7 @@ public static String readString(ClassLoader loader, String source) {
}
}

@Deprecated
public static boolean copyAsset(ClassLoader loader, String source, Path dest) {
public static boolean copyAsset(String source, Path dest, ClassLoader loader) {
try (InputStream is = readResourceAsStream$byClassLoader(source, loader)) {
if (is == null) throw new FileNotFoundException("Resource was not found in " + source);

Expand All @@ -49,8 +47,7 @@ public static boolean copyAsset(ClassLoader loader, String source, Path dest) {
return false;
}

@Deprecated
public static List<String> readStringList(ClassLoader loader, String source) {
public static List<String> readStringList(String source, ClassLoader loader) {
List<String> result = new ArrayList<>();
try (InputStreamReader reader = new InputStreamReader(readResourceAsStream$byClassLoader(source, loader))) {
result.addAll(GSON.fromJson(reader, new TypeToken<List<String>>() {}.getType()));
Expand All @@ -61,8 +58,7 @@ public static List<String> readStringList(ClassLoader loader, String source) {
return result;
}

@Deprecated
public static BufferedImage readImage(ClassLoader loader, String path) {
public static BufferedImage readImage(String path, ClassLoader loader) {
try (InputStream in = readResourceAsStream$byClassLoader(path, loader)) {
BufferedImage image = ImageIO.read(in);
if (image != null) return image;
Expand All @@ -72,8 +68,7 @@ public static BufferedImage readImage(ClassLoader loader, String path) {
}
}

@Deprecated
public static GifDecoder readGif(ClassLoader loader, String path) {
public static GifDecoder readGif(String path, ClassLoader loader) {
try (BufferedInputStream in = new BufferedInputStream(readResourceAsStream$byClassLoader(path, loader))) {
GifDecoder gif = new GifDecoder();
int status = gif.read(in);
Expand Down

0 comments on commit d66d0bd

Please sign in to comment.