Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 674d9c4

Browse files
committed
Improved the writeAndFlush method.
1 parent b75f968 commit 674d9c4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/main/java/org/tinystruct/http/servlet/ResponseBuilder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,16 @@ public void sendRedirect(String url) throws IOException {
108108
*/
109109
@Override
110110
public void writeAndFlush(byte[] bytes) throws ApplicationException {
111+
ServletOutputStream out = this.get();
112+
if (out == null) {
113+
logger.log(Level.SEVERE, "ServletOutputStream is null, cannot write response. Possible client disconnect or response already closed.");
114+
throw new ApplicationException("ServletOutputStream is null, cannot write response.");
115+
}
111116
try {
112-
this.get().write(bytes);
113-
this.get().flush();
117+
out.write(bytes);
118+
out.flush();
114119
} catch (IOException e) {
115-
throw new RuntimeException(e);
120+
throw new ApplicationException("Failed to write to ServletOutputStream", e);
116121
}
117122
}
118123
}

0 commit comments

Comments
 (0)