diff --git a/Dorkbox-Util/LICENSE.TXT b/Dorkbox-Util/LICENSE.TXT index 7f3709b..3e33be0 100644 --- a/Dorkbox-Util/LICENSE.TXT +++ b/Dorkbox-Util/LICENSE.TXT @@ -81,7 +81,6 @@ Legal: - ConcurrentHashMapV8 - CC0 License - http://www.bouncycastle.org Written by Doug Lea with assistance from members of JCP JSR-166 Expert Group and released to the public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/ diff --git a/Dorkbox-Util/src/dorkbox/util/MathUtils.java b/Dorkbox-Util/src/dorkbox/util/MathUtils.java index 083bf9b..2613c4d 100644 --- a/Dorkbox-Util/src/dorkbox/util/MathUtils.java +++ b/Dorkbox-Util/src/dorkbox/util/MathUtils.java @@ -27,7 +27,6 @@ public class MathUtils { /** * Creates the thread local MersenneTwister (as it's not thread safe), if necessary - * @return the MersenneTwister. */ public static MersenneTwisterFast random() { MersenneTwisterFast mersenneTwisterFast = random.get(); diff --git a/Dorkbox-Util/src/dorkbox/util/Sys.java b/Dorkbox-Util/src/dorkbox/util/Sys.java index 69ed7b9..5f53dbf 100644 --- a/Dorkbox-Util/src/dorkbox/util/Sys.java +++ b/Dorkbox-Util/src/dorkbox/util/Sys.java @@ -17,6 +17,8 @@ import java.net.NetworkInterface; import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; @@ -31,9 +33,16 @@ import java.util.regex.Pattern; import org.bouncycastle.crypto.digests.SHA256Digest; +import com.esotericsoftware.kryo.util.Util; + import dorkbox.urlHandler.BoxURLConnection; public class Sys { + public static final int javaVersion = getJavaVersion(); + public static final boolean isAndroid = Util.isAndroid; + + public static final sun.misc.Unsafe unsafe = getUNSAFE(); + public static final int KILOBYTE = 1024; public static final int MEGABYTE = 1024 * KILOBYTE; public static final int GIGABYTE = 1024 * MEGABYTE; @@ -49,6 +58,51 @@ public class Sys { return charArray; } + private static int getJavaVersion() { + String fullJavaVersion = System.getProperty("java.version"); + + // Converts a java version string, such as "1.7u45", and converts it into 7 + char versionChar; + if (fullJavaVersion.startsWith("1.")) { + versionChar = fullJavaVersion.charAt(2); + } else { + versionChar = fullJavaVersion.charAt(0); + } + + switch (versionChar) { + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + default: return -1; + } + } + + private static sun.misc.Unsafe getUNSAFE() { + try { + final PrivilegedExceptionAction action = new PrivilegedExceptionAction() { + @Override + public sun.misc.Unsafe run() throws Exception { + Class unsafeClass = sun.misc.Unsafe.class; + Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Object unsafeObject = theUnsafe.get(null); + if (unsafeClass.isInstance(unsafeObject)) { + return unsafeClass.cast(unsafeObject); + } + + throw new NoSuchFieldError("the Unsafe"); + } + }; + + return AccessController.doPrivileged(action); + } + catch (Exception e) { + throw new RuntimeException("Unable to load unsafe", e); + } + } public static final void eraseString(String string) { // You can change the value of the inner char[] using reflection. diff --git a/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java b/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java index 2a34434..f240208 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java @@ -375,6 +375,9 @@ public class StorageBase { metaData.dataCapacity = size; metaData.dataCount = 0; } + + // TODO: should check to see if the data is different. IF SO, then we write, otherwise nothing! + metaData.writeData(dataStream, this.file); }