diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java index bcaeb1d79eb..6b5fd97921f 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java @@ -69,7 +69,6 @@ import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -85,12 +84,12 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import static io.fabric8.kubernetes.client.utils.internal.OptionalDependencyWrapper.wrapRunWithOptionalDependency; @@ -477,7 +476,7 @@ private void copyFile(String source, File target) { destination = destination.toPath().resolve(filename).toFile(); } - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(destination))) { + try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(destination.toPath()))) { ExecWatch w = writingOutput(out).exec(readFileCommand(source)); w.exitCode().get(); } catch (Exception e) { @@ -668,7 +667,7 @@ public Pod patchReadinessGateStatus(Map readiness) { } } } - pod.getStatus().setConditions(conditions.values().stream().collect(Collectors.toList())); + pod.getStatus().setConditions(new ArrayList<>(conditions.values())); return this.resource(pod).subresource("status").patch(); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUpload.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUpload.java index 247e0e6ee53..a370aaf58df 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUpload.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/PodUpload.java @@ -33,6 +33,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Objects; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -51,12 +52,13 @@ private PodUpload() { public static boolean upload(PodOperationsImpl operation, Path pathToUpload) throws IOException { - if (Utils.isNotNullOrEmpty(operation.getContext().getFile()) && pathToUpload.toFile().isFile()) { + final File toUpload = pathToUpload.toFile(); + if (Utils.isNotNullOrEmpty(operation.getContext().getFile()) && toUpload.isFile()) { return uploadTar(operation, getDirectoryFromFile(operation.getContext().getFile()), - tar -> addFileToTar(new File(operation.getContext().getFile()).getName(), pathToUpload.toFile(), tar)); - } else if (Utils.isNotNullOrEmpty(operation.getContext().getDir()) && pathToUpload.toFile().isDirectory()) { + tar -> addFileToTar(new File(operation.getContext().getFile()).getName(), toUpload, tar)); + } else if (Utils.isNotNullOrEmpty(operation.getContext().getDir()) && toUpload.isDirectory()) { return uploadTar(operation, ensureEndsWithSlash(operation.getContext().getDir()), tar -> { - for (File file : pathToUpload.toFile().listFiles()) { + for (File file : Objects.requireNonNull(toUpload.listFiles())) { addFileToTar(file.getName(), file, tar); } }); @@ -69,12 +71,6 @@ private static String getDirectoryFromFile(String file) { return ensureEndsWithSlash(directoryTrimmedFromFilePath.isEmpty() ? "/" : directoryTrimmedFromFilePath); } - private interface UploadProcessor { - - void process(T out) throws IOException; - - } - private static boolean upload(PodOperationsImpl operation, String file, UploadProcessor processor) throws IOException { @@ -102,8 +98,8 @@ private static boolean upload(PodOperationsImpl operation, String file, UploadPr LOG.debug("failed to complete upload before timeout expired"); return false; } - Integer exitCode = exitFuture.getNow(null); - if (exitCode != null && exitCode.intValue() != 0) { + final Integer exitCode = exitFuture.getNow(null); + if (exitCode != null && exitCode != 0) { LOG.debug("upload process failed with exit code {}", exitCode); return false; } @@ -180,7 +176,7 @@ private static void addFileToTar(String fileName, File file, TarArchiveOutputStr tar.closeArchiveEntry(); } else if (file.isDirectory()) { tar.closeArchiveEntry(); - for (File fileInDirectory : file.listFiles()) { + for (File fileInDirectory : Objects.requireNonNull(file.listFiles())) { addFileToTar(fileName + TAR_PATH_DELIMITER + fileInDirectory.getName(), fileInDirectory, tar); } } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/UploadProcessor.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/UploadProcessor.java new file mode 100644 index 00000000000..c3eb3da352f --- /dev/null +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/uploadable/UploadProcessor.java @@ -0,0 +1,26 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.kubernetes.client.dsl.internal.uploadable; + +import java.io.IOException; +import java.io.OutputStream; + +@FunctionalInterface +interface UploadProcessor { + + void process(T out) throws IOException; + +}