From c52aefc5dc70c0e21a98489810f33749a4385f6a Mon Sep 17 00:00:00 2001 From: Bertil Chapuis Date: Wed, 6 Dec 2023 10:48:42 +0100 Subject: [PATCH] Remove use buffer instead of stream when deserializing headers --- .../baremaps/tilestore/pmtiles/PMTiles.java | 63 ++++++++++--------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java index 38e8544ea..f44a91388 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java @@ -17,7 +17,6 @@ package org.apache.baremaps.tilestore.pmtiles; -import com.google.common.io.LittleEndianDataInputStream; import com.google.common.math.LongMath; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -182,34 +181,40 @@ public static long[] tileIdToZxy(long i) { private static final int HEADER_SIZE_BYTES = 127; - public static Header deserializeHeader(LittleEndianDataInputStream input) throws IOException { - input.skipBytes(7); + public static Header deserializeHeader(InputStream input) throws IOException { + byte[] bytes = new byte[HEADER_SIZE_BYTES]; + var num = input.read(bytes); + if (num != HEADER_SIZE_BYTES) { + throw new IOException("Invalid header size"); + } + var buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); + buffer.position(7); return new Header( - input.readByte(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readLong(), - input.readByte() == 1, - Compression.values()[input.readByte()], - Compression.values()[input.readByte()], - TileType.values()[input.readByte()], - input.readByte(), - input.readByte(), - (double) input.readInt() / 10000000, - (double) input.readInt() / 10000000, - (double) input.readInt() / 10000000, - (double) input.readInt() / 10000000, - input.readByte(), - (double) input.readInt() / 10000000, - (double) input.readInt() / 10000000); + buffer.get(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.getLong(), + buffer.get() == 1, + Compression.values()[buffer.get()], + Compression.values()[buffer.get()], + TileType.values()[buffer.get()], + buffer.get(), + buffer.get(), + (double) buffer.getInt() / 10000000, + (double) buffer.getInt() / 10000000, + (double) buffer.getInt() / 10000000, + (double) buffer.getInt() / 10000000, + buffer.get(), + (double) buffer.getInt() / 10000000, + (double) buffer.getInt() / 10000000); } public static byte[] serializeHeader(Header header) { @@ -278,7 +283,7 @@ public static void serializeEntries(OutputStream output, List entries) output.write(buffer.array(), 0, buffer.limit()); } - public static List deserializeEntries(LittleEndianDataInputStream buffer) + public static List deserializeEntries(InputStream buffer) throws IOException { long numEntries = readVarInt(buffer); List entries = new ArrayList<>((int) numEntries);