From c66c1d43dfbe878e951e054eaec529250affcec2 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 25 Aug 2014 14:40:13 +0200 Subject: [PATCH] Fixed null pointer with FileUtil.parseDir(), and issues with MathUtil.random. Added more logging info to Storage --- Dorkbox-Util/src/dorkbox/util/FileUtil.java | 20 +++++++------ Dorkbox-Util/src/dorkbox/util/MathUtils.java | 30 +++++++++++++++----- Dorkbox-Util/src/dorkbox/util/Storage.java | 2 +- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Dorkbox-Util/src/dorkbox/util/FileUtil.java b/Dorkbox-Util/src/dorkbox/util/FileUtil.java index f496c3a..19d8b01 100644 --- a/Dorkbox-Util/src/dorkbox/util/FileUtil.java +++ b/Dorkbox-Util/src/dorkbox/util/FileUtil.java @@ -589,16 +589,18 @@ public class FileUtil { while (directories.peek() != null) { File dir = directories.poll(); File[] listFiles = dir.listFiles(); - for (File file : listFiles) { - if (file.isDirectory()) { - directories.add(file); - } else { - if (extensionsToMatch == null) { - jarList.add(file); + if (listFiles != null) { + for (File file : listFiles) { + if (file.isDirectory()) { + directories.add(file); } else { - for (String e : extensionsToMatch) { - if (file.getAbsolutePath().endsWith(e)) { - jarList.add(file); + if (extensionsToMatch == null || extensionsToMatch.length == 0 || extensionsToMatch[0] == null) { + jarList.add(file); + } else { + for (String e : extensionsToMatch) { + if (file.getAbsolutePath().endsWith(e)) { + jarList.add(file); + } } } } diff --git a/Dorkbox-Util/src/dorkbox/util/MathUtils.java b/Dorkbox-Util/src/dorkbox/util/MathUtils.java index 33fd74b..083bf9b 100644 --- a/Dorkbox-Util/src/dorkbox/util/MathUtils.java +++ b/Dorkbox-Util/src/dorkbox/util/MathUtils.java @@ -25,39 +25,55 @@ public class MathUtils { private static ThreadLocal random = new ThreadLocal(); + /** + * Creates the thread local MersenneTwister (as it's not thread safe), if necessary + * @return the MersenneTwister. + */ + public static MersenneTwisterFast random() { + MersenneTwisterFast mersenneTwisterFast = random.get(); + + if (mersenneTwisterFast == null) { + mersenneTwisterFast = new MersenneTwisterFast(); + random.set(mersenneTwisterFast); + return mersenneTwisterFast; + } else { + return mersenneTwisterFast; + } + } + /** Returns a random integer */ static public final int randomInt () { - return random.get().nextInt(); + return random().nextInt(); } /** Returns a random number between 0 (inclusive) and the specified value (inclusive). */ static public final int randomInt (int range) { - return random.get().nextInt(range + 1); + return random().nextInt(range + 1); } /** Returns a random number between start (inclusive) and end (inclusive). */ static public final int randomInt (int start, int end) { - return start + random.get().nextInt(end - start + 1); + return start + random().nextInt(end - start + 1); } /** Returns a random boolean value. */ static public final boolean randomBoolean () { - return random.get().nextBoolean(); + return random().nextBoolean(); } /** Returns random number between 0.0 (inclusive) and 1.0 (exclusive). */ static public final float randomFloat () { - return random.get().nextFloat(); + return random().nextFloat(); } /** Returns a random number between 0 (inclusive) and the specified value (exclusive). */ static public final float randomFloat (float range) { - return random.get().nextFloat() * range; + return random().nextFloat() * range; } /** Returns a random number between start (inclusive) and end (exclusive). */ static public final float randomFloat (float start, float end) { - return start + random.get().nextFloat() * (end - start); + return start + random().nextFloat() * (end - start); } // --- diff --git a/Dorkbox-Util/src/dorkbox/util/Storage.java b/Dorkbox-Util/src/dorkbox/util/Storage.java index 2085826..43e0bab 100644 --- a/Dorkbox-Util/src/dorkbox/util/Storage.java +++ b/Dorkbox-Util/src/dorkbox/util/Storage.java @@ -235,7 +235,7 @@ public class Storage { Object readObject = this.kryo.readObject(input, clazz); return (T) readObject; } catch (Exception e) { - logger.error("Error reading from '{}'! Perhaps the file is corrupt?", file.getAbsolutePath()); + logger.error("Error reading from '{}'! Perhaps the file is corrupt?", file.getAbsolutePath(), e); return null; } finally { if (input != null) {