FileDocCategorySizeDatePackage
TOTorrentFileImpl.javaAPI DocAzureus 3.0.3.45046Wed Jan 10 23:10:36 GMT 2007org.gudy.azureus2.core3.torrent.impl

TOTorrentFileImpl

public class TOTorrentFileImpl extends Object implements TOTorrentFile

Fields Summary
private final TOTorrent
torrent
private final long
file_length
private final byte[]
path_components
private final int
first_piece_number
private final int
last_piece_number
private final Map
additional_properties
private final boolean
is_utf8
Constructors Summary
protected TOTorrentFileImpl(TOTorrent _torrent, long _torrent_offset, long _len, String _path)


	
	
				
					
					
					 
		
		 
	
		torrent			= _torrent;
		file_length		= _len;

		first_piece_number 	= (int)( _torrent_offset / torrent.getPieceLength());
		last_piece_number	= (int)(( _torrent_offset + file_length - 1 ) /  torrent.getPieceLength());

		is_utf8	= true;
		
		try{
			
			
			Vector	temp = new Vector();
			
			int	pos = 0;
			
			while(true){
				
				int	p1 = _path.indexOf( File.separator, pos );
				
				if ( p1 == -1 ){
					
					temp.add( _path.substring( pos ).getBytes( Constants.DEFAULT_ENCODING ));
					
					break;
				}
				
				temp.add( _path.substring( pos, p1 ).getBytes( Constants.DEFAULT_ENCODING ));
				
				pos = p1+1;
			}
			
			path_components		= new byte[temp.size()][];
			
			temp.copyInto( path_components );
			
			checkComponents();
			
		}catch( UnsupportedEncodingException e ){
	
			throw( new TOTorrentException( 	"Unsupported encoding for '" + _path + "'",
											TOTorrentException.RT_UNSUPPORTED_ENCODING));
		}
	
protected TOTorrentFileImpl(TOTorrent _torrent, long _torrent_offset, long _len, byte[] _path_components)

		torrent				= _torrent;
		file_length			= _len;
		path_components		= _path_components;
		
		first_piece_number 	= (int)( _torrent_offset / torrent.getPieceLength());
		last_piece_number	= (int)(( _torrent_offset + file_length - 1 ) /  torrent.getPieceLength());

		is_utf8				= false;
		
		checkComponents();
	
Methods Summary
protected voidcheckComponents()

		for (int i=0;i<path_components.length;i++){
			
			byte[]	comp = path_components[i];
			
			if (	comp.length == 2 && 
					comp[0] == (byte)'." &&
					comp[1] == (byte)'." ){
				
				throw( 	new TOTorrentException( 
						"Torrent file contains illegal '..' component",
						TOTorrentException.RT_DECODE_FAILS ));
			}
		}
	
protected java.util.MapgetAdditionalProperties()

		return( additional_properties );
	
public intgetFirstPieceNumber()

		return( first_piece_number );
	
public intgetLastPieceNumber()

		return( last_piece_number );
	
public longgetLength()

		return( file_length );
	
public intgetNumberOfPieces()

		return( getLastPieceNumber() - getFirstPieceNumber() + 1 );
	
public byte[][]getPathComponents()

		return( path_components );
	
public java.lang.StringgetRelativePath()

		if (torrent == null)
			return "";
		String sRelativePath = "";

		LocaleUtilDecoder decoder = null;
		try {
			decoder = LocaleTorrentUtil.getTorrentEncodingIfAvailable(torrent);
		} catch (Exception e) {
			// Do Nothing
		}

		if (decoder != null) {
			for (int j = 0; j < path_components.length; j++) {

				try {
					String comp;
					try {
						comp = decoder.decodeString(path_components[j]);
					} catch (UnsupportedEncodingException e) {
						System.out.println("file - unsupported encoding!!!!");
						try {
							comp = new String(path_components[j]);
						} catch (Exception e2) {
							comp = "UnsupportedEncoding";
						}
					}
	
					comp = FileUtil.convertOSSpecificChars(comp);
	
					sRelativePath += (j == 0 ? "" : File.separator) + comp;
				} catch (Exception ex) {
					Debug.out(ex);
				}

			}

		}
		return sRelativePath;
	
public TOTorrentgetTorrent()

		return( torrent );
	
protected booleanisUTF8()

		return( is_utf8 );
	
protected voidsetAdditionalProperty(java.lang.String name, java.lang.Object value)

		additional_properties.put( name, value );