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

📄 torrentdownloaderimpl.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      this.error(0, "I/O Exception while initializing download of '" + url + "':" + ioe.toString());
    } catch( Throwable e ){
        this.error(0, "Exception while initializing download of '" + url + "':" + e.toString());   	
    }
    
    if ( this.state == STATE_ERROR ){
    	
    	return;
    }
    
    try{
		final boolean	status_reader_run[] = { true };
    
    	this.state = STATE_START;
      
    	notifyListener();
      
    	this.state = STATE_DOWNLOADING;
      
    	notifyListener();
  
        Thread	status_reader = 
        	new AEThread( "TorrentDownloader:statusreader" )
			{
        		public void
				runSupport()
        		{
        			boolean changed_status	= false;
        			
        			while( true ){
        				
        				try{
        					Thread.sleep(250);
        					
        					try{
        						this_mon.enter();
        						
        						if ( !status_reader_run[0] ){
        						
        							break;
        						}
        					}finally{
        						
        						this_mon.exit();
        					}
        					
        					String	s = con.getResponseMessage();
        					        					
        					if ( !s.equals( getStatus())){
        						
        						if ( !s.toLowerCase().startsWith("error:")){
        							
        							setStatus(s);
        							
        						}else{
        							
        							error(con.getResponseCode(), s.substring(6));
        						}
        						
        						changed_status	= true;
        					}
        				}catch( Throwable e ){
        					
        					break;
        				}
        			}
        			
        			if ( changed_status ){
        				
        				setStatus( "" );
        			}
        		}
			};
			
		status_reader.setDaemon( true );
		
		status_reader.start();
  
		InputStream in;
			
		try{
			in = this.con.getInputStream();
				
		}finally{
			
			try{ 
				this_mon.enter();
					
				status_reader_run[0]	= false;
				
			}finally{
					
				this_mon.exit();
			}
		}
			
	    if ( this.state != STATE_ERROR ){
		    	
	    	this.file = new File(this.directoryname, this.filename);
	        
	    	this.file.createNewFile();
	        
	        FileOutputStream fileout = new FileOutputStream(this.file, false);
	        
	        bufBytes = 0;
	        
	        int size = this.con.getContentLength();
	        
			this.percentDone = -1;
			
	        do {
	          if (this.cancel){
	            break;
	          }
	          
	          try {
	          	bufBytes = in.read(buf);
	            
	            this.readTotal += bufBytes;
	            
	            if (size != 0){
	              this.percentDone = (100 * this.readTotal) / size;
	            }
	            
	            notifyListener();
	            
	          } catch (IOException e) {
	          }
	          
	          if (bufBytes > 0){
	            fileout.write(buf, 0, bufBytes);
	          }
	        } while (bufBytes > 0);
	        
	        in.close();
	        
	        fileout.flush();
	        
	        fileout.close();
	        
	        if (this.cancel) {
	          this.state = STATE_CANCELLED;
	          if (deleteFileOnCancel) {
	          	this.cleanUpFile();
	          }
	        } else {
	          if (this.readTotal <= 0) {
	            this.error(0, "No data contained in '" + this.url.toString() + "'");
	            return;
	          }
	          
	          	// if the file has come down with a not-so-useful name then we try to rename
	          	// it to something more useful
	          
	          try{
	          	if ( !filename.toLowerCase().endsWith(".torrent" )){
	
	          		TOTorrent	torrent = TorrentUtils.readFromFile( file, false );
	          		
	          		String	name = TorrentUtils.getLocalisedName( torrent ) + ".torrent";
	          		
	          		File	new_file	= new File( directoryname, name );
	          		
	          		if ( file.renameTo( new_file )){
	          			
	          			filename	= name;
					
	          			file	= new_file;
	          		}
	          	}
	          }catch( Throwable e ){
	          		
	          	Debug.printStackTrace( e );
	          }
	          
	          this.state = STATE_FINISHED;
	        }
	        this.notifyListener();
	      }
      } catch (Exception e) {
    	  
    	if ( !cancel ){
    		
    		Debug.out("'" + this.directoryname + "' '" +  this.filename + "'", e);
    	}
      	
        this.error(0, "Exception while downloading '" + this.url.toString() + "':" + e.getMessage());
      }
  }

  public boolean 
  equals(Object obj) 
  {
    if (this == obj){
    	
      return true;
    }
    
    if ( obj instanceof TorrentDownloaderImpl ){
    	
      TorrentDownloaderImpl other = (TorrentDownloaderImpl) obj;
      
      if (other.getURL().equals(this.url.toString())){
    	  
    	  File	other_file 	= other.getFile();
    	  File	this_file	= file;
    	  
    	  if ( other_file == this_file ){
    		  
    		  return( true );
    	  }
    	  
    	  if ( other_file == null || this_file == null ){
    		  
    		  return( false );
    	  }
    	  
    	  return( other_file.getAbsolutePath().equals(this_file.getAbsolutePath()));
    	  
      	}else{
      
      		return false;
      	}
    }else{
    	return false;
    }
  }

  
  public int hashCode() {  return this.url.hashCode();  }
  
  
  
  public String getError() {
    return this.error;
  }

  public void setError(int errCode, String err) {
    this.error = err;
    this.errCode = errCode;
  }
  
  public int getErrorCode() {
  	return errCode;
  }

  protected void
  setStatus(
  	String	str )
  {
  	status	= str;
  	notifyListener();
  }
  
  public String
  getStatus()
  {
  	return( status );
  }
  
  public java.io.File getFile() {
    if ((!this.isAlive()) || (this.file == null))
      this.file = new File(this.directoryname, this.filename);
    return this.file;
  }

  public int getPercentDone() {
    return this.percentDone;
  }

  public int getDownloadState() {
    return this.state;
  }

  public void setDownloadState(int state) {
    this.state = state;
  }

  public String getURL() {
    return this.url.toString();
  }

  public void cancel() {
    this.cancel = true;
    if ( con instanceof MagnetConnection ){
    	con.disconnect();
    }
  }

  public void setDownloadPath(String path, String file) {
    if (!this.isAlive()) {
      if (path != null)
        this.directoryname = path;
      if (file != null)
        this.filename = file;
    }
  }

  /* (non-Javadoc)
   * @see org.gudy.azureus2.core3.torrentdownloader.TorrentDownloader#getTotalRead()
   */
  public int getTotalRead() {
    return this.readTotal;
  }

  public byte[] getLastReadBytes() {
  	if (bufBytes <= 0) {
  		return new byte[0];
  	}
  	byte[] bytes = new byte[bufBytes];
  	System.arraycopy(buf, 0, bytes, 0, bufBytes);
  	return bytes;
  }

  public int getLastReadCount() {
  	return bufBytes;
  }
  
  public void setDeleteFileOnCancel(boolean deleteFileOnCancel) {
  	this.deleteFileOnCancel = deleteFileOnCancel;
  }
  
  public boolean getDeleteFileOnCancel() {
  	return deleteFileOnCancel;
  }
}

⌨️ 快捷键说明

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