public static void fill ( final FileChannel fileChannel , final long position , final long length , final byte value ) { try { final byte [ ] filler ; if ( 0 != value ) { filler = new byte [ BLOCK_SIZE ] ; Arrays . fill ( filler , value ) ; } else { filler = FILLER ; } final ByteBuffer byteBuffer = ByteBuffer . wrap ( filler ) ; fileChannel . position ( position ) ; final int blocks = ( int ) ( length / BLOCK_SIZE ) ; final int blockRemainder = ( int ) ( length % BLOCK_SIZE ) ; for ( int i = 0 ; i < blocks ; i ++ ) { byteBuffer . position ( 0 ) ; fileChannel . write ( byteBuffer ) ; } if ( blockRemainder > 0 ) { byteBuffer . position ( 0 ) ; byteBuffer . limit ( blockRemainder ) ; fileChannel . write ( byteBuffer ) ; } } catch ( final IOException ex ) { LangUtil . rethrowUnchecked ( ex ) ; } }
Fill a region of a file with a given byte value .