Skip to content

Commit e4b9410

Browse files
author
Lee Rhodes
committed
Correct "bugs" found with FindBugs.
1 parent 98c5f87 commit e4b9410

3 files changed

Lines changed: 34 additions & 30 deletions

File tree

src/main/java/com/yahoo/sketches/memory/UnsafeUtil.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.yahoo.sketches.memory;
77

88
import java.lang.reflect.Constructor;
9+
import java.lang.reflect.InvocationTargetException;
910

1011
import com.yahoo.sketches.SketchesException;
1112

@@ -70,18 +71,25 @@ final class UnsafeUtil {
7071
static final long UNSAFE_COPY_THRESHOLD = 1L << 20; //2^20
7172

7273
static {
73-
try {
74-
//should work across JVMs, e.g., with Android:
75-
Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
76-
unsafeConstructor.setAccessible(true);
77-
unsafe = unsafeConstructor.newInstance();
78-
79-
// Alternative, but may not work across different JVMs.
80-
// Field field = Unsafe.class.getDeclaredField("theUnsafe");
81-
// field.setAccessible(true);
82-
// unsafe = (Unsafe) field.get(null);
83-
84-
//4 on 32-bits systems, 8 on 64-bit systems. Not an indicator of compressed ref (Oop)
74+
try {
75+
//should work across JVMs, e.g., with Android:
76+
Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
77+
unsafeConstructor.setAccessible(true);
78+
unsafe = unsafeConstructor.newInstance();
79+
80+
// Alternative, but may not work across different JVMs.
81+
// Field field = Unsafe.class.getDeclaredField("theUnsafe");
82+
// field.setAccessible(true);
83+
// unsafe = (Unsafe) field.get(null);
84+
85+
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
86+
| InvocationTargetException | NoSuchMethodException e) {
87+
e.printStackTrace();
88+
throw new SketchesException("Unable to acquire Unsafe. ", e);
89+
}
90+
91+
//4 on 32-bits systems and 64-bit systems < 32GB, otherwise 8.
92+
//This alone is not an indicator of compressed ref (Oop)
8593
ADDRESS_SIZE = unsafe.addressSize();
8694

8795
ARRAY_BOOLEAN_BASE_OFFSET = unsafe.arrayBaseOffset(boolean[].class);
@@ -103,11 +111,6 @@ final class UnsafeUtil {
103111
ARRAY_FLOAT_INDEX_SCALE = unsafe.arrayIndexScale(float[].class);
104112
ARRAY_DOUBLE_INDEX_SCALE = unsafe.arrayIndexScale(double[].class);
105113
ARRAY_OBJECT_INDEX_SCALE = unsafe.arrayIndexScale(Object[].class);
106-
107-
}
108-
catch (Exception e) {
109-
throw new SketchesException("Unable to acquire Unsafe. ", e);
110-
}
111114
}
112115

113116
private UnsafeUtil() {}

src/main/java/com/yahoo/sketches/theta/SetOperation.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,8 @@ static final int computeMinLgArrLongsFromCount(final int count) {
170170
*/
171171
static boolean isValidSetOpID(int id) {
172172
Family family = Family.idToFamily(id);
173-
boolean ret;
174-
switch (family) {
175-
case UNION :
176-
case INTERSECTION :
177-
case A_NOT_B : ret = true; break;
178-
default: ret = false; break;
179-
}
173+
boolean ret = ((family == Family.UNION) || (family == Family.INTERSECTION)
174+
|| (family == Family.A_NOT_B));
180175
return ret;
181176
}
182177
}

src/test/java/com/yahoo/sketches/memory/MemoryMappedFileTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import static org.testng.Assert.assertEquals;
99
import static org.testng.Assert.assertTrue;
1010
import static org.testng.Assert.fail;
11+
import static java.nio.charset.StandardCharsets.UTF_8;
1112

1213
import java.io.CharArrayReader;
1314
import java.io.File;
1415
import java.io.FileNotFoundException;
1516
import java.io.PrintWriter;
17+
import java.io.UnsupportedEncodingException;
1618

1719
import org.testng.annotations.Test;
1820

@@ -25,7 +27,7 @@ public class MemoryMappedFileTest {
2527
@Test(expectedExceptions = RuntimeException.class)
2628
public void testMapException() throws Exception {
2729
File dummy = createFile("dummy.txt", "");
28-
Memory mmf = new MemoryMappedFile(dummy, 0, dummy.length()); //zero length
30+
new MemoryMappedFile(dummy, 0, dummy.length()); //zero length
2931
}
3032

3133
@SuppressWarnings("unused")
@@ -154,7 +156,7 @@ public void testForce() throws Exception {
154156

155157
// add content
156158
String cor = "Correcting spelling mistakes";
157-
byte[] b = cor.getBytes();
159+
byte[] b = cor.getBytes(UTF_8);
158160
mmf.putByteArray(0, b, 0, b.length);
159161

160162
mmf.force();
@@ -193,13 +195,17 @@ public void checkPassThrough() {
193195
mem.freeMemory();
194196
}
195197

196-
@SuppressWarnings("resource")
197198
private static File createFile(String fileName, String text) throws FileNotFoundException {
198199
File file = new File(fileName);
199200
file.deleteOnExit();
200-
PrintWriter writer = new PrintWriter(file);
201-
writer.print(text);
202-
writer.close();
201+
PrintWriter writer;
202+
try {
203+
writer = new PrintWriter(file, UTF_8.name());
204+
writer.print(text);
205+
writer.close();
206+
} catch (UnsupportedEncodingException e) {
207+
e.printStackTrace();
208+
}
203209
return file;
204210
}
205211

0 commit comments

Comments
 (0)