⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diskmanagerimpl.java

📁 一个基于JAVA的多torrent下载程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

                try{
                    FileUtil.deleteWithRecycle( file );

                }catch (Exception e){

                    Debug.out(e.toString());
                }
            }
        }

        FileUtil.recursiveEmptyDirDelete(new File( torrent_save_dir, torrent_save_file ));
    }

    public void
    skippedFileSetChanged(
    	DiskManagerFileInfo	file )
    {
    	skipped_file_set_changed	= true;
	    listeners.dispatch(LDT_PRIOCHANGED, file);
    }

	public void 
	priorityChanged(
		DiskManagerFileInfo	file ) 
	{
	    listeners.dispatch(LDT_PRIOCHANGED, file);
    }
  
  private void 
  loadFilePriorities() 
  {
	  loadFilePriorities( download_manager, files );
  }
  
  private static void 
  loadFilePriorities(
	DownloadManager			download_manager,
	DiskManagerFileInfo[]	files )
  {
  	//  TODO: remove this try/catch.  should only be needed for those upgrading from previous snapshot
    try {
    	if ( files == null ) return;
    	List file_priorities = (List)download_manager.getData( "file_priorities" );
    	if ( file_priorities == null ) return;
    	for (int i=0; i < files.length; i++) {
    		DiskManagerFileInfo file = files[i];
    		if (file == null) return;
    		int priority = ((Long)file_priorities.get( i )).intValue();
    		if ( priority == 0 ) file.setSkipped( true );
    		else if (priority == 1) file.setPriority( true );
    	}
    }
    catch (Throwable t) {Debug.printStackTrace( t );}
  }
  
  protected void 
  storeFilePriorities() 
  {
	  storeFilePriorities( download_manager, files );
  }
  
  protected static void 
  storeFilePriorities(
	DownloadManager			download_manager,
	DiskManagerFileInfo[]	files )
  {
    if ( files == null ) return;
    List file_priorities = new ArrayList();
    for (int i=0; i < files.length; i++) {
      DiskManagerFileInfo file = files[i];
      if (file == null) return;
      boolean skipped = file.isSkipped();
      boolean priority = file.isPriority();
      int value = -1;
      if ( skipped ) value = 0;
      else if ( priority ) value = 1;
      file_priorities.add( i, new Long(value));            
    }
    download_manager.setData( "file_priorities", file_priorities );
  }
  
  protected static void
  storeFileDownloaded(
	DownloadManager			download_manager,
	DiskManagerFileInfo[]	files )
  {
	  DownloadManagerState	state = download_manager.getDownloadState();
	  
	  Map	details = new HashMap();
	  
	  List	downloaded = new ArrayList();
	  
	  details.put( "downloaded", downloaded );
	  
	  for (int i=0;i<files.length;i++){
		  
		  downloaded.add( new Long( files[i].getDownloaded()));
	  }
	  
	  state.setMapAttribute( DownloadManagerState.AT_FILE_DOWNLOADED, details );
	  
	  state.save();
  }
  
  protected static void
  loadFileDownloaded(
	DownloadManager				download_manager,
	DiskManagerFileInfoHelper[]	files )
  {
	  DownloadManagerState	state = download_manager.getDownloadState();

	  Map	details = state.getMapAttribute( DownloadManagerState.AT_FILE_DOWNLOADED );
	  
	  if ( details == null ){
		  
		  return;
	  }  
		
	  List	downloaded = (List)details.get( "downloaded" );
	  
	  if ( downloaded == null ){
		  
		  return;
	  }
		
	  try{
		  for (int i=0;i<files.length;i++){
	
			  files[i].setDownloaded(((Long)downloaded.get(i)).longValue());
		  }
	 }catch( Throwable e ){
		 
		 Debug.printStackTrace(e);
	 }
	
  }
  
  public void
  saveState()
  {
	  if ( files != null ){
		
		storeFileDownloaded( download_manager, files );
		
		storeFilePriorities();
	}
  }
  
  public DownloadManager getDownloadManager() {
    return download_manager;
  }
   
	public String
	getInternalName()
	{
		return( download_manager.getInternalName());
	}
	
	public DownloadManagerState
	getDownloadState()
	{
		return( download_manager.getDownloadState());
	}

	public File
	getSaveLocation()
	{
		return( download_manager.getSaveLocation());
	}
	
	public String[]
	getStorageTypes()                 
	{
		return( getStorageTypes( download_manager ));
	}
	
	protected static String[]
	getStorageTypes(
		DownloadManager		download_manager )
	{
		DownloadManagerState	state = download_manager.getDownloadState();

		String[]	types = state.getListAttribute( DownloadManagerState.AT_FILE_STORE_TYPES );
		
		if ( types.length == 0 ){
			
			types = new String[download_manager.getTorrent().getFiles().length];
			
			for (int i=0;i<types.length;i++){
				
				types[i] = "L";
			}	
		}
		
		return( types );
	}
	
	protected static boolean
	setFileLink(
		DownloadManager			download_manager,
		DiskManagerFileInfo[]	info,
		DiskManagerFileInfo		file_info,
		File					from_link,
		File					to_link )
	{
		File	existing_link = FMFileManagerFactory.getSingleton().getFileLink( download_manager.getTorrent(), to_link );
		
		if ( !existing_link.equals( to_link )){
			
				// where we're mapping to is already linked somewhere else. Only case we support
				// is where this is a remapping of the same file back to where it came from
			
			if ( !from_link.equals( to_link )){
				
				Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
								"Attempt to link to existing link '" + existing_link.toString()
										+ "'"));
				
				return( false );
			}
		}
		
		for (int i=0;i<info.length;i++){
			
			if ( to_link.equals( info[i].getFile( true ))){
				
				Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
								"Attempt to link to existing file '" + info[i].getFile(true)
										+ "'"));
				
				return( false );
			}
		}

		File	existing_file = file_info.getFile( true );
		
		if ( to_link.exists()){
   		 
			if ( to_link.equals( existing_file )){
		  
				return( true );
				
			}else if ( !existing_file.exists()){

					// using a new file, make sure we recheck
				
				download_manager.recheckFile( file_info );
				
			}else{
				
				if ( FileUtil.deleteWithRecycle( existing_file )){
		    		
						// new file, recheck
					
					download_manager.recheckFile( file_info );
					
				}else{
	    	
					Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
							"Failed to delete '" + existing_file.toString() + "'"));
					
					return( false );
				}
			}
		}else{
			
			if ( existing_file.exists()){
  			  
				if ( !FileUtil.renameFile( existing_file, to_link )){
		  			  
					Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR, 
		    			"Failed to rename '" + existing_file.toString() + "'" ));

					return( false );
				}
			}
		}

		DownloadManagerState	state = download_manager.getDownloadState();
		
		state.setFileLink( from_link, to_link );
		
		state.save();
		
		return( true );
	}
	
	public static DiskManagerFileInfo[]
	getFileInfoSkeleton(
		final DownloadManager		download_manager )
	{
		TOTorrent	torrent = download_manager.getTorrent();
		
		if ( torrent == null ){
			
			return( new DiskManagerFileInfo[0]);
		}
		
		String	root_dir = download_manager.getAbsoluteSaveLocation().getParent();
		
		if ( !torrent.isSimpleTorrent()){
			
			root_dir += File.separator + download_manager.getAbsoluteSaveLocation().getName();
		}
		
		root_dir	+= File.separator;	

		try{
		    LocaleUtilDecoder locale_decoder = LocaleUtil.getSingleton().getTorrentEncoding( torrent );
			
		    TOTorrentFile[]	torrent_files = torrent.getFiles();
			
		    long	piece_size 	= torrent.getPieceLength();
		    long	size_so_far	= 0;
		    
			final DiskManagerFileInfoHelper[]	res = new DiskManagerFileInfoHelper[ torrent_files.length ];
			
			for (int i=0;i<res.length;i++){
			
				final TOTorrentFile	torrent_file	= torrent_files[i];
				
				final int file_index = i;
				
				long	file_length = torrent_file.getLength();
				
				final int	first_piece = (int)(size_so_far/piece_size);
				
				size_so_far += file_length;
				
				final int	last_piece	= (int)((size_so_far-1)/piece_size);
								
				String	path_str = root_dir + File.separator;
				
					// for a simple torrent the target file can be changed 
				
				if ( torrent.isSimpleTorrent()){
					
					path_str = path_str + download_manager.getAbsoluteSaveLocation().getName();
					
				}else{
			        byte[][]path_comps = torrent_file.getPathComponents();
		
			        for (int j=0;j<path_comps.length;j++){
		
						String comp = locale_decoder.decodeString( path_comps[j] );
		
			            comp = FileUtil.convertOSSpecificChars( comp );
		
			            path_str += (j==0?"":File.separator) + comp;
			        }
				}
				
				final File		data_file	= new File( path_str );
	
				final String	data_name 	= data_file.getName();
							
				int separator = data_name.lastIndexOf(".");
				
				if (separator == -1){
					
					separator = 0;
				}
				
				final String	data_extension	= data_name.substring(separator);
				
				DiskManagerFileInfoHelper	info = 
					new DiskManagerFileInfoHelper()
					{		
						private boolean	priority;
						private boolean	skipped;
						private long	downloaded;
						
						public void 
						setPriority(boolean b)
						{
							priority	= b;
							
							storeFilePriorities( download_manager, res );
						}
				
						public void 
						setSkipped(boolean b)
						{
							skipped	= b;
							
							storeFilePriorities( download_manager, res );
						}
				 		 	
						public int 
						getAccessMode()
						{
							return( READ );
						}
						
						public long 
						getDownloaded()
						{
							return( downloaded );
						}
						
						public void
						setDownloaded(
							long	l )
						{
							downloaded	= l;
						}
						
						public String 
						getExtension()
						{
							return( data_extension );
						}
							
						public int 
						getFirstPieceNumber()
						{
							return( first_piece );
						}
					  
						public int 
						getLastPieceNumber()
						{
							return( last_piece );
						}
						
						public long 
						getLength()
						{
							return( torrent_file.getLength());
						}
							
						public int	
						getIndex()
						{
							return( file_index );
						}
						
						public int 
						getNbPieces()
						{
							return( last_piece - first_piece + 1 );
						}
													
						public boolean 
						isPriority()
						{
							return( priority );
						}
						
						public boolean 
						isSkipped()
						{
							return( skipped );
						}
						
						public DiskManager 
						getDiskManager()
						{
							return( null );
						}
			
						public DownloadManager 
						getDownloadManager()
						{
							return( download_manager );
						}
	
						public File 
						getFile(
							boolean	follow_link )
						{
							if ( follow_link ){
								
								File link = getLink();
								
								if ( link != null ){
									
									return( link );
								}
							}
							return( data_file );
						}
						
						public TOTorrentFile
						getTorrentFile()
						{
							return( torrent_file );
						}
						
						public boolean
						setLink(
							File	link_destination )
						{
							return( setFileLink( download_manager, res, this, data_file, link_destination ));
						}
												
						public File
						getLink()
						{
							return( download_manager.getDownloadState().getFileLink( data_file ));
						}
						
						public boolean
						setStorageType(
							int		type )
						{
							String[]	types = getStorageTypes( download_manager );

							int	old_type = types[file_index].equals( "L")?ST_LINEAR:ST_COMPACT;
							
							if ( type == old_type ){
								
								return( true );
							}
							
							

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -