Skip to content

Commit

Permalink
Change some of the GeoTiff methods to package-private static
Browse files Browse the repository at this point in the history
* This allows us to drop reflection in the TestIFDEntry tests
* Simplifies the test setup and execution
  • Loading branch information
WeatherGod committed May 7, 2024
1 parent 610b1fb commit ec4f996
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
14 changes: 7 additions & 7 deletions cdm/misc/src/main/java/ucar/nc2/geotiff/GeoTiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 4 additions & 11 deletions cdm/misc/src/test/java/ucar/nc2/geotiff/TestIFDEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit ec4f996

Please sign in to comment.