FileDocCategorySizeDatePackage
DiskManagerFileInfoImpl.javaAPI DocAzureus 3.0.3.412726Mon Aug 27 07:45:14 BST 2007org.gudy.azureus2.core3.disk.impl

DiskManagerFileInfoImpl

public class DiskManagerFileInfoImpl extends Object implements DiskManagerFileInfo, CacheFileOwner
author
Olivier

Fields Summary
private File
file
private int
file_index
private CacheFile
cache_file
private String
extension
private long
downloaded
private DiskManagerHelper
diskManager
private org.gudy.azureus2.core3.torrent.TOTorrentFile
torrent_file
private boolean
priority
private boolean
skipped
private com.aelitis.azureus.core.util.CopyOnWriteList
listeners
Constructors Summary
public DiskManagerFileInfoImpl(DiskManagerHelper _disk_manager, File _file, int _file_index, org.gudy.azureus2.core3.torrent.TOTorrentFile _torrent_file, boolean _linear_storage)

  
  
  
		
  					
  						
			
					 
  
  	 
  
    diskManager 	= _disk_manager;
    torrent_file	= _torrent_file;
  	
    file		= _file;
    file_index	= _file_index;
    
  	cache_file = CacheFileManagerFactory.getSingleton().createFile( 
  						this, _file, _linear_storage?CacheFile.CT_LINEAR:CacheFile.CT_COMPACT );
  
  		// if compact storage then the file must be skipped
  	
  	if ( !_linear_storage ){
  		
  		skipped	= true;
  	}
  
Methods Summary
public voidaddListener(DiskManagerFileInfoListener listener)

		if ( listeners == null ){
			
			listeners = new CopyOnWriteList();
		}
		
		synchronized( listeners ){
			
			if ( listeners.getList().contains( listener )){
				
				return;
			}
		}
		
		listeners.add( listener );
		
		new Runnable()
		{
			private long	file_start;
			private long	file_end;

			private long	current_write_start  	= -1;
			private long	current_write_end		= -1;
			private long	current_check_start  	= -1;
			private long	current_check_end		= -1;

			public void
			run()
			{
				TOTorrentFile[]	tfs = torrent_file.getTorrent().getFiles();
				
				long	torrent_offset = 0;
				
				for (int i=0;i<file_index;i++){
					
					torrent_offset += tfs[i].getLength();
				}
				
				file_start 	= torrent_offset;
				file_end	= file_start + torrent_file.getLength();
					
				DiskManagerPiece[]	pieces = diskManager.getPieces();
				
				int	first_piece = getFirstPieceNumber();
				int last_piece	= getLastPieceNumber();
				long	piece_size	= torrent_file.getTorrent().getPieceLength();
							
				for (int i=first_piece;i<=last_piece;i++){
				
					long	piece_offset = piece_size * i;
					
					DiskManagerPiece	piece = pieces[i];
					
					if ( piece.isDone()){
						
						long	bit_start 	= piece_offset;
						long	bit_end		= bit_start + piece.getLength();
						
						bitWritten( bit_start, bit_end, true );
						
					}else{
						
						int	block_offset = 0;
						
						for (int j=0;j<piece.getNbBlocks();j++){
							
							int	block_size = piece.getBlockSize(j);
							
							if ( piece.isWritten(j)){
								
								long	bit_start 	= piece_offset + block_offset;
								long	bit_end		= bit_start + block_size;
								
								bitWritten( bit_start, bit_end, false );
							}
							
							block_offset += block_size;
						}
					}
				}
				
				bitWritten( -1, -1, false );
			}
			
			protected void
			bitWritten(
				long	bit_start,
				long	bit_end,
				boolean	checked )
			{
				if ( current_write_start == -1 ){
					
					current_write_start	= bit_start;
					current_write_end	= bit_end;
					
				}else if ( current_write_end == bit_start ){
					
					current_write_end = bit_end;
					
				}else{
					
					if ( current_write_start < file_start ){
						
						current_write_start  = file_start;
					}
					
					if ( current_write_end > file_end ){
						
						current_write_end	= file_end;
					}
					
					if ( current_write_start < current_write_end ){
						
						try{
							listener.dataWritten( current_write_start-file_start, current_write_end-current_write_start );
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
						}
					}
					
					current_write_start	= bit_start;
					current_write_end	= bit_end;
				}
				
					// checked case
				
				if ( checked && current_check_start == -1 ){
					
					current_check_start	= bit_start;
					current_check_end	= bit_end;
					
				}else if ( checked && current_check_end == bit_start ){
					
					current_check_end = bit_end;
					
				}else{
					
					if ( current_check_start < file_start ){
						
						current_check_start  = file_start;
					}
					
					if ( current_check_end > file_end ){
						
						current_check_end	= file_end;
					}
					
					if ( current_check_start < current_check_end ){
						
						try{
							listener.dataChecked( current_check_start-file_start, current_check_end-current_check_start );
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
						}
					}
					
					if ( checked ){
						current_check_start	= bit_start;
						current_check_end	= bit_end;
					}else{
						current_check_start	= -1;
						current_check_end	= -1;
					}
				}
			}
		}.run();
	
public voidclose()

		// this doesn't need to do anything as overall closure is handled by the disk manager closing
	
public voiddataChecked(long offset, long size)

  		if ( listeners != null ){
  			
  			Iterator	it = listeners.iterator();
  			
  			while( it.hasNext()){
  				
  				try{
  					((DiskManagerFileInfoListener)it.next()).dataChecked( offset, size );
  					
  				}catch( Throwable e ){
  					
  					Debug.printStackTrace(e);
  				}
  			}
  		}
  	
public voiddataWritten(long offset, long size)

  		if ( listeners != null ){
  			
  			Iterator	it = listeners.iterator();
  			
  			while( it.hasNext()){
  				
  				try{
  					((DiskManagerFileInfoListener)it.next()).dataWritten( offset, size );
  					
  				}catch( Throwable e ){
  					
  					Debug.printStackTrace(e);
  				}
  			}
  		}
  	
public voidflushCache()

  	cache_file.flushCache();
  
public booleanforceNoCache()

		return( diskManager.forceNoCache());
	
public intgetAccessMode()

  	int	mode = cache_file.getAccessMode();
  	
	return( mode == CacheFile.CF_READ?DiskManagerFileInfo.READ:DiskManagerFileInfo.WRITE);
  
public CacheFilegetCacheFile()

  	return( cache_file );
  
public java.io.FilegetCacheFileControlFile(java.lang.String name)

		return( diskManager.getDownloadState().getStateFile( name ));
	
public java.lang.StringgetCacheFileOwnerName()

  		return( diskManager.getInternalName());
  	
public org.gudy.azureus2.core3.torrent.TOTorrentFilegetCacheFileTorrentFile()

		return( torrent_file );
	
public DiskManagergetDiskManager()

    return diskManager;
  
public org.gudy.azureus2.core3.download.DownloadManagergetDownloadManager()

	  DownloadManagerState	state = diskManager.getDownloadState();
	  
	  if ( state == null ){
		  return( null );
	  }
	  
	  return( state.getDownloadManager());
  
public longgetDownloaded()

return

	return downloaded;
  
public java.lang.StringgetExtension()

return

	return extension;
  
public java.io.FilegetFile(boolean follow_link)

return

	  if ( follow_link ){
	  
		  File	res = getLink();
	  
		  if ( res != null ){
		
			  return( res );
		  }
	  }
	  
	  return( file );
  	
public intgetFirstPieceNumber()

return

    return torrent_file.getFirstPieceNumber();
  
public intgetIndex()

		return( file_index );
	
public intgetLastPieceNumber()

    return torrent_file.getLastPieceNumber();
  
public longgetLength()

return

	return torrent_file.getLength();
  
public java.io.FilegetLink()

		return( diskManager.getDownloadState().getFileLink( getFile( false )));
	
