Advertisement
cyecize

Java CEF - Util Class For Loading cef native lib

Mar 10th, 2019
1,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1. package util;
  2.  
  3. import java.io.*;
  4. import java.lang.reflect.Field;
  5. import java.security.DigestInputStream;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8.  
  9. /**
  10.  * Cef natives file extract helper
  11.  */
  12. public final class CefNativeLibFileExtractor {
  13.  
  14.     private static String[] localeFiles = new String[]{"am.pak", "ar.pak", "bg.pak", "bn.pak", "ca.pak", "cs.pak",
  15.             "da.pak", "de.pak", "el.pak", "en-GB.pak", "en-US.pak", "es-419.pak", "es.pak", "et.pak", "fa.pak", "fi.pak",
  16.             "fil.pak", "fr.pak", "gu.pak", "he.pak", "hi.pak", "hr.pak", "hu.pak", "id.pak", "it.pak", "ja.pak",
  17.             "kn.pak", "ko.pak", "lt.pak", "lv.pak", "ml.pak", "mr.pak", "ms.pak", "nb.pak", "nl.pak", "pl.pak",
  18.             "pt-BR.pak", "pt-PT.pak", "ro.pak", "ru.pak", "sk.pak", "sl.pak", "sr.pak", "sv.pak", "sw.pak", "ta.pak",
  19.             "te.pak", "th.pak", "tr.pak", "uk.pak", "vi.pak", "zh-CN.pak", "zh-TW.pak"};
  20.  
  21.     private static String[] cefFiles = new String[]{
  22.             "cef.pak",
  23.             "cef_100_percent.pak",
  24.             "cef_200_percent.pak",
  25.             "cef_extensions.pak",
  26.             "chrome_elf.dll",
  27.             "d3dcompiler_43.dll",
  28.             "d3dcompiler_47.dll",
  29.             "devtools_resources.pak",
  30.             "icudtl.dat",
  31.             "jcef.dll",
  32.             "jcef.exp",
  33.             "jcef.lib",
  34.             "jcef_helper.exe",
  35.             "libcef.dll",
  36.             "libEGL.dll",
  37.             "libGLESv2.dll",
  38.             "natives_blob.bin",
  39.             "snapshot_blob.bin",
  40.             "v8_context_snapshot.bin",
  41.     };
  42.  
  43.     /**
  44.      * exract native and resource files to temp dir and set to
  45.      * "java.library.path"
  46.      *
  47.      * @throws Exception
  48.      */
  49.     public static void extractNativeAndResource() throws Exception {
  50.         // create temp dir first
  51.         File tempDir = new File(System.getProperty("java.io.tmpdir") + "org.bitbucket.johness.javacef.49.87.win64.1");
  52.         if (!tempDir.exists() && !tempDir.mkdir()) return;
  53.  
  54.         // localesDir
  55.         File localesDir = new File(tempDir.getPath() + File.separator + "locales");
  56.         if (!localesDir.exists() && !localesDir.mkdirs()) return;
  57.  
  58.         // extract cef files
  59.         for (String cefFileName : cefFiles) {
  60.             File cefFile = new File(tempDir.getPath() + File.separator + cefFileName);
  61.             if (cefFile.exists()) {
  62.                 try (FileInputStream ofis = new FileInputStream(cefFile);
  63.                      InputStream nfis = CefNativeLibFileExtractor.class.getResourceAsStream("/" + cefFileName)) {
  64.                     if (equalMD5(ofis, nfis))
  65.                         continue;
  66.                 }
  67.                 cefFile.delete();
  68.                 cefFile.createNewFile();
  69.             } else {
  70.                 cefFile.createNewFile();
  71.             }
  72.             try (InputStream is = CefNativeLibFileExtractor.class.getResourceAsStream("/" + cefFileName);
  73.                  FileOutputStream fos = new FileOutputStream(cefFile)) {
  74.                is.transferTo(fos);
  75.             }
  76.         }
  77.  
  78.         // extrac locale files
  79.         for (String localFileName : localeFiles) {
  80.             File localeFile = new File(localesDir.getPath() + File.separator + localFileName);
  81.             if (localeFile.exists()) {
  82.                 try (FileInputStream ofis = new FileInputStream(localeFile);
  83.                      InputStream nfis = CefNativeLibFileExtractor.class.getResourceAsStream("/locales/" + localFileName)) {
  84.                     if (equalMD5(ofis, nfis))
  85.                         continue;
  86.                 }
  87.                 localeFile.delete();
  88.                 localeFile.createNewFile();
  89.             } else {
  90.                 localeFile.createNewFile();
  91.             }
  92.             try (InputStream is = CefNativeLibFileExtractor.class.getResourceAsStream("/locales/" + localFileName);
  93.                  FileOutputStream fos = new FileOutputStream(localeFile)) {
  94.                 int byteCount = 0;
  95.  
  96.                 byte[] bytes = new byte[1024];
  97.  
  98.                 while ((byteCount = is.read(bytes)) != -1) {
  99.                     fos.write(bytes, 0, byteCount);
  100.                 }
  101.             }
  102.         }
  103.  
  104.         addLibraryDir(tempDir.getPath());
  105.     }
  106.  
  107.     private static void addLibraryDir(String libraryPath) throws Exception {
  108.         Field userPathsField = ClassLoader.class.getDeclaredField("usr_paths");
  109.         userPathsField.setAccessible(true);
  110.         String[] paths = (String[]) userPathsField.get(null);
  111.         StringBuilder sb = new StringBuilder();
  112.         for (int i = 0; i < paths.length; i++) {
  113.             if (libraryPath.equals(paths[i])) {
  114.                 continue;
  115.             }
  116.             sb.append(paths[i]).append(File.pathSeparatorChar);
  117.         }
  118.         sb.append(libraryPath);
  119.         System.setProperty("java.library.path", sb.toString());
  120.         final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
  121.         sysPathsField.setAccessible(true);
  122.         sysPathsField.set(null, null);
  123.     }
  124.  
  125.     public static String byteArrayToHex(byte[] byteArray) {
  126.         StringBuilder hex = new StringBuilder();
  127.         for (int n = 0; n < byteArray.length; n++) {
  128.             String stmp = (Integer.toHexString(byteArray[n] & 0XFF));
  129.             if (stmp.length() == 1)
  130.                 hex.append('0');
  131.             hex.append(stmp);
  132.         }
  133.         return hex.toString();
  134.     }
  135.  
  136.     public static String streamMD5(InputStream is) throws IOException {
  137.         try {
  138.             MessageDigest messageDigest = MessageDigest.getInstance("MD5");
  139.             try (DigestInputStream digestInputStream = new DigestInputStream(is, messageDigest)) {
  140.                 byte[] buffer = new byte[4 * 1024];
  141.                 while (digestInputStream.read(buffer) > 0) ;
  142.                 messageDigest = digestInputStream.getMessageDigest();
  143.                 byte[] resultByteArray = messageDigest.digest();
  144.                 return byteArrayToHex(resultByteArray);
  145.             }
  146.         } catch (NoSuchAlgorithmException e) {
  147.             return null;
  148.         }
  149.     }
  150.  
  151.     public static boolean equalMD5(InputStream is0, InputStream is1) {
  152.         try {
  153.             String md50 = streamMD5(is0);
  154.             String md51 = streamMD5(is1);
  155.             if (md50 == null || md51 == null) return false;
  156.             return md50.equals(md51);
  157.         } catch (Exception e) {
  158.             return false;
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement