private static void copy ( SeekableInputStream from , PositionOutputStream to , long start , long length ) throws IOException { LOG . debug ( "Copying {} bytes at {} to {}" , length , start , to . getPos ( ) ) ; from . seek ( start ) ; long bytesCopied = 0 ; byte [ ] buffer = COPY_BUFFER . get ( ) ; while ( bytesCopied < length ) { long bytesLeft = length - bytesCopied ; int bytesRead = from . read ( buffer , 0 , ( buffer . length < bytesLeft ? buffer . length : ( int ) bytesLeft ) ) ; if ( bytesRead < 0 ) { throw new IllegalArgumentException ( "Unexpected end of input file at " + start + bytesCopied ) ; } to . write ( buffer , 0 , bytesRead ) ; bytesCopied += bytesRead ; } }
Copy from a FS input stream to an output stream . Thread - safe