Skip to content

Commit 99e8e70

Browse files
committed
refactor: use java.nio api
Signed-off-by: lewisbirks <22620804+lewisbirks@users.noreply.github.com>
1 parent e799192 commit 99e8e70

1 file changed

Lines changed: 27 additions & 25 deletions

File tree

src/main/java/io/jenkins/plugins/pipelinegraphview/utils/PipelineGraphViewCache.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
import com.github.benmanes.caffeine.cache.Caffeine;
77
import java.io.BufferedInputStream;
88
import java.io.BufferedOutputStream;
9-
import java.io.File;
10-
import java.io.FileInputStream;
11-
import java.io.FileOutputStream;
129
import java.io.IOException;
1310
import java.io.InputStream;
1411
import java.io.OutputStream;
1512
import java.nio.charset.StandardCharsets;
1613
import java.nio.file.AtomicMoveNotSupportedException;
1714
import java.nio.file.Files;
15+
import java.nio.file.Path;
1816
import java.nio.file.StandardCopyOption;
1917
import java.util.function.Supplier;
2018
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -78,12 +76,12 @@ public boolean tryServeAllSteps(WorkflowRun run, OutputStream out) throws IOExce
7876
return tryServe(allStepsFile(run), out);
7977
}
8078

81-
private boolean tryServe(File file, OutputStream out) throws IOException {
82-
if (!file.isFile()) {
79+
private boolean tryServe(Path file, OutputStream out) throws IOException {
80+
if (!Files.exists(file)) {
8381
return false;
8482
}
8583
out.write(ENVELOPE_PREFIX);
86-
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
84+
try (InputStream in = new BufferedInputStream(Files.newInputStream(file))) {
8785
in.transferTo(out);
8886
}
8987
out.write(ENVELOPE_SUFFIX);
@@ -141,55 +139,59 @@ public void seed(WorkflowRun run, PipelineGraph graph, PipelineStepList allSteps
141139
}
142140

143141
/** Returns the JSON-decoded value at {@code source} or {@code null} if the file is absent or unreadable. */
144-
private <T> T readJson(File source, Class<T> type) {
145-
if (!source.isFile()) {
142+
private <T> T readJson(Path source, Class<T> type) {
143+
if (!Files.exists(source)) {
146144
return null;
147145
}
148-
try (InputStream in = new BufferedInputStream(new FileInputStream(source))) {
146+
try (InputStream in = new BufferedInputStream(Files.newInputStream(source))) {
149147
return MAPPER.readValue(in, type);
150148
} catch (IOException e) {
151149
// A corrupt/older file shouldn't wedge the cache: drop it and fall back to compute.
152-
logger.warn("Failed to read pipeline graph cache for {}; recomputing", source.getName(), e);
150+
logger.warn("Failed to read pipeline graph cache for {}; recomputing", source.getFileName(), e);
153151
return null;
154152
}
155153
}
156154

157-
private void writeJson(File target, Object data) {
158-
File dir = target.getParentFile();
159-
File tmp = null;
155+
private void writeJson(Path target, Object data) {
156+
Path dir = target.getParent();
157+
Path tmp = null;
160158
try {
161-
tmp = File.createTempFile(target.getName() + ".", ".tmp", dir);
162-
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(tmp))) {
159+
tmp = Files.createTempFile(dir, target.getFileName() + ".", ".tmp");
160+
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmp))) {
163161
MAPPER.writeValue(os, data);
164162
}
165163
try {
166164
Files.move(
167-
tmp.toPath(),
168-
target.toPath(),
165+
tmp,
166+
target,
169167
StandardCopyOption.ATOMIC_MOVE,
170168
StandardCopyOption.REPLACE_EXISTING);
171169
} catch (AtomicMoveNotSupportedException e) {
172-
Files.move(tmp.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
170+
Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING);
173171
}
174172
tmp = null;
175173
// Best-effort cleanup of any pre-v1 XStream cache left behind by older versions
176174
// of the plugin.
177-
new File(dir, LEGACY_XSTREAM_FILE_NAME).delete();
175+
Files.deleteIfExists(dir.resolve(LEGACY_XSTREAM_FILE_NAME));
178176
} catch (IOException e) {
179-
logger.warn("Failed to write pipeline graph cache for {}", target.getName(), e);
177+
logger.warn("Failed to write pipeline graph cache for {}", target.getFileName(), e);
180178
} finally {
181179
if (tmp != null) {
182-
tmp.delete();
180+
try {
181+
Files.deleteIfExists(tmp);
182+
} catch (IOException e) {
183+
logger.warn("Failed to delete temporary pipeline graph cache file", e);
184+
}
183185
}
184186
}
185187
}
186188

187-
private File treeFile(WorkflowRun run) {
188-
return new File(run.getRootDir(), TREE_FILE_NAME);
189+
private Path treeFile(WorkflowRun run) {
190+
return run.getRootDir().toPath().resolve(TREE_FILE_NAME);
189191
}
190192

191-
private File allStepsFile(WorkflowRun run) {
192-
return new File(run.getRootDir(), ALL_STEPS_FILE_NAME);
193+
private Path allStepsFile(WorkflowRun run) {
194+
return run.getRootDir().toPath().resolve(ALL_STEPS_FILE_NAME);
193195
}
194196

195197
/** Test hook: drop in-memory entries so the next call re-runs the supplier. */

0 commit comments

Comments
 (0)