Great job! The logic flow of your code is clear and correct.
However, the line String content = in.readLine()
may not be entirely safe. It assumes the file content (1) contains only one line and (2) ends with a newline, which isn’t always the case.
Consider reading exact n
bytes as specified in the request header instead:
Content-Length: n
Debug re-enactment:
-
Problem: The code was responding correctly, but the tester did not receive anything, as indicated by
[tester::#QV8] Received: "" (no content received)
. Why?! -
Line of Attack: What if we ignore the file processing temporarily, and focus only on ensuring that the tester receive our response first?
-
Brute-Force: I repeatedly inserted the
getOutputStream().write
line at every position of the code. -
Intermediate result:
- When the
write
line was belowString content = in.readLine()
, the error stayed the same . - When the
write
line was aboveString content = in.readLine()
, a new “Error reading file” appeared, indicating that the tester had received our response but complained that we hadn’t saved the file.
- When the
-
Target Acquired: So
String content = in.readLine()
must have been the culprit, causing the tester to start reading our response before we were able to send it. -
Nitty-gritty: What exactly happened when
String content = in.readLine()
was run?- The remaining content to be read was
grape strawberry banana apple raspberry pineapple strawberry banana
, with no newline at the end. - Without encountering a newline,
readLine
would attempt to read beyond the end ofBufferedReader
, instead of stopping just before the end. - TBH, I don’t know exactly what happened here, but that must have caused the tester to start reading our response before we could send it, causing the
[tester::#QV8] Received: "" (no content received)
error.
- The remaining content to be read was