Code example for PrintWriter

Methods: close, flush, write

0
 
    private static final transient Logger LOG = LoggerFactory.getLogger(ServletHelpers.class); 
 
 
    static void writeEmpty(PrintWriter out) {
        out.write("{}");
        out.flush();
        out.close();
    } 
 
    static void writeObject(Converters converters, JsonConvertOptions options, PrintWriter out, Object answer) {
        Object result = null;
 
        try { 
            result = converters.getToJsonConverter().convertToJson(answer, null, options);
        } catch (AttributeNotFoundException e) {
            LOG.warn("Failed to convert object to json", e);
        } 
 
        if (result != null) {
            out.write(result.toString());
            out.flush();
            out.close();
        } else { 
            writeEmpty(out);
        } 
    } 
}