~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar-oss/src/org/herac/tuxguitar/player/impl/midiport/oss/JNILibraryLoader.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.player.impl.midiport.oss;
 
2
 
 
3
import java.io.File;
 
4
import java.io.FileOutputStream;
 
5
import java.io.InputStream;
 
6
import java.io.OutputStream;
 
7
 
 
8
import org.herac.tuxguitar.util.TGClassLoader;
 
9
 
 
10
public class JNILibraryLoader {
 
11
        
 
12
        private static final String JNI_EXTENSION = ".jnilib";
 
13
        
 
14
        private static final String JNI_TMP_PATH = (System.getProperty( "java.io.tmpdir" ) + File.separator);
 
15
        
 
16
        public static void loadLibrary(String libname){
 
17
                if(!JNILibraryLoader.loadFromClassPath(libname + JNI_EXTENSION)){
 
18
                        System.loadLibrary(libname);
 
19
                }
 
20
        }
 
21
        
 
22
        private static boolean loadFromClassPath(String filename){
 
23
                File file = new File(JNI_TMP_PATH + filename);
 
24
                try{
 
25
                        if(!file.exists()){
 
26
                                InputStream inputStream = TGClassLoader.instance().getClassLoader().getResourceAsStream(filename);
 
27
                                if (inputStream != null) {
 
28
                                        OutputStream outputStream = new FileOutputStream(file);
 
29
                                        
 
30
                                        int read;
 
31
                                        byte [] buffer = new byte [4096];
 
32
                                        while ((read = inputStream.read (buffer)) != -1) {
 
33
                                                outputStream.write(buffer, 0, read);
 
34
                                        }
 
35
                                        outputStream.close();
 
36
                                        inputStream.close();
 
37
                                }
 
38
                        }
 
39
                        if(file.exists()){
 
40
                                System.load(file.getAbsolutePath());
 
41
                                return true;
 
42
                        }
 
43
                }catch(Throwable throwable){
 
44
                        return false;
 
45
                }finally{
 
46
                        if(file.exists()){
 
47
                                file.delete();
 
48
                        }
 
49
                }
 
50
                return false;
 
51
        }
 
52
}