From 4695dcb633d675bd3f590dd2ebb198af9e04345f Mon Sep 17 00:00:00 2001 From: catobus Date: Mon, 2 Mar 2026 19:25:03 -0800 Subject: [PATCH] Add openRemoteStream hook for custom HTTP stacks --- .../viewer/AbstractTileFactory.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/jxmapviewer2/src/main/java/org/jxmapviewer/viewer/AbstractTileFactory.java b/jxmapviewer2/src/main/java/org/jxmapviewer/viewer/AbstractTileFactory.java index 93f1481..99af549 100644 --- a/jxmapviewer2/src/main/java/org/jxmapviewer/viewer/AbstractTileFactory.java +++ b/jxmapviewer2/src/main/java/org/jxmapviewer/viewer/AbstractTileFactory.java @@ -340,6 +340,23 @@ public synchronized int getPendingTiles() { return tileQueue.size(); } + /** + * Opens an InputStream for the given tile URL. + * + * Subclasses may override this method to use a custom HTTP implementation. + * The default implementation uses URLConnection. + * + * @param url the URL of the tile + * @return the input stream for the URL + * @throws IOException if an I/O error occurs while opening the stream + */ + protected InputStream openRemoteStream(URL url) throws IOException { + URLConnection connection = url.openConnection(); + connection.setRequestProperty("User-Agent", userAgent); + addCustomRequestProperties(connection); + return connection.getInputStream(); + } + /** * An inner class which actually loads the tiles. Used by the thread queue. Subclasses can override this * via {@link #createTileRunner(Tile)} if necessary. @@ -437,15 +454,15 @@ public void run() private byte[] cacheInputStream(URL url) throws IOException { InputStream ins = localCache.get(url); - if (ins == null) { - URLConnection connection = url.openConnection(); - connection.setRequestProperty("User-Agent", userAgent); - addCustomRequestProperties(connection); - ins = connection.getInputStream(); + boolean fromCache = (ins != null); + if (!fromCache) { + ins = openRemoteStream(url); } try { byte[] data = readAllBytes(ins); - localCache.put(url, new ByteArrayInputStream(data)); + if (!fromCache) { + localCache.put(url, new ByteArrayInputStream(data)); + } return data; } finally {