Buggy code:
public class Handler_query_csv extends MethodHandler { private static final Logger log = LogManager . getLogger ( ) ; public Handler_query_csv ( RemoteTsDB tsdb ) { } @ Override public void handle ( String target , Request baseRequest , HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException { baseRequest . setHandled ( true ) ; response . setContentType ( "text//plain;charset=utf-8" ) ; String plot = request . getParameter ( "plot" ) ; if ( plot == null ) { Handler_query_csv . log . warn ( "wrong<seq2seq4repair_space>call<seq2seq4repair_space>no<seq2seq4repair_space>plot" ) ; response . setStatus ( SC_BAD_REQUEST ) ; return ; } String [ ] sensorNames = request . getParameterValues ( "sensor" ) ; if ( sensorNames == null ) { Handler_query_csv . log . warn ( "wrong<seq2seq4repair_space>call<seq2seq4repair_space>no<seq2seq4repair_space>sensor" ) ; response . getWriter ( ) . println ( "wrong<seq2seq4repair_space>call<seq2seq4repair_space>no<seq2seq4repair_space>sensor" ) ; response . setStatus ( SC_BAD_REQUEST ) ; return ; } String aggregation = request . getParameter ( "aggregation" ) ; AggregationInterval agg = AggregationInterval . HOUR ; if ( aggregation != null ) { try { agg = AggregationInterval . parse ( aggregation ) ; if ( agg == null ) { agg = AggregationInterval . HOUR ; } } catch ( Exception e ) { Handler_query_csv . log . warn ( e ) ; } } String quality = request . getParameter ( "quality" ) ; DataQuality dataQuality = DataQuality . STEP ; if ( quality != null ) { try { dataQuality = DataQuality . parse ( quality ) ; if ( dataQuality == null ) { dataQuality = DataQuality . STEP ; } } catch ( Exception e ) { Handler_query_csv . log . warn ( e ) ; } } String interpolated = request . getParameter ( "interpolated" ) ; boolean isInterpolated = false ; if ( interpolated != null ) { switch ( interpolated ) { case "true" : isInterpolated = true ; break ; case "false" : isInterpolated = false ; break ; default : Handler_query_csv . log . warn ( "unknown<seq2seq4repair_space>input" ) ; isInterpolated = false ; } } String timeYear = request . getParameter ( "year" ) ; Long startTime = null ; Long endTime = null ; if ( timeYear != null ) { try { int year = Integer . parseInt ( timeYear ) ; if ( ( year < 2008 ) || ( year > 2015 ) ) { Handler_query_csv . log . error ( ( "year<seq2seq4repair_space>out<seq2seq4repair_space>of<seq2seq4repair_space>range<seq2seq4repair_space>" + year ) ) ; response . setStatus ( SC_BAD_REQUEST ) ; return ; } String timeMonth = request . getParameter ( "month" ) ; if ( timeMonth == null ) { startTime = TimeConverter . DateTimeToOleMinutes ( LocalDateTime . of ( year , 1 , 1 , 0 , 0 ) ) ; endTime = TimeConverter . DateTimeToOleMinutes ( LocalDateTime . of ( year , 12 , 31 , 23 , 0 ) ) ; } else { try { int month = Integer . parseInt ( timeMonth ) ; if ( ( month < 1 ) || ( month > 12 ) ) { Handler_query_csv . log . error ( ( "month<seq2seq4repair_space>out<seq2seq4repair_space>of<seq2seq4repair_space>range<seq2seq4repair_space>" + month ) ) ; response . setStatus ( SC_BAD_REQUEST ) ; return ; } LocalDateTime dateMonth = LocalDateTime . of ( year , month , 1 , 0 , 0 ) ; startTime = TimeConverter . DateTimeToOleMinutes ( dateMonth ) ; endTime = TimeConverter . DateTimeToOleMinutes ( LocalDateTime . of ( year , month , dateMonth . toLocalDate ( ) . lengthOfMonth ( ) , 23 , 0 ) ) ; } catch ( Exception e ) { Handler_query_csv . log . error ( e ) ; response . setStatus ( SC_BAD_REQUEST ) ; return ; } } } catch ( Exception e ) { Handler_query_csv . log . error ( e ) ; response . setStatus ( SC_BAD_REQUEST ) ; return ; } } try { if ( ( Util . containsString ( sensorNames , "WD" ) ) && ( ! ( Util . containsString ( sensorNames , "WV" ) ) ) ) { sensorNames = Util . concat ( sensorNames , "WV" ) ; } String [ ] validSchema = tsdb . getValidSchema ( plot , sensorNames ) ; if ( ( sensorNames . length ) != ( validSchema . length ) ) { String error = ( ( "some<seq2seq4repair_space>sensors<seq2seq4repair_space>not<seq2seq4repair_space>in<seq2seq4repair_space>plot:<seq2seq4repair_space>" + plot ) + "<seq2seq4repair_space>" ) + ( Arrays . toString ( sensorNames ) ) ; Handler_query_csv . log . info ( error ) ; response . getWriter ( ) . println ( error ) ; response . setStatus ( SC_BAD_REQUEST ) ; return ; } TimestampSeries ts = tsdb . plot ( null , plot , sensorNames , agg , dataQuality , isInterpolated , startTime , endTime ) ; if ( ts == null ) { String error = "TimestampSeries<seq2seq4repair_space>null:<seq2seq4repair_space>" + plot ; Handler_query_csv . log . info ( error ) ; response . getWriter ( ) . println ( error ) ; response . setStatus ( SC_INTERNAL_SERVER_ERROR ) ; return ; } ServletOutputStream out = response . getOutputStream ( ) ; <START_BUG> CSV . write ( ts . tsIterator ( ) , true , out , "," , "Na" , DATETIME , false , false ) ; <END_BUG> response . setStatus ( SC_OK ) ; } catch ( Exception e ) { Handler_query_csv . log . error ( e ) ; response . getWriter ( ) . println ( e ) ; response . setStatus ( SC_INTERNAL_SERVER_ERROR ) ; } } }

Fix:
CSV . write ( ts . tsIterator ( ) , true , out , "," , "NA" , DATETIME , false , false ) ;