Skip to content

Commit

Permalink
Fix: Check for null message before calling replaceAll to prevent Null…
Browse files Browse the repository at this point in the history
…PointerException. This could happen if a channel has no message.
  • Loading branch information
selectAll authored Jun 19, 2023
1 parent cebcdb0 commit 93bd829
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public void export(List<Message> messages, OutputStream outputStream) {
writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));

for (Message normalizedMessage : messages) {
String message = normalizedMessage.getMessage();
message = message == null ? "" : message.replaceAll("\"", "\\\\\"");
writer.write("\"" + normalizedMessage.getTimestamp().getEpochSecond() + "\"," +
"\"" + normalizedMessage.getChannel() + "\"," +
"\"" + normalizedMessage.getUsername() + "\"," +
"\"" + normalizedMessage.getMessage().replaceAll("\"", "\\\\\"") + "\"" +
"\"" + message + "\"" +
"\n");
}
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ public void export() {
exportFormat.export(
List.of(
new Message("m1", "u1", "c1", EPOCH),
new Message("m2", "u2", "c3", EPOCH.plusSeconds(1))
new Message("m2", "u2", "c3", EPOCH.plusSeconds(1)),
new Message(null, "u3", "c3", EPOCH)
),
bout);

String out = bout.toString();

assertEquals("\"0\",\"c1\",\"u1\",\"m1\"\n" +
"\"1\",\"c3\",\"u2\",\"m2\"\n", out);
"\"1\",\"c3\",\"u2\",\"m2\"\n" +
"\"0\",\"c3\",\"u3\",\"\"\n", out);
}
}

0 comments on commit 93bd829

Please sign in to comment.