package com.artifex.mupdf.fitz;

// This class handles the loading of the MuPDF shared library, together
// with the ThreadLocal magic to get the required context.
//
// The only publicly accessible method here is Context.setStoreSize, which
// sets the store size to use. This must be called before any other MuPDF
// function.
public class Context
{
	private static boolean inited = false;
	private static native int initNative();
	public static native int gprfSupportedNative();

	public static void init() {
		if (!inited) {
			inited = true;
			System.loadLibrary(getLibraryName());
			if (initNative() < 0)
				throw new RuntimeException("cannot initialize mupdf library");
		}
	}

	private static String getLibraryName() {
		/* Mac OS always uses 64bit DLLs for any JDK 1.7 or above */
		if (System.getProperty("os.name").toLowerCase().contains("mac os")) {
			return "mupdf_java64";
		}
		/* Sun and OpenJDK JVMs support this way of finding bittedness */
		String val = System.getProperty("sun.arch.data.model");
		/* Android does NOT support this, and returns NULL */
		if (val != null && val.equals("64")) {
			return "mupdf_java64";
		}

		/* We might be running Android here. We could find out by
		 * doing the following test:
		 *  val = System.getProperty("java.vm.name");
		 *  if (val != null && val.toLowerCase().contains("dalvik")) {
		 *    ...Do something Androidy to test for 32/64 here...
		 *  }
		 * (Currently, both Dalvik and ART return 'Dalvik').
		 * We know of no portable way to detect 32 or 64bittedness
		 * on android though, so for now will assume 32.
		 */

		return "mupdf_java32";
	}

	static { init(); }

	// FIXME: We should support the store size being changed dynamically.
	// This requires changes within the MuPDF core.
	//public native static void setStoreSize(long newSize);
}

@@ -540,16 +540,26 @@
 = true;%0A
+%09%09%09try %7B%0A%09
 %09%09%09Syste
@@ -576,567 +576,178 @@
 ary(
-getLibraryName());%0A%09%09%09if (initNative() %3C 0)%0A%09%09%09%09throw new RuntimeException(%22cannot initialize mupdf library%22);%0A%09%09%7D%0A%09%7D%0A%0A%09private static String getLibraryName() %7B%0A%09%09/* Mac OS always uses 64bit DLLs for any JDK 1.7 or above */%0A%09%09if (System.getProperty(%22os.name%22).toLowerCase().contains(%22mac os%22)) %7B%0A%09%09%09return %22mupdf_java64%22;%0A%09%09%7D%0A%09%09/* Sun and OpenJDK JVMs support this way of finding bittedness */%0A%09%09String val = System.getProperty(%22sun.arch.data.model%22);%0A%09%09/* Android does NOT support this, and returns NULL */%0A%09%09if (val != null && val.equals(%2264%22)) %7B%0A%09%09%09return 
+%22mupdf_java64%22);%0A%09%09%09%7D catch (UnsatisfiedLinkError e) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09System.loadLibrary(%22mupdf_java32%22);%0A%09%09%09%09%7D catch (UnsatisfiedLinkError ee) %7B%0A%09%09%09%09%09System.loadLibrary(
 %22mup
@@ -757,487 +757,118 @@
 java
-64%22;%0A
+%22);%0A%09%09
 %09%09%7D%0A
-%0A
 %09%09
-/* We might be running Android here. We could find out by%0A%09%09 * doing the following test:%0A%09%09 *  val = System.getProperty(%22java.vm.name%22);%0A%09%09 *  if (val != null && val.toLowerCase().contains(%22dalvik%22)) %7B%0A%09%09 *    ...Do something Androidy to test for 32/64 here...%0A%09%09 *  %7D%0A%09%09 * (Currently, both Dalvik and ART return 'Dalvik').%0A%09%09 * We know of no portable way to detect 32 or 64bittedness%0A%09%09 * on android though, so for now will assume 32.%0A%09%09 */%0A%0A%09%09return %22mupdf_java32%22;
+%09%7D%0A%09%09%09if (initNative() %3C 0)%0A%09%09%09%09throw new RuntimeException(%22cannot initialize mupdf library%22);%0A%09%09%7D
 %0A%09%7D%0A

Simplify loadLibrary call.