public int decode ( final InputStream stream , final ByteBuffer buffer ) throws IOException { while ( buffer . remaining ( ) >= 64 ) { int val = stream . read ( ) ; if ( val < 0 ) { break ; // EOF } if ( ( val & COMPRESSED_RUN_MASK ) == COMPRESSED_RUN_MASK ) { int count = val & ~ COMPRESSED_RUN_MASK ; int pixel = stream . read ( ) ; if ( pixel < 0 ) { break ; // EOF } for ( int i = 0 ; i < count ; i ++ ) { buffer . put ( ( byte ) pixel ) ; } } else { buffer . put ( ( byte ) val ) ; } } return buffer . position ( ) ; }
even if this will make the output larger .