OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_04/usb_fpga_2_04/trunk

Subversion Repositories usb_fpga_2_04

[/] [usb_fpga_2_04/] [trunk/] [libusbJava-src/] [ch/] [ntb/] [usb/] [LibLoader.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*
2
 * library loader with the ability to load libraries as system resource (e.g. form the current .jar file)
3
 * Copyright (c) 2007-2009, ZTEX e.K.
4
 * http://www.ztex.de
5
 *
6
 * This library is covered by the LGPL, read LGPL.txt for details.
7
 */
8
 
9
package ch.ntb.usb;
10
 
11
import java.io.*;
12
 
13
/**
14
 * This class allows to load libraries in the normal way or as a system resource (e.g. form the current .jar file).
15
 * See below for a further description. <br>
16
 *
17
 * @author Stefan Ziegenbalg
18
 *
19
 */
20
public class LibLoader {
21
 
22
/**
23
 * Loads a library. This is done in three steps.<br>
24
 * 1. The library is tried to be load from the path list specified by the java.library.path property. <br>
25
 * 2. The library is tried to be load from the current directory. <br>
26
 * 3. The library is searched as a system resource (e.g. in the current .jar file),
27
 * copied to to temporary directory and loaded from there. Afterwards the temporary library is deleted.
28
 * The copying is necessary because libraries can't be loaded directly from .jar files.<br>
29
 *
30
 * @param libName Library name (e.g. usbJava)
31
 *
32
 * @throws UnsatisfiedLinkError
33
 *
34
 */
35
    public static void load( String libName ) {
36
 
37
// Step 1: Normal way
38
        try {
39
            System.loadLibrary( libName );
40
        }
41
        catch ( Throwable e1 ) {
42
 
43
// Step 2: From the current directory
44
            String basename = System.mapLibraryName( libName );
45
            try {
46
                System.load( System.getProperty("user.dir") + System.getProperty("file.separator") + basename );
47
            }
48
            catch ( Throwable e2 ) {
49
 
50
// Step 2: As system ressource
51
                String libFileName = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + basename;
52
                try {
53
                    InputStream inputStream = ClassLoader.getSystemResourceAsStream( basename );
54
                    if ( inputStream == null ) {
55
                        throw new Exception();
56
                    }
57
                    File libFile = new File( libFileName );
58
 
59
                    FileOutputStream outputStream = new FileOutputStream( libFile );
60
 
61
                    byte[] buf = new byte[65536];
62
                    int bytesRead = -1;
63
                    while ( (bytesRead = inputStream.read(buf)) > 0 ) {
64
                        outputStream.write(buf, 0, bytesRead);
65
                    }
66
                    outputStream.close();
67
                    inputStream.close();
68
 
69
                    System.load( libFileName );
70
 
71
                    try {
72
                        libFile.delete();
73
                    }
74
                    catch (Exception e3) {
75
//                      System.err.println( "Warning: Cannot delete temporary library file `" + libFileName + "'" );
76
                    }
77
                }
78
                catch (Exception e3) {
79
                    throw new UnsatisfiedLinkError ("Library `"+basename+"' cannot be loaded as system resource, from current directory or from java.library.path   (java.library.path=" + System.getProperty("java.library.path")+")" );
80
                }
81
            }
82
        }
83
   }
84
 
85
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.