public intgetNbPieces()

return

	return torrent_file.getNumberOfPieces();
  
public intgetStorageType()

		String[]	types = diskManager.getStorageTypes();
		
		return( types[file_index].equals( "L")?ST_LINEAR:ST_COMPACT );

	
public org.gudy.azureus2.core3.torrent.TOTorrentFilegetTorrentFile()

		return( torrent_file );
	
protected booleanisLinked()

		return( getLink() != null );
	
public booleanisPriority()

return

	return priority;
  
public booleanisSkipped()

return

	return skipped;
  
protected voidmoveFile(java.io.File newFile, boolean link_only)

	  if ( !link_only ){
		  
		  cache_file.moveFile( newFile );
	  }
	  
	  file	= newFile;
  
public DirectByteBufferread(long offset, int length)

		DirectByteBuffer	buffer = 
			DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_DM_READ, length );
		
		try{
			cache_file.read( buffer, offset, CacheFile.CP_READ_CACHE );
			
		}catch( Throwable e ){
			
			buffer.returnToPool();
			
			Debug.printStackTrace(e);
			
			throw( new IOException( e.getMessage()));
		}
		
		return( buffer );	
	
public voidremoveListener(DiskManagerFileInfoListener listener)

	
		listeners.remove( listener );
	
public voidsetAccessMode(int mode)

	int	old_mode =  cache_file.getAccessMode();
	
  	cache_file.setAccessMode( mode==DiskManagerFileInfo.READ?CacheFile.CF_READ:CacheFile.CF_WRITE );
  	
  	if ( old_mode != mode ){
  		
  		diskManager.accessModeChanged( this, old_mode, mode );
  	}
  
public voidsetDownloaded(long l)

param
l

	downloaded = l;
  
public voidsetExtension(java.lang.String string)

param
string

	extension = string;
  
public booleansetLink(java.io.File link_destination)

		Debug.out( "setLink: download must be stopped" );
		
		return( false );
	
public booleansetLinkAtomic(java.io.File link_destination)

		Debug.out( "setLink: download must be stopped" );
		
		return( false );
	
public voidsetPriority(boolean b)

param
b

	priority = b;
	diskManager.priorityChanged( this );
  
public voidsetSkipped(boolean _skipped)

param
skipped

	  // currently a non-skipped file must be linear
	if ( !_skipped && getStorageType() == ST_COMPACT ){
		if ( !setStorageType( ST_LINEAR )){
			return;
		}
	}
	skipped = _skipped;
	diskManager.skippedFileSetChanged( this );
  
public booleansetStorageType(int type)

		
		String[]	types = diskManager.getStorageTypes();

		int	old_type = types[file_index].equals( "L")?ST_LINEAR:ST_COMPACT;
		
		if ( type == old_type ){
			
			return( true );
		}
	
		if ( type == ST_COMPACT ){
			
			Debug.out( "Download must be stopped for linear -> compact conversion" );
			
			return( false );
		}
	
		boolean	set_skipped	= false;	// currently compact files must be skipped
		
		try{
			
			cache_file.setStorageType( type==ST_LINEAR?CacheFile.CT_LINEAR:CacheFile.CT_COMPACT );	
			
			set_skipped	= type == ST_COMPACT && !isSkipped();
			
			return( true );
			
		}catch( Throwable e ){
			
			Debug.printStackTrace(e);
			
			diskManager.setFailed( this, "Failed to change storage type for '" + getFile(true) + "': " + Debug.getNestedExceptionMessage(e));
			
			return( false );
			
		}finally{
			
			types[file_index] = cache_file.getStorageType()==CacheFile.CT_LINEAR?"L":"C";
			
			DownloadManagerState	dm_state = diskManager.getDownloadState();
			
			dm_state.setListAttribute( DownloadManagerState.AT_FILE_STORE_TYPES, types );
			
			dm_state.save();
			
			if ( set_skipped ){
				
				setSkipped( true );
			}
		}