Skip to content

Commit c8d1798

Browse files
Merge pull request #99 from Codeforces/dev-mikemirzayanov
Fix UnsafeFileUtil#readFileCropped
2 parents 0433d54 + f4439a5 commit c8d1798

1 file changed

Lines changed: 39 additions & 35 deletions

File tree

code/src/main/java/com/codeforces/commons/io/internal/UnsafeFileUtil.java

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -884,50 +884,54 @@ public static byte[] downloadFileAsByteArray(
884884

885885
@Nonnull
886886
public static String readFileCropped(File file, int maxLineCount, int maxLineLength) throws IOException {
887-
if (maxLineCount <= 0) {
888-
return "";
889-
}
887+
try {
888+
if (maxLineCount <= 0) {
889+
return "";
890+
}
890891

891-
List<String> lines;
892-
boolean moreLinesExist;
892+
List<String> lines;
893+
boolean moreLinesExist;
893894

894-
// 1. Read one line more than the limit (maxLineCount + 1)
895-
// try-with-resources ensures the underlying file handle/buffer is closed immediately.
896-
try (Stream<String> linesStream = Files.lines(file.toPath())) {
895+
// 1. Read one line more than the limit (maxLineCount + 1)
896+
// try-with-resources ensures the underlying file handle/buffer is closed immediately.
897+
try (Stream<String> linesStream = Files.lines(file.toPath())) {
897898

898-
// Collect up to maxLineCount + 1 lines. The .limit() operation short-circuits reading.
899-
lines = linesStream
900-
.limit(maxLineCount + 1)
901-
// Apply line-level cropping/shrinking
902-
.map(line -> StringUtil.shrinkTo(line, maxLineLength))
903-
.collect(Collectors.toList());
904-
}
899+
// Collect up to maxLineCount + 1 lines. The .limit() operation short-circuits reading.
900+
lines = linesStream
901+
.limit(maxLineCount + 1)
902+
// Apply line-level cropping/shrinking
903+
.map(line -> StringUtil.shrinkTo(line, maxLineLength))
904+
.collect(Collectors.toList());
905+
}
905906

906-
// 2. Check if the (maxLineCount + 1)-th line was present
907-
if (lines.size() > maxLineCount) {
908-
moreLinesExist = true;
909-
// Remove the extra line so it's not included in the output
910-
lines.remove(maxLineCount);
911-
} else {
912-
moreLinesExist = false;
913-
}
907+
// 2. Check if the (maxLineCount + 1)-th line was present
908+
if (lines.size() > maxLineCount) {
909+
moreLinesExist = true;
910+
// Remove the extra line so it's not included in the output
911+
lines.remove(maxLineCount);
912+
} else {
913+
moreLinesExist = false;
914+
}
914915

915-
// 3. Assemble the final result string
916-
String lineSeparator = System.lineSeparator();
916+
// 3. Assemble the final result string
917+
String lineSeparator = System.lineSeparator();
917918

918-
// Join the processed lines
919-
String result = String.join(lineSeparator, lines);
919+
// Join the processed lines
920+
String result = String.join(lineSeparator, lines);
920921

921-
// Conditionally append the ellipsis line if the file had more content
922-
if (moreLinesExist) {
923-
// Append line separator only if there are existing lines
924-
if (!result.isEmpty()) {
925-
result += lineSeparator;
922+
// Conditionally append the ellipsis line if the file had more content
923+
if (moreLinesExist) {
924+
// Append line separator only if there are existing lines
925+
if (!result.isEmpty()) {
926+
result += lineSeparator;
927+
}
928+
result += "...";
926929
}
927-
result += "...";
928-
}
929930

930-
return result;
931+
return result;
932+
} catch (Exception e) {
933+
return "<binary file of length " + file.length() + " bytes>";
934+
}
931935
}
932936

933937
@SuppressWarnings({"FinalizeDeclaration", "DeserializableClassInSecureContext"})

0 commit comments

Comments
 (0)