Skip to content

Commit

Permalink
Implement a different strategy with sha1 checksum for extracting libr…
Browse files Browse the repository at this point in the history
…aries, fixes #56
  • Loading branch information
gnodet committed Apr 2, 2019
1 parent 40e0b2f commit bd514b7
Showing 1 changed file with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
Expand Down Expand Up @@ -80,8 +82,15 @@
*/
public class Library {

public static final String STRATEGY_PROPERTY = "hawtjni.strategy";
public static final String STRATEGY_SHA1 = "sha1";
public static final String STRATEGY_TEMP = "temp";

static final String SLASH = System.getProperty("file.separator");

static final String STRATEGY = System.getProperty(STRATEGY_PROPERTY,
"windows".equals(getOperatingSystem()) ? STRATEGY_SHA1 : STRATEGY_TEMP);

final private String name;
final private String version;
final private ClassLoader classLoader;
Expand Down Expand Up @@ -310,7 +319,12 @@ private boolean extractAndLoad(ArrayList<Throwable> errors, String customPath, S
file(System.getProperty("user.home"), ".hawtjni", name))) {
if( path!=null ) {
// Try to extract it to the custom path...
File target = extract(errors, resource, prefix, suffix, path);
File target;
if (STRATEGY_SHA1.equals(STRATEGY)) {
target = extractSha1(errors, resource, prefix, suffix, path);
} else {
target = extractTemp(errors, resource, prefix, suffix, path);
}
if( target!=null ) {
if( load(errors, target) ) {
nativeLibrarySourceUrl = resource;
Expand Down Expand Up @@ -348,7 +362,81 @@ private String map(String libName) {
return libName;
}

private File extract(ArrayList<Throwable> errors, URL source, String prefix, String suffix, File directory) {
private File extractSha1(ArrayList<Throwable> errors, URL source, String prefix, String suffix, File directory) {
File target = null;
directory = directory.getAbsoluteFile();
if (!directory.exists()) {
if (!directory.mkdirs()) {
errors.add(new IOException("Unable to create directory: " + directory));
return null;
}
}
try {
String sha1 = computeSha1(source.openStream());
String sha1f = "";
target = new File(directory, prefix + sha1 + suffix);

if (target.isFile() && target.canRead()) {
sha1f = computeSha1(new FileInputStream(target));
}
if (sha1f.equals(sha1)) {
return target;
}

FileOutputStream os = null;
InputStream is = null;
try {
is = source.openStream();
if (is != null) {
byte[] buffer = new byte[4096];
os = new FileOutputStream(target);
int read;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
chmod755(target);
}
return target;
} finally {
close(os);
close(is);
}
} catch (Throwable e) {
IOException io;
if (target != null) {
target.delete();
io = new IOException("Unable to extract library from " + source + " to " + target);
} else {
io = new IOException("Unable to create temporary file in " + directory);
}
io.initCause(e);
errors.add(io);
}
return null;
}

private String computeSha1(InputStream is) throws NoSuchAlgorithmException, IOException {
String sha1;
try {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
int read;
byte[] buffer = new byte[4096];
while ((read = is.read(buffer)) != -1) {
mDigest.update(buffer, 0, read);
}
byte[] result = mDigest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : result) {
sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
}
sha1 = sb.toString();
} finally {
close(is);
}
return sha1;
}

private File extractTemp(ArrayList<Throwable> errors, URL source, String prefix, String suffix, File directory) {
File target = null;
directory = directory.getAbsoluteFile();
if (!directory.exists()) {
Expand Down

0 comments on commit bd514b7

Please sign in to comment.