diff --git a/src/main/java/com/yahoo/sketches/memory/UnsafeUtil.java b/src/main/java/com/yahoo/sketches/memory/UnsafeUtil.java index fc04a77f5..c84d5c15e 100644 --- a/src/main/java/com/yahoo/sketches/memory/UnsafeUtil.java +++ b/src/main/java/com/yahoo/sketches/memory/UnsafeUtil.java @@ -6,6 +6,7 @@ package com.yahoo.sketches.memory; import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import com.yahoo.sketches.SketchesException; @@ -70,18 +71,25 @@ final class UnsafeUtil { static final long UNSAFE_COPY_THRESHOLD = 1L << 20; //2^20 static { - try { - //should work across JVMs, e.g., with Android: - Constructor unsafeConstructor = Unsafe.class.getDeclaredConstructor(); - unsafeConstructor.setAccessible(true); - unsafe = unsafeConstructor.newInstance(); - - // Alternative, but may not work across different JVMs. -// Field field = Unsafe.class.getDeclaredField("theUnsafe"); -// field.setAccessible(true); -// unsafe = (Unsafe) field.get(null); - - //4 on 32-bits systems, 8 on 64-bit systems. Not an indicator of compressed ref (Oop) + try { + //should work across JVMs, e.g., with Android: + Constructor unsafeConstructor = Unsafe.class.getDeclaredConstructor(); + unsafeConstructor.setAccessible(true); + unsafe = unsafeConstructor.newInstance(); + + // Alternative, but may not work across different JVMs. +// Field field = Unsafe.class.getDeclaredField("theUnsafe"); +// field.setAccessible(true); +// unsafe = (Unsafe) field.get(null); + + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + throw new SketchesException("Unable to acquire Unsafe. ", e); + } + + //4 on 32-bits systems and 64-bit systems < 32GB, otherwise 8. + //This alone is not an indicator of compressed ref (Oop) ADDRESS_SIZE = unsafe.addressSize(); ARRAY_BOOLEAN_BASE_OFFSET = unsafe.arrayBaseOffset(boolean[].class); @@ -103,11 +111,6 @@ final class UnsafeUtil { ARRAY_FLOAT_INDEX_SCALE = unsafe.arrayIndexScale(float[].class); ARRAY_DOUBLE_INDEX_SCALE = unsafe.arrayIndexScale(double[].class); ARRAY_OBJECT_INDEX_SCALE = unsafe.arrayIndexScale(Object[].class); - - } - catch (Exception e) { - throw new SketchesException("Unable to acquire Unsafe. ", e); - } } private UnsafeUtil() {} diff --git a/src/main/java/com/yahoo/sketches/theta/SetOperation.java b/src/main/java/com/yahoo/sketches/theta/SetOperation.java index 40921513e..cfb98e2ba 100644 --- a/src/main/java/com/yahoo/sketches/theta/SetOperation.java +++ b/src/main/java/com/yahoo/sketches/theta/SetOperation.java @@ -170,13 +170,8 @@ static final int computeMinLgArrLongsFromCount(final int count) { */ static boolean isValidSetOpID(int id) { Family family = Family.idToFamily(id); - boolean ret; - switch (family) { - case UNION : - case INTERSECTION : - case A_NOT_B : ret = true; break; - default: ret = false; break; - } + boolean ret = ((family == Family.UNION) || (family == Family.INTERSECTION) + || (family == Family.A_NOT_B)); return ret; } } diff --git a/src/test/java/com/yahoo/sketches/memory/MemoryMappedFileTest.java b/src/test/java/com/yahoo/sketches/memory/MemoryMappedFileTest.java index 00fcfbf22..69fc3ad1f 100644 --- a/src/test/java/com/yahoo/sketches/memory/MemoryMappedFileTest.java +++ b/src/test/java/com/yahoo/sketches/memory/MemoryMappedFileTest.java @@ -8,11 +8,13 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; +import static java.nio.charset.StandardCharsets.UTF_8; import java.io.CharArrayReader; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; import org.testng.annotations.Test; @@ -25,7 +27,7 @@ public class MemoryMappedFileTest { @Test(expectedExceptions = RuntimeException.class) public void testMapException() throws Exception { File dummy = createFile("dummy.txt", ""); - Memory mmf = new MemoryMappedFile(dummy, 0, dummy.length()); //zero length + new MemoryMappedFile(dummy, 0, dummy.length()); //zero length } @SuppressWarnings("unused") @@ -154,7 +156,7 @@ public void testForce() throws Exception { // add content String cor = "Correcting spelling mistakes"; - byte[] b = cor.getBytes(); + byte[] b = cor.getBytes(UTF_8); mmf.putByteArray(0, b, 0, b.length); mmf.force(); @@ -193,13 +195,17 @@ public void checkPassThrough() { mem.freeMemory(); } - @SuppressWarnings("resource") private static File createFile(String fileName, String text) throws FileNotFoundException { File file = new File(fileName); file.deleteOnExit(); - PrintWriter writer = new PrintWriter(file); - writer.print(text); - writer.close(); + PrintWriter writer; + try { + writer = new PrintWriter(file, UTF_8.name()); + writer.print(text); + writer.close(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } return file; }