Skip to content

Commit a524c8a

Browse files
mcm001spacey-sooty
authored andcommitted
[NFC] Kill stupid while loop copy in PhotonJNICommon (PhotonVision#2219)
## Description We can avoid copying files by chunks just using `Files.copy`. This should be NFC, just makes the code cleaner <!-- What changed? Why? (the code + comments should speak for itself on the "how") --> <!-- Fun screenshots or a cool video or something are super helpful as well. If this touches platform-specific behavior, this is where test evidence should be collected. --> <!-- Any issues this pull request closes or pull requests this supersedes should be linked with `Closes #issuenumber`. --> ## Meta Merge checklist: - [x] Pull Request title is [short, imperative summary](https://cbea.ms/git-commit/) of proposed changes - [ ] The description documents the _what_ and _why_ - [ ] If this PR changes behavior or adds a feature, user documentation is updated - [ ] If this PR touches photon-serde, all messages have been regenerated and hashes have not changed unexpectedly - [ ] If this PR touches configuration, this is backwards compatible with settings back to v2025.3.2 - [ ] If this PR touches pipeline settings or anything related to data exchange, the frontend typing is updated - [ ] If this PR addresses a bug, a regression test for it is added
1 parent e184d38 commit a524c8a

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

photon-core/src/main/java/org/photonvision/jni/PhotonJNICommon.java

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
package org.photonvision.jni;
1919

20-
import java.io.File;
21-
import java.io.FileOutputStream;
2220
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.StandardCopyOption;
2323
import java.util.List;
2424
import org.photonvision.common.hardware.Platform;
2525
import org.photonvision.common.logging.LogGroup;
@@ -35,42 +35,35 @@ public abstract class PhotonJNICommon {
3535
protected static synchronized void forceLoad(
3636
PhotonJNICommon instance, Class<?> clazz, List<String> libraries) throws IOException {
3737
if (instance.isLoaded()) return;
38-
if (logger == null) logger = new Logger(clazz, LogGroup.Camera);
38+
if (logger == null) logger = new Logger(clazz, LogGroup.General);
3939

4040
for (var libraryName : libraries) {
41-
try {
42-
// We always extract the shared object (we could hash each so, but that's a lot of work)
43-
var arch_name = Platform.getNativeLibraryFolderName();
44-
var nativeLibName = System.mapLibraryName(libraryName);
45-
var in = clazz.getResourceAsStream("/nativelibraries/" + arch_name + "/" + nativeLibName);
46-
41+
logger.info("Loading " + libraryName);
42+
// We always extract the shared object (we could hash each so, but that's a lot
43+
// of work)
44+
var arch_name = Platform.getNativeLibraryFolderName();
45+
var nativeLibName = System.mapLibraryName(libraryName);
46+
try (var in =
47+
clazz.getResourceAsStream("/nativelibraries/" + arch_name + "/" + nativeLibName)) {
4748
if (in == null) {
49+
logger.error("Could not find " + libraryName);
4850
instance.setLoaded(false);
4951
return;
5052
}
5153

52-
// It's important that we don't mangle the names of these files on Windows at least
53-
File temp = new File(System.getProperty("java.io.tmpdir"), nativeLibName);
54-
FileOutputStream fos = new FileOutputStream(temp);
54+
// It's important that we don't mangle the names of these files
55+
var temp = Files.createTempDirectory("nativeExtract").resolve(nativeLibName);
56+
Files.copy(in, temp, StandardCopyOption.REPLACE_EXISTING);
5557

56-
int read = -1;
57-
byte[] buffer = new byte[1024];
58-
while ((read = in.read(buffer)) != -1) {
59-
fos.write(buffer, 0, read);
58+
try {
59+
System.load(temp.toAbsolutePath().toString());
60+
logger.info("Successfully loaded shared object " + temp.getFileName());
61+
} catch (UnsatisfiedLinkError e) {
62+
logger.error("Couldn't load shared object " + libraryName, e);
63+
e.printStackTrace();
64+
instance.setLoaded(false);
65+
return;
6066
}
61-
fos.close();
62-
in.close();
63-
64-
System.load(temp.getAbsolutePath());
65-
66-
logger.info("Successfully loaded shared object " + temp.getName());
67-
68-
} catch (UnsatisfiedLinkError e) {
69-
logger.error("Couldn't load shared object " + libraryName, e);
70-
e.printStackTrace();
71-
// logger.error(System.getProperty("java.library.path"));
72-
instance.setLoaded(false);
73-
return;
7467
}
7568
}
7669
instance.setLoaded(true);

0 commit comments

Comments
 (0)