Skip to content

Commit

Permalink
Improve the robustness of getResourceAsStream() in Utils.java.
Browse files Browse the repository at this point in the history
  • Loading branch information
t-burch committed Sep 26, 2024
1 parent 8922051 commit 841bc73
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
import jakarta.mail.internet.*;

import java.io.*;
import java.net.URL;
import java.time.*;
import java.time.format.*;
import java.util.*;
import java.util.regex.*;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import static com.predic8.membrane.core.exchange.Exchange.*;
import static java.nio.charset.StandardCharsets.*;
Expand Down Expand Up @@ -200,9 +206,23 @@ public static String normalizeForId(String s) {
}

public static InputStream getResourceAsStream(Object obj, String fileName) {
return obj.getClass().getResourceAsStream(fileName);
try {
URL url = obj.getClass().getResource(fileName);
if (url == null) {
return null;
}

return new FileInputStream(new URI(url.toString()).getPath());
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
return null;
}
}

/*public static InputStream getResourceAsStream(Object obj, String fileName) {
return obj.getClass().getResourceAsStream(fileName);
}*/

public static byte[] createErrorMessage(String msg) {
return String.format("{ \"error\": \"%s\" }",msg).getBytes();
}
Expand Down

0 comments on commit 841bc73

Please sign in to comment.