From f4439a5c64897be4fb868d92d0246b90ab3dfeb7 Mon Sep 17 00:00:00 2001 From: mikemirzayanov Date: Wed, 5 Nov 2025 22:59:22 +0300 Subject: [PATCH] Fix UnsafeFileUtil#readFileCropped --- .../commons/io/internal/UnsafeFileUtil.java | 74 ++++++++++--------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/code/src/main/java/com/codeforces/commons/io/internal/UnsafeFileUtil.java b/code/src/main/java/com/codeforces/commons/io/internal/UnsafeFileUtil.java index 5373f6f..95c31ae 100644 --- a/code/src/main/java/com/codeforces/commons/io/internal/UnsafeFileUtil.java +++ b/code/src/main/java/com/codeforces/commons/io/internal/UnsafeFileUtil.java @@ -884,50 +884,54 @@ public static byte[] downloadFileAsByteArray( @Nonnull public static String readFileCropped(File file, int maxLineCount, int maxLineLength) throws IOException { - if (maxLineCount <= 0) { - return ""; - } + try { + if (maxLineCount <= 0) { + return ""; + } - List lines; - boolean moreLinesExist; + List lines; + boolean moreLinesExist; - // 1. Read one line more than the limit (maxLineCount + 1) - // try-with-resources ensures the underlying file handle/buffer is closed immediately. - try (Stream linesStream = Files.lines(file.toPath())) { + // 1. Read one line more than the limit (maxLineCount + 1) + // try-with-resources ensures the underlying file handle/buffer is closed immediately. + try (Stream linesStream = Files.lines(file.toPath())) { - // Collect up to maxLineCount + 1 lines. The .limit() operation short-circuits reading. - lines = linesStream - .limit(maxLineCount + 1) - // Apply line-level cropping/shrinking - .map(line -> StringUtil.shrinkTo(line, maxLineLength)) - .collect(Collectors.toList()); - } + // Collect up to maxLineCount + 1 lines. The .limit() operation short-circuits reading. + lines = linesStream + .limit(maxLineCount + 1) + // Apply line-level cropping/shrinking + .map(line -> StringUtil.shrinkTo(line, maxLineLength)) + .collect(Collectors.toList()); + } - // 2. Check if the (maxLineCount + 1)-th line was present - if (lines.size() > maxLineCount) { - moreLinesExist = true; - // Remove the extra line so it's not included in the output - lines.remove(maxLineCount); - } else { - moreLinesExist = false; - } + // 2. Check if the (maxLineCount + 1)-th line was present + if (lines.size() > maxLineCount) { + moreLinesExist = true; + // Remove the extra line so it's not included in the output + lines.remove(maxLineCount); + } else { + moreLinesExist = false; + } - // 3. Assemble the final result string - String lineSeparator = System.lineSeparator(); + // 3. Assemble the final result string + String lineSeparator = System.lineSeparator(); - // Join the processed lines - String result = String.join(lineSeparator, lines); + // Join the processed lines + String result = String.join(lineSeparator, lines); - // Conditionally append the ellipsis line if the file had more content - if (moreLinesExist) { - // Append line separator only if there are existing lines - if (!result.isEmpty()) { - result += lineSeparator; + // Conditionally append the ellipsis line if the file had more content + if (moreLinesExist) { + // Append line separator only if there are existing lines + if (!result.isEmpty()) { + result += lineSeparator; + } + result += "..."; } - result += "..."; - } - return result; + return result; + } catch (Exception e) { + return ""; + } } @SuppressWarnings({"FinalizeDeclaration", "DeserializableClassInSecureContext"})