Fix 'Resource leak: <variable> is never closed' warnings (#2023)

Fix numerous places where using AutoCloseable objects without closing
them.

Changes:
- Upgrade JUnit from 5.10.0 to 5.11.4 (so `@AutoClose` can be used)
- Use `Files.copy()` to copy files
- Use try-with-resources when calling `Files.list()` or `Files.walk()`
- Use try-with-resources or `@AutoClose` to close `PhotonCamera` and
  `PhotonCameraSim` objects created by tests
- Update `SQLConfigTest` to use `@TempDir`

## Meta

Merge checklist:
- [x] Pull Request title is [short, imperative
summary](https://cbea.ms/git-commit/) of proposed changes
- [x] 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 v2024.3.1
- [ ] 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:
Kevin Cooney
2025-08-18 20:37:00 -07:00
committed by GitHub
parent bd2c5062f9
commit 7d927aca3b
10 changed files with 115 additions and 125 deletions

View File

@@ -336,14 +336,10 @@ public class RequestHandler {
return;
}
try {
Path filePath =
Paths.get(ProgramDirectoryUtilities.getProgramDirectory(), "photonvision.jar");
File targetFile = new File(filePath.toString());
var stream = new FileOutputStream(targetFile);
file.content().transferTo(stream);
stream.close();
Path targetPath =
Paths.get(ProgramDirectoryUtilities.getProgramDirectory(), "photonvision.jar");
try (InputStream fileSteam = file.content()) {
Files.copy(fileSteam, targetPath);
ctx.status(200);
ctx.result(
@@ -655,8 +651,8 @@ public class RequestHandler {
return;
}
try (FileOutputStream out = new FileOutputStream(modelPath.toFile())) {
modelFile.content().transferTo(out);
try (InputStream modelFileStream = modelFile.content()) {
Files.copy(modelFileStream, modelPath);
}
ModelProperties modelProperties =