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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.herac.tuxguitar.player.impl.midiport.oss;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.herac.tuxguitar.util.TGClassLoader;

public class JNILibraryLoader {
	
	private static final String JNI_EXTENSION = ".jnilib";
	
	private static final String JNI_TMP_PATH = (System.getProperty( "java.io.tmpdir" ) + File.separator);
	
	public static void loadLibrary(String libname){
		if(!JNILibraryLoader.loadFromClassPath(libname + JNI_EXTENSION)){
			System.loadLibrary(libname);
		}
	}
	
	private static boolean loadFromClassPath(String filename){
		File file = new File(JNI_TMP_PATH + filename);
		try{
			if(!file.exists()){
				InputStream inputStream = TGClassLoader.instance().getClassLoader().getResourceAsStream(filename);
				if (inputStream != null) {
					OutputStream outputStream = new FileOutputStream(file);
					
					int read;
					byte [] buffer = new byte [4096];
					while ((read = inputStream.read (buffer)) != -1) {
						outputStream.write(buffer, 0, read);
					}
					outputStream.close();
					inputStream.close();
				}
			}
			if(file.exists()){
				System.load(file.getAbsolutePath());
				return true;
			}
		}catch(Throwable throwable){
			return false;
		}finally{
			if(file.exists()){
				file.delete();
			}
		}
		return false;
	}
}