public static void streamResponseAndClose ( Writer pWriter , JSONStreamAware pJson , String callback ) throws IOException { try { if ( callback == null ) { pJson . writeJSONString ( pWriter ) ; } else { pWriter . write ( callback ) ; pWriter . write ( "(" ) ; pJson . writeJSONString ( pWriter ) ; pWriter . write ( ");" ) ; } // Writer end marker for chunked responses pWriter . write ( STREAM_END_MARKER ) ; } finally { // Flush and close, even on an exception to avoid locks in the thread pWriter . flush ( ) ; pWriter . close ( ) ; } }
Stream a JSON stream to a given writer potentiall wrap it in a callback for a JSONP response and then flush & close the writer . The writer is closed in any case also when an exception occurs