From 506ae5e9fd258db7bfe737999e769477a32643a7 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 1 Oct 2013 07:41:45 +0200 Subject: Fix Bug 843: Remove Platform's requirement and use of TempJarCache.bootstrapNativeLib(), allowing versatile use of 1st native jar file (big-java-jar w/ big-native-jar) The remaining Platform dependency existed in IOUtil.copyStream2Stream(..), used by JarUtil.extract(..), i.e. the MachineDescription's PAGE_SIZE. Solved by using a const buffer size of 4096 bytes. --- src/java/com/jogamp/common/os/Platform.java | 14 +--- src/java/com/jogamp/common/util/IOUtil.java | 21 +++++- src/java/com/jogamp/common/util/JarUtil.java | 4 +- .../com/jogamp/common/util/cache/TempJarCache.java | 78 +--------------------- 4 files changed, 25 insertions(+), 92 deletions(-) (limited to 'src/java/com/jogamp') diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index 32fc9f4..a01a4b4 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -33,7 +33,7 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.util.concurrent.TimeUnit; -import com.jogamp.common.util.IOUtil; +import com.jogamp.common.jvm.JNILibLoaderBase; import com.jogamp.common.util.JarUtil; import com.jogamp.common.util.ReflectionUtil; import com.jogamp.common.util.VersionNumber; @@ -192,19 +192,11 @@ public class Platform extends PlatformPropsImpl { // load GluegenRT native library if(_USE_TEMP_JAR_CACHE[0] && TempJarCache.initSingleton()) { - String nativeJarName = null; - URI jarUriRoot = null; - URI nativeJarURI = null; try { - final String jarName = JarUtil.getJarBasename( platformClassJarURI ); - final String nativeJarBasename = jarName.substring(0, jarName.indexOf(".jar")); // ".jar" already validated w/ JarUtil.getJarBasename(..) - nativeJarName = nativeJarBasename+"-natives-"+PlatformPropsImpl.os_and_arch+".jar"; - jarUriRoot = IOUtil.getDirname( JarUtil.getJarSubURI( platformClassJarURI ) ); - nativeJarURI = JarUtil.getJarFileURI(jarUriRoot, nativeJarName); - TempJarCache.bootstrapNativeLib(Platform.class, libBaseName, nativeJarURI); + JNILibLoaderBase.addNativeJarLibs(new Class[] { Platform.class }, null, null ); } catch (Exception e0) { // IllegalArgumentException, IOException - System.err.println("Catched "+e0.getClass().getSimpleName()+": "+e0.getMessage()+", while TempJarCache.bootstrapNativeLib() of "+nativeJarURI+" ("+jarUriRoot+" + "+nativeJarName+")"); + System.err.println("Catched "+e0.getClass().getSimpleName()+": "+e0.getMessage()+", while JNILibLoaderBase.addNativeJarLibs(..)"); } } DynamicLibraryBundle.GlueJNILibLoader.loadLibrary(libBaseName, false, cl); diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index de5a8ea..17bf136 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -169,7 +169,22 @@ public class IOUtil { * @throws IOException */ public static int copyStream2Stream(InputStream in, OutputStream out, int totalNumBytes) throws IOException { - final byte[] buf = new byte[Platform.getMachineDescription().pageSizeInBytes()]; + return copyStream2Stream(Platform.getMachineDescription().pageSizeInBytes(), in, out, totalNumBytes); + } + + /** + * Copy the specified input stream to the specified output stream. The total + * number of bytes written is returned. + * + * @param bufferSize the intermediate buffer size, should be {@link MachineDescription#pageSizeInBytes()} for best performance. + * @param in the source + * @param out the destination + * @param totalNumBytes informal number of expected bytes, maybe used for user feedback while processing. -1 if unknown + * @return + * @throws IOException + */ + public static int copyStream2Stream(int bufferSize, InputStream in, OutputStream out, int totalNumBytes) throws IOException { + final byte[] buf = new byte[bufferSize]; int numBytes = 0; while (true) { int count; @@ -181,7 +196,7 @@ public class IOUtil { } return numBytes; } - + /** * Copy the specified input stream to a byte array, which is being returned. */ @@ -281,7 +296,7 @@ public class IOUtil { * @throws URISyntaxException if path is empty or has no parent directory available while resolving ../ */ public static String slashify(String path, boolean startWithSlash, boolean endWithSlash) throws URISyntaxException { - String p = path.replace('\\', '/'); // unify file seperator + String p = path.replace('\\', '/'); // unify file separator if (startWithSlash && !p.startsWith("/")) { p = "/" + p; } diff --git a/src/java/com/jogamp/common/util/JarUtil.java b/src/java/com/jogamp/common/util/JarUtil.java index 5604c3d..145368f 100644 --- a/src/java/com/jogamp/common/util/JarUtil.java +++ b/src/java/com/jogamp/common/util/JarUtil.java @@ -53,6 +53,8 @@ import jogamp.common.Debug; public class JarUtil { private static final boolean DEBUG = Debug.debug("JarUtil"); + private static final int BUFFER_SIZE = 4096; + /** * Interface allowing users to provide an URL resolver that will convert custom classloader * URLs like Eclipse/OSGi bundleresource: URLs to normal jar: URLs. @@ -579,7 +581,7 @@ public class JarUtil { final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile)); int numBytes = -1; try { - numBytes = IOUtil.copyStream2Stream(in, out, -1); + numBytes = IOUtil.copyStream2Stream(BUFFER_SIZE, in, out, -1); } finally { in.close(); out.close(); diff --git a/src/java/com/jogamp/common/util/cache/TempJarCache.java b/src/java/com/jogamp/common/util/cache/TempJarCache.java index 30dea45..3943633 100644 --- a/src/java/com/jogamp/common/util/cache/TempJarCache.java +++ b/src/java/com/jogamp/common/util/cache/TempJarCache.java @@ -27,20 +27,13 @@ */ package com.jogamp.common.util.cache; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import java.security.cert.Certificate; -import java.util.Enumeration; import java.util.HashMap; import java.util.Map; -import java.util.jar.JarEntry; import java.util.jar.JarFile; import jogamp.common.Debug; @@ -397,76 +390,7 @@ public class TempJarCache { } return null; } - - - /** - * Bootstrapping version extracting the JAR files root entry containing libBaseName, - * assuming it's a native library. This is used to get the 'gluegen-rt' - * native library, hence bootstrapping. - * - * @param certClass if class is certified, the JarFile entries needs to have the same certificate - * - * @throws IOException - * @throws SecurityException - * @throws URISyntaxException - * @throws IllegalArgumentException - */ - public synchronized static final void bootstrapNativeLib(Class certClass, String libBaseName, URI jarURI) - throws IOException, SecurityException, IllegalArgumentException, URISyntaxException { - checkInitialized(); - boolean ok = false; - int countEntries = 0; - final LoadState nativeLibJarsLS = nativeLibJars.get(jarURI); - if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) && !nativeLibMap.containsKey(libBaseName) ) { - if(DEBUG) { - System.err.println("TempJarCache: bootstrapNativeLib(certClass: "+certClass+", libBaseName "+libBaseName+", jarURI "+jarURI+")"); - } - nativeLibJars.put(jarURI, LoadState.LOOKED_UP); - final JarFile jarFile = JarUtil.getJarFile(jarURI); - if(DEBUG) { - System.err.println("TempJarCache: bootstrapNativeLib: nativeJar "+jarFile.getName()); - } - validateCertificates(certClass, jarFile); - final Enumeration entries = jarFile.entries(); - while (entries.hasMoreElements()) { - final JarEntry entry = entries.nextElement(); - final String entryName = entry.getName(); - - if( entryName.indexOf('/') == -1 && - entryName.indexOf(File.separatorChar) == -1 && - entryName.indexOf(libBaseName) >= 0 ) - { - final File destFile = new File(tmpFileCache.getTempDir(), entryName); - final InputStream in = new BufferedInputStream(jarFile.getInputStream(entry)); - final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile)); - int numBytes = 0; - try { - final byte[] buf = new byte[ 2048 ]; - while (true) { - int countBytes; - if ((countBytes = in.read(buf)) == -1) { break; } - out.write(buf, 0, countBytes); - numBytes += countBytes; - } - } finally { in.close(); out.close(); } - if (numBytes>0) { - nativeLibMap.put(libBaseName, destFile.getAbsolutePath()); - nativeLibJars.put(jarURI, LoadState.LOADED); - ok = true; - countEntries++; - } - } - } - } else if( testLoadState(nativeLibJarsLS, LoadState.LOADED) ) { - ok = true; // already loaded - } else { - throw new IOException("TempJarCache: bootstrapNativeLib: "+jarURI+", previous load attempt failed"); - } - if(DEBUG) { - System.err.println("TempJarCache: bootstrapNativeLib() done, count "+countEntries+", ok "+ok); - } - } - + private static void validateCertificates(Class certClass, JarFile jarFile) throws IOException, SecurityException { if(null == certClass) { throw new IllegalArgumentException("certClass is null"); -- cgit v1.2.3