From ec4f996dc2604a546179cc66ec84966d27a8ee06 Mon Sep 17 00:00:00 2001 From: Benjamin Root Date: Tue, 7 May 2024 15:05:44 -0400 Subject: [PATCH] Change some of the GeoTiff methods to package-private static * This allows us to drop reflection in the TestIFDEntry tests * Simplifies the test setup and execution --- .../src/main/java/ucar/nc2/geotiff/GeoTiff.java | 14 +++++++------- .../test/java/ucar/nc2/geotiff/TestIFDEntry.java | 15 ++++----------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/cdm/misc/src/main/java/ucar/nc2/geotiff/GeoTiff.java b/cdm/misc/src/main/java/ucar/nc2/geotiff/GeoTiff.java index 41c11ca5b8..2bbdbf0731 100644 --- a/cdm/misc/src/main/java/ucar/nc2/geotiff/GeoTiff.java +++ b/cdm/misc/src/main/java/ucar/nc2/geotiff/GeoTiff.java @@ -326,7 +326,7 @@ private void writeIFDEntry(FileChannel channel, IFDEntry ifd, int start) throws } } - private int writeValues(ByteBuffer buffer, IFDEntry ifd) { + static int writeValues(ByteBuffer buffer, IFDEntry ifd) { int done = 0; if (ifd.type == FieldType.ASCII) { @@ -354,7 +354,7 @@ private int writeValues(ByteBuffer buffer, IFDEntry ifd) { return done; } - private int writeIntValue(ByteBuffer buffer, IFDEntry ifd, int v) { + static int writeIntValue(ByteBuffer buffer, IFDEntry ifd, int v) { switch (ifd.type.code) { case 1: case 2: @@ -380,7 +380,7 @@ private int writeIntValue(ByteBuffer buffer, IFDEntry ifd, int v) { return 0; } - private int writeSValue(ByteBuffer buffer, IFDEntry ifd) { + static int writeSValue(ByteBuffer buffer, IFDEntry ifd) { buffer.put(ifd.valueS.getBytes(StandardCharsets.UTF_8)); int size = ifd.valueS.length(); if ((size & 1) != 0) @@ -527,7 +527,7 @@ private IFDEntry readIFDEntry(FileChannel channel, int start) throws IOException return ifd; } - private void readValues(ByteBuffer buffer, IFDEntry ifd) { + static void readValues(ByteBuffer buffer, IFDEntry ifd) { if (ifd.type == FieldType.ASCII) { ifd.valueS = readSValue(buffer, ifd); @@ -555,7 +555,7 @@ private void readValues(ByteBuffer buffer, IFDEntry ifd) { } - private int readIntValue(ByteBuffer buffer, IFDEntry ifd) { + static int readIntValue(ByteBuffer buffer, IFDEntry ifd) { switch (ifd.type.code) { case 1: case 2: @@ -585,11 +585,11 @@ private int readIntValue(ByteBuffer buffer, IFDEntry ifd) { return 0; } - private int readUShortValue(ByteBuffer buffer) { + static int readUShortValue(ByteBuffer buffer) { return buffer.getShort() & 0xffff; } - private String readSValue(ByteBuffer buffer, IFDEntry ifd) { + static String readSValue(ByteBuffer buffer, IFDEntry ifd) { byte[] dst = new byte[ifd.count]; buffer.get(dst); return new String(dst, StandardCharsets.UTF_8); diff --git a/cdm/misc/src/test/java/ucar/nc2/geotiff/TestIFDEntry.java b/cdm/misc/src/test/java/ucar/nc2/geotiff/TestIFDEntry.java index 8c8ab2a8d2..5ed03353d9 100644 --- a/cdm/misc/src/test/java/ucar/nc2/geotiff/TestIFDEntry.java +++ b/cdm/misc/src/test/java/ucar/nc2/geotiff/TestIFDEntry.java @@ -11,9 +11,7 @@ import org.junit.runners.Parameterized; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.lang.ReflectiveOperationException; import java.lang.invoke.MethodHandles; -import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; @@ -79,22 +77,17 @@ public TestIFDEntry(IFDEntry ifd, int testValue) { } @Test - public void testRoundtrip() throws ReflectiveOperationException { - GeoTiff geotiff = new GeoTiff("foobar"); + public void testRoundtrip() { // 16 bytes should be more than enough ByteBuffer buffer = ByteBuffer.allocate(16); ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; buffer.order(byteOrder); - Method writeMethod = - geotiff.getClass().getDeclaredMethod("writeIntValue", ByteBuffer.class, ifd.getClass(), int.class); - Method readMethod = geotiff.getClass().getDeclaredMethod("readIntValue", ByteBuffer.class, ifd.getClass()); - readMethod.setAccessible(true); - writeMethod.setAccessible(true); - int writeSize = (int) writeMethod.invoke(geotiff, buffer, ifd, testValue); + int writeSize = GeoTiff.writeIntValue(buffer, ifd, testValue); + Assert.assertEquals(ifd.type.size, writeSize); buffer.position(0); - int readValue = (int) readMethod.invoke(geotiff, buffer, ifd); + int readValue = GeoTiff.readIntValue(buffer, ifd); Assert.assertEquals(testValue, readValue); }