response.setContentLength(len);
OutputStream out;
if (Servlets.isIncluded(request)) { //usually getWriter
try {
out = new WriterOutputStream(response.getWriter(), "UTF-8");
//Not response.getCharacterEncoding becauses it is for "data"
} catch (IllegalStateException ex) {
try {
out = response.getOutputStream();
} catch (Throwable t) {
log.warning("getOutputStream: failed" + Exceptions.getMessage(t));
throw ex;
}
}
} else {
try {
out = response.getOutputStream();
} catch (IllegalStateException ex) {
if (compressed) throw ex;
try {
out = new WriterOutputStream(response.getWriter(), "UTF-8");
} catch (Throwable t) {
log.warning("getWriter: failed" + Exceptions.getMessage(t));
throw ex;
}
}