1. import java.io.IOException;
  2. import java.net.URL;
  3. import java.util.logging.Logger;
  4. public class ResourceLoader {
  5. private static final Logger logger = Logger.getLogger(ResourceLoader.class.getName());
  6. /**
  7. * Loads resources from URLs provided as URL parameters.
  8. *
  9. * @param resourceUrls An array of URLs pointing to resources.
  10. * @return A map where keys are resource names (extracted from URL paths)
  11. * and values are the content of the resources as byte arrays.
  12. * Returns an empty map if no URLs are provided or if an error occurs.
  13. */
  14. public static java.util.Map<String, byte[]> loadResources(String[] resourceUrls) {
  15. java.util.Map<String, byte[]> resources = new java.util.HashMap<>();
  16. if (resourceUrls == null || resourceUrls.length == 0) {
  17. logger.info("No resource URLs provided. Returning empty map.");
  18. return resources;
  19. }
  20. for (String url : resourceUrls) {
  21. if (url == null || url.trim().isEmpty()) {
  22. logger.warning("Skipping null or empty URL.");
  23. continue;
  24. }
  25. try {
  26. URL resourceUrl = new URL(url);
  27. byte[] resourceBytes = fetchResource(resourceUrl);
  28. if (resourceBytes != null) {
  29. // Extract resource name from URL path
  30. String resourceName = extractResourceName(resourceUrl.getPath());
  31. resources.put(resourceName, resourceBytes);
  32. logger.info("Successfully loaded resource: " + resourceName + " from " + url);
  33. } else {
  34. logger.error("Failed to fetch resource from " + url);
  35. }
  36. } catch (IOException e) {
  37. logger.error("Error loading resource from " + url, e);
  38. }
  39. }
  40. return resources;
  41. }
  42. /**
  43. * Fetches the content of a resource from a URL.
  44. *
  45. * @param url The URL of the resource.
  46. * @return The content of the resource as a byte array, or null if an error occurs.
  47. * @throws IOException if an I/O error occurs during the fetch.
  48. */
  49. private static byte[] fetchResource(URL url) throws IOException {
  50. try (java.io.InputStream in = url.openStream();
  51. java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream()) {
  52. byte[] buffer = new byte[4096];
  53. int len;
  54. while ((len = in.read(buffer)) > 0) {
  55. out.write(buffer, 0, len);
  56. }
  57. return out.toByteArray();
  58. }
  59. }
  60. /**
  61. * Extracts the resource name from the URL path.
  62. *
  63. * @param path The URL path.
  64. * @return The extracted resource name.
  65. */
  66. private static String extractResourceName(String path) {
  67. String[] parts = path.split("/");
  68. if (parts.length > 1) {
  69. return parts[parts.length - 1];
  70. } else {
  71. return "unknown"; // Handle cases where the path is just the root
  72. }
  73. }
  74. public static void main(String[] args) {
  75. // Example usage
  76. String[] urls = {
  77. "https://www.example.com/image.jpg",
  78. "https://www.example.com/data.txt",
  79. "https://www.example.com/nonexistent.pdf",
  80. "invalid_url" //Example of an invalid url.
  81. };
  82. java.util.Map<String, byte[]> loadedResources = loadResources(urls);
  83. if (loadedResources != null) {
  84. for (java.util.Map.Entry<String, byte[]> entry : loadedResources.entrySet()) {
  85. System.out.println("Resource: " + entry.getKey() + ", Size: " + entry.getValue().length + " bytes");
  86. }
  87. }
  88. }
  89. }

Add your comment