2019-10-16 07:49:58 +02:00
|
|
|
/*
|
2020-04-11 12:33:05 +02:00
|
|
|
* Copyright (C) 2010 Ryszard Wiśniewski <brut.alll@gmail.com>
|
|
|
|
* Copyright (C) 2010 Connor Tumbleson <connor.tumbleson@gmail.com>
|
2012-09-20 03:19:12 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package brut.util;
|
|
|
|
|
2019-12-19 11:39:04 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2017-06-29 17:30:00 +02:00
|
|
|
|
2019-12-19 11:39:04 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2012-09-20 03:19:12 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2019-12-19 11:39:04 +01:00
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
2017-06-29 17:30:00 +02:00
|
|
|
|
2019-12-19 11:39:04 +01:00
|
|
|
import brut.common.BrutException;
|
2012-09-20 03:19:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
|
|
|
|
*/
|
|
|
|
abstract public class Jar {
|
|
|
|
private final static Set<String> mLoaded = new HashSet<String>();
|
2017-06-29 17:30:00 +02:00
|
|
|
private final static Map<String, File> mExtracted = new HashMap<String, File>();
|
|
|
|
|
|
|
|
public static File getResourceAsFile(String name, Class clazz) throws BrutException {
|
2012-09-20 03:19:12 +02:00
|
|
|
File file = mExtracted.get(name);
|
|
|
|
if (file == null) {
|
2017-06-29 13:36:05 +02:00
|
|
|
file = extractToTmp(name, clazz);
|
2012-09-20 03:19:12 +02:00
|
|
|
mExtracted.put(name, file);
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2017-06-29 13:36:05 +02:00
|
|
|
public static File getResourceAsFile(String name) throws BrutException {
|
|
|
|
return getResourceAsFile(name, Class.class);
|
|
|
|
}
|
|
|
|
|
2012-09-20 03:19:12 +02:00
|
|
|
public static void load(String libPath) {
|
|
|
|
if (mLoaded.contains(libPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
File libFile;
|
|
|
|
try {
|
|
|
|
libFile = getResourceAsFile(libPath);
|
|
|
|
} catch (BrutException ex) {
|
|
|
|
throw new UnsatisfiedLinkError(ex.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
System.load(libFile.getAbsolutePath());
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:30:00 +02:00
|
|
|
public static File extractToTmp(String resourcePath) throws BrutException {
|
|
|
|
return extractToTmp(resourcePath, Class.class);
|
|
|
|
}
|
|
|
|
|
2017-06-29 13:36:05 +02:00
|
|
|
public static File extractToTmp(String resourcePath, Class clazz) throws BrutException {
|
|
|
|
return extractToTmp(resourcePath, "brut_util_Jar_", clazz);
|
|
|
|
}
|
2017-06-29 17:30:00 +02:00
|
|
|
|
|
|
|
public static File extractToTmp(String resourcePath, String tmpPrefix) throws BrutException {
|
|
|
|
return extractToTmp(resourcePath, tmpPrefix, Class.class);
|
2012-09-20 03:19:12 +02:00
|
|
|
}
|
|
|
|
|
2017-06-29 17:30:00 +02:00
|
|
|
public static File extractToTmp(String resourcePath, String tmpPrefix, Class clazz) throws BrutException {
|
2012-09-20 03:19:12 +02:00
|
|
|
try {
|
2017-06-29 13:36:05 +02:00
|
|
|
InputStream in = clazz.getResourceAsStream(resourcePath);
|
2012-09-20 03:19:12 +02:00
|
|
|
if (in == null) {
|
|
|
|
throw new FileNotFoundException(resourcePath);
|
|
|
|
}
|
2020-04-11 12:50:09 +02:00
|
|
|
long suffix = ThreadLocalRandom.current().nextLong();
|
|
|
|
suffix = suffix == Long.MIN_VALUE ? 0 : Math.abs(suffix);
|
|
|
|
File fileOut = File.createTempFile(tmpPrefix, suffix + ".tmp");
|
2012-09-20 03:19:12 +02:00
|
|
|
fileOut.deleteOnExit();
|
2020-04-11 12:50:09 +02:00
|
|
|
|
2012-09-20 03:19:12 +02:00
|
|
|
OutputStream out = new FileOutputStream(fileOut);
|
|
|
|
IOUtils.copy(in, out);
|
|
|
|
in.close();
|
|
|
|
out.close();
|
2020-04-11 12:50:09 +02:00
|
|
|
|
2012-09-20 03:19:12 +02:00
|
|
|
return fileOut;
|
|
|
|
} catch (IOException ex) {
|
2017-06-29 17:30:00 +02:00
|
|
|
throw new BrutException("Could not extract resource: " + resourcePath, ex);
|
2012-09-20 03:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|