Advertisement
Guest User

lwjgl load natives

a guest
Oct 15th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. /**
  2.  * Based on http://frommyplayground.com/how-to-load-native-jni-library-from-jar by Adam Heirnich <adam@adamh.cz>
  3.  */
  4. public class NativeUtils {
  5.  
  6.     private final static String DIR_NATIVES = "lwjgl-natives";
  7.    
  8.     private NativeUtils() {}
  9.    
  10.     public static File createTempDir() throws IOException {
  11.         final File tempDir = File.createTempFile(DIR_NATIVES, "");
  12.         tempDir.delete();
  13.         tempDir.mkdir();
  14.         tempDir.deleteOnExit();
  15.        
  16.         return tempDir;
  17.     }
  18.    
  19.     public static void loadLibraryFromJar(File parent, String library) throws IOException {
  20.         if (!library.startsWith("/")) throw new IllegalArgumentException("The path to be absolute (start with '/').");
  21.        
  22.         final String filename = library.substring(library.lastIndexOf('/'));            // Obtain filename from path
  23.  
  24.         if (filename == null)                                                           // Check if the filename is okay
  25.            throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
  26.        
  27.         final File temp = new File(parent, filename);                                   // Prepare temporary file
  28.         temp.createNewFile();
  29.         temp.deleteOnExit();
  30.  
  31.         if (!temp.exists()) throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist.");
  32.  
  33.         final byte[] buffer =   new byte[1024];                                         // Prepare buffer for data copying
  34.         int readBytes =         0;
  35.  
  36.         final InputStream is = NativeUtils.class.getResourceAsStream(library);          // Open and check input stream
  37.         if (is == null)
  38.             throw new FileNotFoundException("File " + library + " was not found inside JAR.");
  39.        
  40.         final OutputStream os = new FileOutputStream(temp);                             // Open output stream and copy data between                                                                                
  41.         try {                                                                           // source file in JAR and the temporary file
  42.             while ((readBytes = is.read(buffer)) != -1)
  43.                 os.write(buffer, 0, readBytes);
  44.         } finally {                                                                     // If read/write fails, close streams
  45.             os.close();                                                                 // safely before throwing an exception
  46.             is.close();
  47.         }
  48.     }
  49.    
  50.     public static void deleteOldDir() {
  51.         final FileFilter tmpDirFilter = new FileFilter() {
  52.             public boolean accept(File pathname) {
  53.                 return pathname.getName().startsWith(DIR_NATIVES);
  54.             }
  55.         };
  56.        
  57.         final String tmpDirName = System.getProperty("java.io.tmpdir");
  58.         File[] tmpFiles = new File(tmpDirName).listFiles(tmpDirFilter);
  59.        
  60.         for (File f : tmpFiles) delete(f);
  61.     }
  62.    
  63.     private static void delete(File file) {
  64.         if (file.isDirectory())
  65.             for (File f : file.listFiles())
  66.                 delete(f);
  67.         else file.delete();
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement