FileDocCategorySizeDatePackage
TorrentImpl.javaAPI DocAzureus 3.0.3.410605Tue Mar 06 15:53:00 GMT 2007org.gudy.azureus2.pluginsimpl.local.torrent

TorrentImpl

public class TorrentImpl extends org.gudy.azureus2.core3.logging.LogRelation implements Torrent
author
parg

Fields Summary
private static com.aelitis.azureus.plugins.magnet.MagnetPlugin
magnet_plugin
private org.gudy.azureus2.plugins.PluginInterface
pi
private TOTorrent
torrent
private LocaleUtilDecoder
decoder
private boolean
complete
Constructors Summary
public TorrentImpl(TOTorrent _torrent)

		this( null, _torrent );
	
public TorrentImpl(org.gudy.azureus2.plugins.PluginInterface _pi, TOTorrent _torrent)

		pi		= _pi;
		torrent	= _torrent;
	
Methods Summary
protected java.lang.Stringdecode(byte[] data)

		getDecoder();
		
		if ( data != null ){
			
			if ( decoder != null ){
				
				try{
					return( decoder.decodeString(data));
					
				}catch( Throwable e ){
				}
			}
			
			return( new String(data));
		}
		
		return( "" );
	
public java.lang.ObjectgetAdditionalProperty(java.lang.String name)

		return( torrent.getAdditionalProperty( name ));
	
public java.net.URLgetAnnounceURL()

		return( torrent.getAnnounceURL());
	
public TorrentAnnounceURLListgetAnnounceURLList()

		return( new TorrentAnnounceURLListImpl( this ));
	
public java.lang.StringgetComment()

		return( decode(torrent.getComment()));
	
public java.lang.StringgetCreatedBy()

		return( decode( torrent.getCreatedBy()));
	
public longgetCreationDate()

		return( torrent.getCreationDate());
	
protected voidgetDecoder()

			// We defer the getting of the decoder until it is required as this Torrent may have been 
			// created in order to simply remove additional properties from it before serialising it
			// Indeed, this was happening and unfortunately resulting in 1) the encoding being
			// serialised 2) the user being prompted for an encoding choice 
		
		try{
			decoder = LocaleTorrentUtil.getTorrentEncoding( torrent );
			
		}catch( Throwable e ){
			
		}
	
public java.lang.StringgetEncoding()

		getDecoder();
		
		if ( decoder != null ){
			
			return( decoder.getName());
		}
		
		return( Constants.DEFAULT_ENCODING );
	
public TorrentFile[]getFiles()

		TOTorrentFile[]	files = torrent.getFiles();
		
		TorrentFile[]	res = new TorrentFile[files.length];
		
		for (int i=0;i<res.length;i++){
		
			TOTorrentFile	tf = files[i];
			
			byte[][]	comps = tf.getPathComponents();
			
			String	name = "";
			
			for (int j=0;j<comps.length;j++){
				
				String	comp = decode(comps[j]);
			
				comp = FileUtil.convertOSSpecificChars( comp );
				
				name += (j==0?"":File.separator)+comp;
			}
			
			res[i] = new TorrentFileImpl(name, tf.getLength());
		}
		
		return( res );
	
public byte[]getHash()

		try{
			return( torrent.getHash());
			
		}catch( TOTorrentException e ){
			
			Debug.printStackTrace( e );
			
			return( null );
		}
	
public java.net.URLgetMagnetURI()

		if ( magnet_plugin == null ){
			
			PluginInterface magnet_pi = AzureusCoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass( MagnetPlugin.class );
			
			if ( magnet_pi != null ){
			
				 magnet_plugin = (MagnetPlugin)magnet_pi.getPlugin();
			}
		}
		
		if ( magnet_plugin == null ){
			
			throw( new TorrentException( "MegnetPlugin unavailable" ));
		}
		
		try{
			URL	res = magnet_plugin.getMagnetURL(  torrent.getHash());
			
			return( res );
				
		}catch( TOTorrentException e ){
			
			throw( new TorrentException(e ));
		}
	
public java.util.MapgetMapProperty(java.lang.String name)

		return( TorrentUtils.getPluginMapProperty( torrent, name ));
	
public java.lang.StringgetName()

		String	name = decode( torrent.getName());
		
		name = FileUtil.convertOSSpecificChars( name );

		return( name );
	
public longgetPieceCount()

		return( torrent.getNumberOfPieces());
	
public longgetPieceSize()

		return( torrent.getPieceLength());
	
public byte[][]getPieces()

		try{
			return( torrent.getPieces());
			
		}catch( TOTorrentException e ){
			
			Debug.printStackTrace( e );
			
			return( new byte[0][0] );
		}
  	
public java.lang.StringgetPluginStringProperty(java.lang.String name)

		PluginInterface	p = pi;
		
		if ( p == null ){
		
			p = UtilitiesImpl.getPluginThreadContext();	
		}
		
		if ( p == null ){
			
			name = "<internal>." + name;
			
		}else{
			
			name = p.getPluginID() + "." + name;
		}
		
		return( TorrentUtils.getPluginStringProperty( torrent, name ));
	
public java.lang.Object[]getQueryableInterfaces()

		return new Object[] { torrent };
	
public java.lang.StringgetRelationText()

		return propogatedRelationText(torrent);
	
public longgetSize()

		return( torrent.getSize());
	
public TOTorrentgetTorrent()

		return( torrent );
	
public booleanisComplete()

			// TODO: could check the download state too I guess...
		
		return( complete );
	
public booleanisDecentralised()

		return( TorrentUtils.isDecentralised( torrent ));
	
public booleanisDecentralisedBackupEnabled()

		return( TorrentUtils.getDHTBackupEnabled( torrent ));
	
public booleanisDecentralisedBackupRequested()

		return( TorrentUtils.isDHTBackupRequested( torrent ));

	
public booleanisPrivate()

		return( TorrentUtils.getPrivate( torrent ));
	
public TorrentremoveAdditionalProperties()

		try{
			TOTorrent	t = TOTorrentFactory.deserialiseFromMap(torrent.serialiseToMap());
					
			t.removeAdditionalProperties();
	
			return( new TorrentImpl( t ));
			
		}catch( TOTorrentException e ){
			
			Debug.printStackTrace(e);
			
			return( this );
		}
	
public voidsave()

		try{
			TorrentUtils.writeToFile( torrent );
			
		}catch( TOTorrentException e ){
			
			throw( new TorrentException( "Torrent::save Fails", e ));
		}	
	
public voidsetAnnounceURL(java.net.URL url)

		torrent.setAnnounceURL( url );
		
		updated();
	
public voidsetComment(java.lang.String comment)

		torrent.setComment( comment );
	
public voidsetComplete(java.io.File data_dir)

		
		try{
			LocaleTorrentUtil.setDefaultTorrentEncoding( torrent );
		
			DownloadManagerState	download_manager_state = 
				DownloadManagerStateFactory.getDownloadState( torrent ); 

			TorrentUtils.setResumeDataCompletelyValid( download_manager_state );

			download_manager_state.save();
			
			complete	= true;
			
		}catch( Throwable e ){
			
			throw( new TorrentException("encoding selection fails", e ));
		}
	
public voidsetDecentralisedBackupRequested(boolean requested)

		TorrentUtils.setDHTBackupRequested( torrent, requested );
	
public voidsetDefaultEncoding()

		setEncoding(Constants.DEFAULT_ENCODING);
	
public voidsetEncoding(java.lang.String encoding)

		
		try {
			LocaleTorrentUtil.setTorrentEncoding(torrent, encoding);
		} catch(LocaleUtilEncodingException e) {
			throw new TorrentEncodingException("Failed to set the encoding",e);
		}
	
public voidsetMapProperty(java.lang.String name, java.util.Map value)

		TorrentUtils.setPluginMapProperty( torrent, name, value );
	
public voidsetPluginStringProperty(java.lang.String name, java.lang.String value)

		PluginInterface	p = pi;
		
		if ( p == null ){
		
			p = UtilitiesImpl.getPluginThreadContext();	
		}
		
		if ( p == null ){
			
			name = "<internal>." + name;
			
		}else{
			
			name = p.getPluginID() + "." + name;
		}
		
		TorrentUtils.setPluginStringProperty( torrent, name, value );
	
public voidsetPrivate(boolean priv)

		TorrentUtils.setPrivate( torrent, priv );
	
protected voidupdated()

		try{
			DownloadImpl dm = (DownloadImpl)DownloadManagerImpl.getDownloadStatic( torrent );
		
			if ( dm != null ){
			
				dm.torrentChanged();
			}
		}catch( DownloadException e ){
			
			// torrent may not be running
		}
	
public booleanwasCreatedByUs()

		return( TorrentUtils.isCreatedTorrent( torrent ));
	
public byte[]writeToBEncodedData()

		try{
			Map	map = torrent.serialiseToMap();
			
			return( BEncoder.encode( map ));
			
		}catch( Throwable e ){
			
			throw( new TorrentException( "Torrent::writeToBEncodedData: fails", e ));
		}
	
public voidwriteToFile(java.io.File file)

		try{
				// don't use TorrentUtils.writeToFile as this updates the internal torrent
				// file reference an means that the torrent get's auto-written to the new
				// location in future, most likley NOT the desired behaviour
			
			torrent.serialiseToBEncodedFile( file );
			
		}catch( TOTorrentException e ){
			
			throw( new TorrentException( "Torrent::writeToFile: fails", e ));
		}
	
public java.util.MapwriteToMap()

		try{
			return( torrent.serialiseToMap());
			
		}catch( TOTorrentException e ){
			
			throw( new TorrentException( "Torrent::writeToMap: fails", e ));
		}