From 07df859a47fab8faf430ed9eb11d8bbb5e869cf5 Mon Sep 17 00:00:00 2001 From: Eugene Maksymenko Date: Mon, 18 Jul 2022 16:13:06 +0300 Subject: [PATCH] Optimize URL connection processing. --- .../worldwind/globe/ElevationRetriever.java | 13 ++++++----- .../nasa/worldwind/layer/LayerFactory.java | 23 +++++++++++-------- .../ogc/Wcs201ElevationCoverage.java | 13 ++++++----- .../nasa/worldwind/render/ImageRetriever.java | 13 ++++++----- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/worldwind/src/main/java/gov/nasa/worldwind/globe/ElevationRetriever.java b/worldwind/src/main/java/gov/nasa/worldwind/globe/ElevationRetriever.java index 6aba57660..e320773ae 100644 --- a/worldwind/src/main/java/gov/nasa/worldwind/globe/ElevationRetriever.java +++ b/worldwind/src/main/java/gov/nasa/worldwind/globe/ElevationRetriever.java @@ -8,8 +8,8 @@ import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.ShortBuffer; @@ -20,7 +20,6 @@ import gov.nasa.worldwind.util.Logger; import gov.nasa.worldwind.util.Retriever; import gov.nasa.worldwind.util.SynchronizedPool; -import gov.nasa.worldwind.util.WWUtil; public class ElevationRetriever extends Retriever { @@ -64,13 +63,13 @@ protected ShortBuffer decodeUrl(String urlString) throws IOException { // TODO retry absent resources, they are currently handled but suppressed entirely after the first failure // TODO configurable connect and read timeouts - InputStream stream = null; + HttpURLConnection conn = null; try { - URLConnection conn = new URL(urlString).openConnection(); + conn = (HttpURLConnection) new URL(urlString).openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(30000); - stream = new BufferedInputStream(conn.getInputStream()); + InputStream stream = new BufferedInputStream(conn.getInputStream()); String contentType = conn.getContentType(); if (contentType.equalsIgnoreCase("application/bil16")) { return this.readInt16Data(stream); @@ -81,7 +80,9 @@ protected ShortBuffer decodeUrl(String urlString) throws IOException { Logger.logMessage(Logger.ERROR, "ElevationRetriever", "decodeUrl", "Format not supported")); } } finally { - WWUtil.closeSilently(stream); + if (conn != null) { + conn.disconnect(); + } } } diff --git a/worldwind/src/main/java/gov/nasa/worldwind/layer/LayerFactory.java b/worldwind/src/main/java/gov/nasa/worldwind/layer/LayerFactory.java index 6c8e154e7..932901ffa 100644 --- a/worldwind/src/main/java/gov/nasa/worldwind/layer/LayerFactory.java +++ b/worldwind/src/main/java/gov/nasa/worldwind/layer/LayerFactory.java @@ -12,8 +12,8 @@ import java.io.BufferedInputStream; import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -53,7 +53,6 @@ import gov.nasa.worldwind.util.LevelSetConfig; import gov.nasa.worldwind.util.Logger; import gov.nasa.worldwind.util.TileFactory; -import gov.nasa.worldwind.util.WWUtil; public class LayerFactory { @@ -432,7 +431,7 @@ protected void createWmtsLayer(WmtsLayer wmtsLayer, Layer layer, Callback callba } protected WmsCapabilities retrieveWmsCapabilities(String serviceAddress) { - InputStream inputStream = null; + HttpURLConnection conn = null; WmsCapabilities wmsCapabilities; try { // Build the appropriate request Uri given the provided service address @@ -443,10 +442,10 @@ protected WmsCapabilities retrieveWmsCapabilities(String serviceAddress) { .build(); // Open the connection as an input stream - URLConnection conn = new URL(serviceUri.toString()).openConnection(); + conn = (HttpURLConnection) new URL(serviceUri.toString()).openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(30000); - inputStream = new BufferedInputStream(conn.getInputStream()); + InputStream inputStream = new BufferedInputStream(conn.getInputStream()); // Parse and read the input stream wmsCapabilities = WmsCapabilities.getCapabilities(inputStream); @@ -454,14 +453,16 @@ protected WmsCapabilities retrieveWmsCapabilities(String serviceAddress) { throw new RuntimeException( Logger.makeMessage("LayerFactory", "retrieveWmsCapabilities", "Unable to open connection and read from service address")); } finally { - WWUtil.closeSilently(inputStream); + if (conn != null) { + conn.disconnect(); + } } return wmsCapabilities; } protected WmtsCapabilities retrieveWmtsCapabilities(String serviceAddress) { - InputStream inputStream = null; + HttpURLConnection conn = null; WmtsCapabilities wmtsCapabilities; try { // Build the appropriate request Uri given the provided service address @@ -472,10 +473,10 @@ protected WmtsCapabilities retrieveWmtsCapabilities(String serviceAddress) { .build(); // Open the connection as an input stream - URLConnection conn = new URL(serviceUri.toString()).openConnection(); + conn = (HttpURLConnection) new URL(serviceUri.toString()).openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(30000); - inputStream = new BufferedInputStream(conn.getInputStream()); + InputStream inputStream = new BufferedInputStream(conn.getInputStream()); // Parse and read the input stream wmtsCapabilities = WmtsCapabilities.getCapabilities(inputStream); @@ -483,7 +484,9 @@ protected WmtsCapabilities retrieveWmtsCapabilities(String serviceAddress) { throw new RuntimeException( Logger.makeMessage("LayerFactory", "retrieveWmsCapabilities", "Unable to open connection and read from service address " + e)); } finally { - WWUtil.closeSilently(inputStream); + if (conn != null) { + conn.disconnect(); + } } return wmtsCapabilities; diff --git a/worldwind/src/main/java/gov/nasa/worldwind/ogc/Wcs201ElevationCoverage.java b/worldwind/src/main/java/gov/nasa/worldwind/ogc/Wcs201ElevationCoverage.java index 39784543a..9c5303688 100644 --- a/worldwind/src/main/java/gov/nasa/worldwind/ogc/Wcs201ElevationCoverage.java +++ b/worldwind/src/main/java/gov/nasa/worldwind/ogc/Wcs201ElevationCoverage.java @@ -11,8 +11,8 @@ import java.io.BufferedInputStream; import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; import gov.nasa.worldwind.WorldWind; import gov.nasa.worldwind.geom.Sector; @@ -26,7 +26,6 @@ import gov.nasa.worldwind.ogc.wcs.Wcs201CoverageDescriptions; import gov.nasa.worldwind.ogc.wcs.WcsXmlParser; import gov.nasa.worldwind.util.Logger; -import gov.nasa.worldwind.util.WWUtil; /** * Generates elevations from OGC Web Coverage Service (WCS) version 2.0.1. @@ -184,7 +183,7 @@ protected TileMatrixSet tileMatrixSetFromCoverageDescription(Wcs201CoverageDescr } protected Wcs201CoverageDescriptions describeCoverage(String serviceAddress, String coverageId) throws Exception { - InputStream inputStream = null; + HttpURLConnection conn = null; Object responseXml; try { // Build the appropriate request Uri given the provided service address @@ -196,7 +195,7 @@ protected Wcs201CoverageDescriptions describeCoverage(String serviceAddress, Str .build(); // Open the connection as an input stream - URLConnection conn = new URL(serviceUri.toString()).openConnection(); + conn = (HttpURLConnection) new URL(serviceUri.toString()).openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(30000); @@ -207,7 +206,7 @@ protected Wcs201CoverageDescriptions describeCoverage(String serviceAddress, Str } // Parse and read the input stream - inputStream = new BufferedInputStream(conn.getInputStream()); + InputStream inputStream = new BufferedInputStream(conn.getInputStream()); responseXml = WcsXmlParser.parse(inputStream); if (responseXml instanceof OwsExceptionReport) { throw new OgcException((OwsExceptionReport) responseXml); @@ -216,7 +215,9 @@ protected Wcs201CoverageDescriptions describeCoverage(String serviceAddress, Str Logger.makeMessage("Wcs201ElevationCoverage", "describeCoverage", "Response is not a WCS DescribeCoverage document")); } } finally { - WWUtil.closeSilently(inputStream); + if (conn != null) { + conn.disconnect(); + } } return (Wcs201CoverageDescriptions) responseXml; diff --git a/worldwind/src/main/java/gov/nasa/worldwind/render/ImageRetriever.java b/worldwind/src/main/java/gov/nasa/worldwind/render/ImageRetriever.java index 21a1707dc..23145a05d 100644 --- a/worldwind/src/main/java/gov/nasa/worldwind/render/ImageRetriever.java +++ b/worldwind/src/main/java/gov/nasa/worldwind/render/ImageRetriever.java @@ -12,13 +12,12 @@ import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; import gov.nasa.worldwind.WorldWind; import gov.nasa.worldwind.util.Logger; import gov.nasa.worldwind.util.Retriever; -import gov.nasa.worldwind.util.WWUtil; public class ImageRetriever extends Retriever { @@ -93,13 +92,13 @@ protected Bitmap decodeUrl(String urlString, ImageOptions imageOptions, ImageSou // TODO retry absent resources, they are currently handled but suppressed entirely after the first failure // TODO configurable connect and read timeouts - InputStream stream = null; + HttpURLConnection conn = null; try { - URLConnection conn = new URL(urlString).openConnection(); + conn = (HttpURLConnection) new URL(urlString).openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(30000); - stream = new BufferedInputStream(conn.getInputStream()); + InputStream stream = new BufferedInputStream(conn.getInputStream()); BitmapFactory.Options factoryOptions = this.bitmapFactoryOptions(imageOptions); Bitmap bitmap = BitmapFactory.decodeStream(stream, null, factoryOptions); @@ -111,7 +110,9 @@ protected Bitmap decodeUrl(String urlString, ImageOptions imageOptions, ImageSou return bitmap; } finally { - WWUtil.closeSilently(stream); + if (conn != null) { + conn.disconnect(); + } } }