From 8f45a9f2289e01d4f6eee6036f7fc3c4a92e3024 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Perrin?= Date: Thu, 30 Jan 2020 10:40:32 +0000 Subject: [PATCH] Read ZIP entries in chunks Cuts reading a tile from several seconds to pretty much instantaneous. Particularily noticable with 1-arcsecond datafiles --- src/tim/prune/function/srtm/SrtmSource.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/tim/prune/function/srtm/SrtmSource.java b/src/tim/prune/function/srtm/SrtmSource.java index 0791cd8..44ceccf 100644 --- a/src/tim/prune/function/srtm/SrtmSource.java +++ b/src/tim/prune/function/srtm/SrtmSource.java @@ -19,11 +19,19 @@ public abstract class SrtmSource { throws IOException { int[] heights = new int[tileSize]; + int dataSize = 2 * tileSize; + byte[] buffer = new byte[dataSize]; // Read entire file contents into one byte array - for (int i = 0; i < heights.length; i++) + int alreadyRead = 0; + while (alreadyRead < dataSize) { - heights[i] = inStream.read() * 256 + inStream.read(); - if (heights[i] >= 32768) {heights[i] -= 65536;} + alreadyRead += inStream.read(buffer, alreadyRead, dataSize - alreadyRead); + } + for (int i = 0; i < tileSize; i++) + { + // Bytes are signed. Cast high-order to int with sign + // extension, and clamp low-order to its unsigned range + heights[i] = buffer[2 * i] * 256 + (0xff & buffer[2 * i + 1]); } // Close stream inStream.close(); -- 2.43.0