Skip to content

Commit

Permalink
Fix castexception for json audit report download by outputting stream…
Browse files Browse the repository at this point in the history
… to array before writing
  • Loading branch information
charliecarlton committed Aug 8, 2024
1 parent 97b9ed2 commit a108a3d
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,21 @@ public static void jsonOut(final String query, final OutputStream os) {
// interleave an object separator (the comma and line break) into the stream
// of json objects to create valid json thx!
// https://stackoverflow.com/a/25624818
// call .skip() to remove the first separator
final Stream<Object[]> results =
q.stream().flatMap(i -> Stream.of(new String[] {",\n"}, i)).skip(1); // remove
// the
// first
// separator
q.stream().flatMap(i -> Stream.of(",\n", i)).skip(1);

// write json by hand to preserve streaming writes in case of big data
try {
os.write("[".getBytes(StandardCharsets.UTF_8));
results.forEach(line -> {
Object[] resultsArr = results.toArray();

for(Object line : resultsArr) {
try {
// the object array is the columns, but in this case there is only
// one, so we take it at index 0
os.write(line[0].toString().getBytes(StandardCharsets.UTF_8));
os.write(line.toString().getBytes(StandardCharsets.UTF_8));
} catch (java.io.IOException e) {
LOGGER.error(e.getMessage());
}
});
}

os.write("]".getBytes(StandardCharsets.UTF_8));
} catch (java.io.IOException e) {
Expand Down

0 comments on commit a108a3d

Please sign in to comment.