Skip to content

Commit

Permalink
Fix some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed Nov 16, 2018
1 parent e352547 commit b8e6320
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
41 changes: 21 additions & 20 deletions src/main/java/org/scijava/nativelib/BaseJniExtractor.java
Expand Up @@ -82,7 +82,7 @@ public BaseJniExtractor(final Class<?> libraryJarClass) throws IOException {
init(libraryJarClass);
}

private void init(final Class<?> libraryJarClass) throws IOException {
private void init(final Class<?> libraryJarClass) {
this.libraryJarClass = libraryJarClass;

final String mxSysInfo = MxSysInfo.getMxSysInfo();
Expand Down Expand Up @@ -140,7 +140,7 @@ protected static File getTempDir() throws IOException {
*/
public abstract File getJniDir();

/** {@inheritDoc} */
@Override
public File extractJni(final String libPath, final String libname)
throws IOException
{
Expand Down Expand Up @@ -196,7 +196,7 @@ else if (mappedlibName.endsWith(".dylib")) {
return null;
}

/** {@inheritDoc} */
@Override
public void extractRegistered() throws IOException {
debug("Extracting libraries registered in classloader " +
this.getClass().getClassLoader());
Expand Down Expand Up @@ -257,25 +257,25 @@ private void extractLibrariesFromResource(final URL resource)
File extractResource(final File dir, final URL resource,
final String outputName) throws IOException
{
try (final InputStream in = resource.openStream()) {
// TODO there's also a getResourceAsStream

// make a lib file with exactly the same lib name
final File outfile = new File(getJniDir(), outputName);
debug("Extracting '" + resource + "' to '" +
outfile.getAbsolutePath() + "'");

// copy resource stream to temporary file
try (final FileOutputStream out = new FileOutputStream(outfile)) {
copy(in, out);
out.close();
}

final InputStream in = resource.openStream();
// TODO there's also a getResourceAsStream

// make a lib file with exactly the same lib name
final File outfile = new File(getJniDir(), outputName);
debug("Extracting '" + resource + "' to '" +
outfile.getAbsolutePath() + "'");

// copy resource stream to temporary file
final FileOutputStream out = new FileOutputStream(outfile);
copy(in, out);
out.close();
in.close();

// note that this doesn't always work:
outfile.deleteOnExit();
// note that this doesn't always work:
outfile.deleteOnExit();

return outfile;
return outfile;
}
}

/**
Expand All @@ -298,6 +298,7 @@ void deleteLeftoverFiles() {
final File tmpDirectory = new File(System.getProperty(JAVA_TMPDIR, ALTR_TMPDIR));
final File[] folders = tmpDirectory.listFiles(new FilenameFilter() {

@Override
public boolean accept(final File dir, final String name) {
return name.startsWith(TMP_PREFIX);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/scijava/nativelib/NativeLibraryUtil.java
Expand Up @@ -302,9 +302,9 @@ public static boolean loadNativeLibrary(final JniExtractor jniExtractor,
}
else {
try {
List<String> libPaths = searchPaths == null ?
new LinkedList<String>() :
new LinkedList<String>(Arrays.asList(searchPaths));
final List<String> libPaths = searchPaths == null ?
new LinkedList<>() :
new LinkedList<>(Arrays.asList(searchPaths));
libPaths.add(0, NativeLibraryUtil.DEFAULT_SEARCH_PATH);
// for backward compatibility
libPaths.add(1, "");
Expand Down
32 changes: 16 additions & 16 deletions src/test/java/org/scijava/nativelib/NativeLoaderTest.java
Expand Up @@ -56,21 +56,21 @@ private void createJar() throws Exception {
File dummyJar = tmpTestDir.newFile("dummy.jar");
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream(dummyJar), manifest);
try (final JarOutputStream target = new JarOutputStream(new FileOutputStream(dummyJar), manifest)) {

// with a dummy binary in it
File source = new File(String.format("natives/%s/%s",
// with a dummy binary in it
File source = new File(String.format("natives/%s/%s",
NativeLibraryUtil.getArchitecture().name().toLowerCase(),
NativeLibraryUtil.getPlatformLibraryName("dummy")));
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
entry.setTime(System.currentTimeMillis());
target.putNextEntry(entry);
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
entry.setTime(System.currentTimeMillis());
target.putNextEntry(entry);

// fill the file...
byte[] buffer = "native-lib-loader".getBytes();
target.write(buffer, 0, buffer.length);
target.closeEntry();
target.close();
// fill the file...
byte[] buffer = "native-lib-loader".getBytes();
target.write(buffer, 0, buffer.length);
target.closeEntry();
}

// and add to classpath as if it is a dependency of the project
Method addURLMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
Expand All @@ -96,10 +96,10 @@ public void testExtracting() throws Exception {
NativeLibraryUtil.getArchitecture().name().toLowerCase());
File extracted = jniExtractor.extractJni(libPath + "", "dummy");

FileInputStream in = new FileInputStream(extracted);
byte[] buffer = new byte[32];
in.read(buffer, 0, buffer.length);
in.close();
assertTrue(new String(buffer).trim().equals("native-lib-loader"));
try (final FileInputStream in = new FileInputStream(extracted)) {
byte[] buffer = new byte[32];
in.read(buffer, 0, buffer.length);
assertTrue(new String(buffer).trim().equals("native-lib-loader"));
}
}
}

0 comments on commit b8e6320

Please sign in to comment.