Buggy code:
class HadoopDataSourceCore implements DirectDataSource { static final Log LOG = LogFactory . getLog ( HadoopDataSourceCore . class ) ; private static final String ATTEMPT_AREA = "attempts" ; private static final String STAGING_AREA = "staging" ; private final HadoopDataSourceProfile profile ; HadoopDataSourceCore ( HadoopDataSourceProfile profile ) { } @ Override public < T > List < DirectInputFragment > findInputFragments ( Class < ? extends T > dataType , DataFormat < T > format , String basePath , SearchPattern resourcePattern ) throws IOException , InterruptedException { } private boolean isIn ( FileStatus stat , Path temporary ) { } private List < BlockInfo > toBlocks ( FileStatus stat ) throws IOException { } private List < FileStatus > filesOnly ( List < FileStatus > stats ) { } @ Override public < T > ModelInput < T > openInput ( Class < ? extends T > dataType , DataFormat < T > format , DirectInputFragment fragment , Counter counter ) throws IOException , InterruptedException { } @ Override public < T > ModelOutput < T > openOutput ( OutputAttemptContext context , Class < ? extends T > dataType , DataFormat < T > format , String basePath , String resourcePath , Counter counter ) throws IOException , InterruptedException { FileSystem fs ; Path attempt ; if ( isLocalAttemptOutput ( ) ) { if ( HadoopDataSourceCore . LOG . isDebugEnabled ( ) ) { HadoopDataSourceCore . LOG . debug ( MessageFormat . format ( "Start<seq2seq4repair_space>opening<seq2seq4repair_space>output<seq2seq4repair_space>(id={0},<seq2seq4repair_space>path={1},<seq2seq4repair_space>resource={2},<seq2seq4repair_space>streaming={3})" , profile . getId ( ) , basePath , resourcePath , true ) ) ; } fs = profile . getLocalFileSystem ( ) ; attempt = getLocalAttemptOutput ( context ) ; } else { if ( HadoopDataSourceCore . LOG . isDebugEnabled ( ) ) { HadoopDataSourceCore . LOG . debug ( MessageFormat . format ( "Start<seq2seq4repair_space>opening<seq2seq4repair_space>output<seq2seq4repair_space>(id={0},<seq2seq4repair_space>path={1},<seq2seq4repair_space>resource={2},<seq2seq4repair_space>streaming={3})" , profile . getId ( ) , basePath , resourcePath , false ) ) ; } fs = profile . getFileSystem ( ) ; attempt = getAttemptOutput ( context ) ; } BinaryStreamFormat < T > sformat = validate ( format ) ; Path file = append ( append ( attempt , basePath ) , resourcePath ) ; FSDataOutputStream stream = fs . create ( file ) ; boolean succeed = false ; try { <START_BUG> CountOutputStream cstream = new CountOutputStream ( stream , counter ) ; <END_BUG> if ( HadoopDataSourceCore . LOG . isDebugEnabled ( ) ) { final HadoopDataSourceProfile p = profile ; final Path f = file ; cstream = new CountOutputStream ( stream , counter ) { @ Override public void close ( ) throws IOException { HadoopDataSourceCore . LOG . debug ( MessageFormat . format ( "Start<seq2seq4repair_space>closing<seq2seq4repair_space>output<seq2seq4repair_space>(id={0},<seq2seq4repair_space>file={1})" , p . getId ( ) , f ) ) ; super . close ( ) ; HadoopDataSourceCore . LOG . debug ( MessageFormat . format ( "Finish<seq2seq4repair_space>closing<seq2seq4repair_space>output<seq2seq4repair_space>(id={0},<seq2seq4repair_space>file={1})" , p . getId ( ) , f ) ) ; } } ; } else { cstream = new CountOutputStream ( stream , counter ) ; } ModelOutput < T > output = sformat . createOutput ( dataType , file . toString ( ) , cstream ) ; if ( HadoopDataSourceCore . LOG . isDebugEnabled ( ) ) { HadoopDataSourceCore . LOG . debug ( MessageFormat . format ( "Finish<seq2seq4repair_space>opening<seq2seq4repair_space>output<seq2seq4repair_space>(id={0},<seq2seq4repair_space>path={1},<seq2seq4repair_space>resource={2},<seq2seq4repair_space>file={3})" , profile . getId ( ) , basePath , resourcePath , file ) ) ; } succeed = true ; return output ; } finally { if ( succeed == false ) { try { stream . close ( ) ; } catch ( IOException e ) { HadoopDataSourceCore . LOG . warn ( MessageFormat . format ( "Failed<seq2seq4repair_space>to<seq2seq4repair_space>close<seq2seq4repair_space>output<seq2seq4repair_space>(path={0})" , file ) , e ) ; } } } } boolean isLocalAttemptOutput ( ) { } private < T > BinaryStreamFormat < T > validate ( DataFormat < T > format ) throws IOException { } @ Override public boolean delete ( String basePath , SearchPattern resourcePattern ) throws IOException , InterruptedException { } private Path append ( Path parent , String child ) { } @ Override public void setupAttemptOutput ( OutputAttemptContext context ) throws IOException , InterruptedException { } @ Override public void commitAttemptOutput ( OutputAttemptContext context ) throws IOException , InterruptedException { } @ Override public void cleanupAttemptOutput ( OutputAttemptContext context ) throws IOException , InterruptedException { } @ Override public void setupTransactionOutput ( OutputTransactionContext context ) throws IOException , InterruptedException { } @ Override public void commitTransactionOutput ( OutputTransactionContext context ) throws IOException , InterruptedException { } @ Override public void cleanupTransactionOutput ( OutputTransactionContext context ) throws IOException , InterruptedException { } private Path getTemporaryOutput ( OutputTransactionContext context ) { } Path getStagingOutput ( OutputTransactionContext context ) { } Path getAttemptOutput ( OutputAttemptContext context ) { } Path getLocalAttemptOutput ( OutputAttemptContext context ) throws IOException { } }

Fix:
CountOutputStream cstream ;