/** * @param newBytes * @throws IOException */ public void writeEntireFile( ByteBuffer newBytes ) throws IOException { if ( newBytes.capacity() != this.bytesPerTile ) { throw new IllegalArgumentException( "byte buffer is to small, required bytes:" + ( this.bytesPerTile ) + ", provided bytes: " + newBytes.capacity() ); } synchronized ( LOCK ) { FileChannel fileChannel = getWriteChannel(); FileLock lock = fileChannel.lock(); fileChannel.position( 0 ); newBytes.rewind(); fileChannel.write( newBytes ); lock.release(); closeWriteStream(); } }
/** * Signals the gridfile reader that it should (not) close the stream after a read. * * @param yesNo */ public void leaveStreamOpen( boolean yesNo ) { // System.out.println( "trying enter yesno: " + Thread.currentThread().getName() ); synchronized ( LOCK ) { this.leaveStreamOpen = yesNo; // System.out.println( "entered yesno: " + Thread.currentThread().getName() ); if ( !this.leaveStreamOpen ) { try { closeWriteStream(); closeReadStream(); } catch ( IOException e ) { LOG.debug( "Could not close stream because: {}", e.getLocalizedMessage(), e ); } } // System.out.println( "leaving yesno: " + Thread.currentThread().getName() ); } }
/** * @param row * @param column * @param tileBuffer * @return true if the writing of the tile was successful. * @throws IOException */ public boolean writeTile( int column, int row, ByteBuffer tileBuffer ) throws IOException { if ( tileBuffer == null || tileBuffer.capacity() != this.bytesPerTile ) { throw new IllegalArgumentException( "Wrong number of bytes." ); } synchronized ( LOCK ) { long position = calcFilePosition( column, row ); FileChannel fileChannel = getWriteChannel(); FileLock lock = fileChannel.lock( position, position + bytesPerTile, false ); fileChannel.position( position ); tileBuffer.rewind(); fileChannel.write( tileBuffer ); lock.release(); closeWriteStream(); } return true; }
fileChannel.write( buffer ); lock.release(); closeWriteStream();
this.closeWriteStream(); this.closeReadStream();