Skip to content

Commit

Permalink
Prevent npe
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Apr 18, 2024
1 parent 8b9aa36 commit cdd9109
Showing 1 changed file with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

import static java.lang.Math.min;
Expand Down Expand Up @@ -55,7 +57,10 @@ public static NetworkClipboardFilesHelper unmarshall(ObjectInputStream in, Netwo

if (getRemainingTotalFilesSize(helper, read, position) == 0) {
String rootPath = format("%s%s%s", tmpDir, File.separator, helper.getTransferId());
helper.setFiles(Arrays.asList(Objects.requireNonNull(new File(rootPath).listFiles())));
File[] filesArray = new File(rootPath).listFiles();
if (filesArray != null) {
helper.setFiles(Arrays.asList(Objects.requireNonNull(filesArray)));
}
}
} catch (ClassNotFoundException e) {
Log.error(e.getMessage());
Expand All @@ -76,7 +81,13 @@ private static long getRemainingTotalFilesSize(NetworkClipboardFilesHelper helpe
}

private static void writeToTempFile(byte[] buffer, int length, String tempFileName, boolean append) throws IOException {
new File(tempFileName.substring(0, tempFileName.lastIndexOf(File.separatorChar))).mkdirs();
final Path parent = Paths.get(tempFileName).getParent();
if (parent != null) {
final boolean created = parent.toFile().mkdirs();
if (!created) {
Log.error("Could not create parent directories for " + tempFileName);
}
}
try (FileOutputStream stream = new FileOutputStream(tempFileName, append)) {
stream.write(copyOf(buffer, length));
}
Expand All @@ -92,7 +103,10 @@ private void extractFileMetaData(File node, List<FileMetaData> fileMetaDatas, St
if (node.isFile()) {
fileMetaDatas.add(new FileMetaData(node.getPath(), node.length(), basePath));
} else if (node.isDirectory()) {
Arrays.stream(Objects.requireNonNull(node.listFiles())).parallel().forEachOrdered(file -> extractFileMetaData(file, fileMetaDatas, basePath));
File[] filesArray = node.listFiles();
if (filesArray != null) {
Arrays.stream(filesArray).parallel().forEachOrdered(file -> extractFileMetaData(file, fileMetaDatas, basePath));
}
}
}

Expand Down Expand Up @@ -123,8 +137,11 @@ private void processFile(File file, ObjectOutputStream out) throws IOException {
if (file.isFile()) {
sendFile(file, out);
} else {
for (File node : Objects.requireNonNull(file.listFiles())) {
processFile(node, out);
File[] filesArray = file.listFiles();
if (filesArray != null) {
for (File node : filesArray) {
processFile(node, out);
}
}
}
}
Expand Down

0 comments on commit cdd9109

Please sign in to comment.