[NFC] Kill stupid while loop copy in PhotonJNICommon (#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
This commit is contained in:
Matt Morley
2025-12-06 13:51:14 -08:00
committed by GitHub
parent 5e830fae57
commit 674f6e2361

View File

@@ -17,9 +17,9 @@
package org.photonvision.jni;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.logging.LogGroup;
@@ -35,42 +35,35 @@ public abstract class PhotonJNICommon {
protected static synchronized void forceLoad(
PhotonJNICommon instance, Class<?> clazz, List<String> libraries) throws IOException {
if (instance.isLoaded()) return;
if (logger == null) logger = new Logger(clazz, LogGroup.Camera);
if (logger == null) logger = new Logger(clazz, LogGroup.General);
for (var libraryName : libraries) {
try {
// We always extract the shared object (we could hash each so, but that's a lot of work)
var arch_name = Platform.getNativeLibraryFolderName();
var nativeLibName = System.mapLibraryName(libraryName);
var in = clazz.getResourceAsStream("/nativelibraries/" + arch_name + "/" + nativeLibName);
logger.info("Loading " + libraryName);
// We always extract the shared object (we could hash each so, but that's a lot
// of work)
var arch_name = Platform.getNativeLibraryFolderName();
var nativeLibName = System.mapLibraryName(libraryName);
try (var in =
clazz.getResourceAsStream("/nativelibraries/" + arch_name + "/" + nativeLibName)) {
if (in == null) {
logger.error("Could not find " + libraryName);
instance.setLoaded(false);
return;
}
// It's important that we don't mangle the names of these files on Windows at least
File temp = new File(System.getProperty("java.io.tmpdir"), nativeLibName);
FileOutputStream fos = new FileOutputStream(temp);
// It's important that we don't mangle the names of these files
var temp = Files.createTempDirectory("nativeExtract").resolve(nativeLibName);
Files.copy(in, temp, StandardCopyOption.REPLACE_EXISTING);
int read = -1;
byte[] buffer = new byte[1024];
while ((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
try {
System.load(temp.toAbsolutePath().toString());
logger.info("Successfully loaded shared object " + temp.getFileName());
} catch (UnsatisfiedLinkError e) {
logger.error("Couldn't load shared object " + libraryName, e);
e.printStackTrace();
instance.setLoaded(false);
return;
}
fos.close();
in.close();
System.load(temp.getAbsolutePath());
logger.info("Successfully loaded shared object " + temp.getName());
} catch (UnsatisfiedLinkError e) {
logger.error("Couldn't load shared object " + libraryName, e);
e.printStackTrace();
// logger.error(System.getProperty("java.library.path"));
instance.setLoaded(false);
return;
}
}
instance.setLoaded(true);