📄 totorrentimpl.java
字号:
{
if ( torrent_hash == null ){
Map root = serialiseToMap();
Map info = (Map)root.get( TK_INFO );
setHashFromInfo( info );
}
return( torrent_hash );
}
public HashWrapper
getHashWrapper()
throws TOTorrentException
{
if ( torrent_hash_wrapper == null ){
getHash();
}
return( torrent_hash_wrapper );
}
public boolean
hasSameHashAs(
TOTorrent other )
{
try{
byte[] other_hash = other.getHash();
return( Arrays.equals( getHash(), other_hash ));
}catch( TOTorrentException e ){
Debug.printStackTrace( e );
return( false );
}
}
protected void
setHashFromInfo(
Map info )
throws TOTorrentException
{
try{
SHA1Hasher s = new SHA1Hasher();
torrent_hash = s.calculateHash(BEncoder.encode(info));
torrent_hash_wrapper = new HashWrapper( torrent_hash );
}catch( Throwable e ){
throw( new TOTorrentException( "TOTorrent::setHashFromInfo: fails '" + e.toString() + "'",
TOTorrentException.RT_HASH_FAILS ));
}
}
public TOTorrentAnnounceURLGroup
getAnnounceURLGroup()
{
return( announce_group );
}
protected void
addTorrentAnnounceURLSet(
URL[] urls )
{
announce_group.addSet( new TOTorrentAnnounceURLSetImpl( urls ));
}
public long
getSize()
{
long res = 0;
for (int i=0;i<files.length;i++){
res += files[i].getLength();
}
return( res );
}
public long
getPieceLength()
{
return( piece_length );
}
protected void
setPieceLength(
long _length )
{
piece_length = _length;
}
public int
getNumberOfPieces()
{
return( number_of_pieces );
}
public byte[][]
getPieces()
{
return( pieces );
}
public void
setPieces(
byte[][] _pieces )
{
pieces = _pieces;
if ( pieces != null ){
number_of_pieces = pieces.length;
}
}
public TOTorrentFile[]
getFiles()
{
return( files );
}
protected void
setFiles(
TOTorrentFileImpl[] _files )
{
files = _files;
}
protected boolean
getSimpleTorrent()
{
return( simple_torrent );
}
protected void
setSimpleTorrent(
boolean _simple_torrent )
{
simple_torrent = _simple_torrent;
}
protected Map
getAdditionalProperties()
{
return( additional_properties );
}
public void
setAdditionalStringProperty(
String name,
String value )
{
try{
setAdditionalByteArrayProperty( name, writeStringToMetaData( value ));
}catch( TOTorrentException e ){
// hide encoding exceptions as default encoding must be available
Debug.printStackTrace( e );
}
}
public String
getAdditionalStringProperty(
String name )
{
try{
return( readStringFromMetaData( getAdditionalByteArrayProperty(name)));
}catch( TOTorrentException e ){
// hide encoding exceptions as default encoding must be available
Debug.printStackTrace( e );
return( null );
}
}
public void
setAdditionalByteArrayProperty(
String name,
byte[] value )
{
additional_properties.put( name, value );
}
public void
setAdditionalProperty(
String name,
Object value )
{
if ( name instanceof String ){
setAdditionalStringProperty(name,(String)value);
}else{
additional_properties.put( name, value );
}
}
public byte[]
getAdditionalByteArrayProperty(
String name )
{
return((byte[])additional_properties.get( name ));
}
public void
setAdditionalLongProperty(
String name,
Long value )
{
additional_properties.put( name, value );
}
public Long
getAdditionalLongProperty(
String name )
{
return((Long)additional_properties.get( name ));
}
public void
setAdditionalListProperty(
String name,
List value )
{
additional_properties.put( name, value );
}
public List
getAdditionalListProperty(
String name )
{
return((List)additional_properties.get( name ));
}
public void
setAdditionalMapProperty(
String name,
Map value )
{
additional_properties.put( name, value );
}
public Map
getAdditionalMapProperty(
String name )
{
return((Map)additional_properties.get( name ));
}
public Object
getAdditionalProperty(
String name )
{
return(additional_properties.get( name ));
}
public void
removeAdditionalProperty(
String name )
{
additional_properties.remove( name );
}
public void
removeAdditionalProperties()
{
Map new_props = new HashMap();
Iterator it = additional_properties.keySet().iterator();
while( it.hasNext()){
String key = (String)it.next();
if ( TK_ADDITIONAL_OK_ATTRS.contains(key)){
new_props.put( key, additional_properties.get( key ));
}
}
additional_properties = new_props;
}
protected void
addAdditionalProperty(
String name,
Object value )
{
additional_properties.put( name, value );
}
protected void
addAdditionalInfoProperty(
String name,
Object value )
{
additional_info_properties.put( name, value );
}
protected Map
getAdditionalInfoProperties()
{
return( additional_info_properties );
}
protected String
readStringFromMetaData(
Map meta_data,
String name )
throws TOTorrentException
{
return(readStringFromMetaData((byte[])meta_data.get(name)));
}
protected String
readStringFromMetaData(
byte[] value )
throws TOTorrentException
{
try{
if ( value == null ){
return( null );
}
return( new String(value, Constants.DEFAULT_ENCODING ));
}catch( UnsupportedEncodingException e ){
throw( new TOTorrentException( "TOTorrentDeserialise: unsupported encoding for '" + value + "'",
TOTorrentException.RT_UNSUPPORTED_ENCODING));
}
}
protected void
writeStringToMetaData(
Map meta_data,
String name,
String value )
throws TOTorrentException
{
meta_data.put( name, writeStringToMetaData( value ));
}
protected byte[]
writeStringToMetaData(
String value )
throws TOTorrentException
{
try{
return( value.getBytes( Constants.DEFAULT_ENCODING ));
}catch( UnsupportedEncodingException e ){
throw( new TOTorrentException( "TOTorrent::writeStringToMetaData: unsupported encoding for '" + value + "'",
TOTorrentException.RT_UNSUPPORTED_ENCODING));
}
}
public void
print()
{
try{
byte[] hash = getHash();
System.out.println( "name = " + torrent_name );
System.out.println( "announce url = " + announce_url );
System.out.println( "announce group = " + announce_group.getAnnounceURLSets().length );
System.out.println( "creation date = " + creation_date );
System.out.println( "creation by = " + created_by );
System.out.println( "comment = " + comment );
System.out.println( "hash = " + ByteFormatter.nicePrint( hash ));
System.out.println( "piece length = " + getPieceLength() );
System.out.println( "pieces = " + getNumberOfPieces() );
Iterator info_it = additional_info_properties.keySet().iterator();
while( info_it.hasNext()){
String key = (String)info_it.next();
Object value = additional_info_properties.get( key );
try{
System.out.println( "info prop '" + key + "' = '" +
( value instanceof byte[]?new String((byte[])value, Constants.DEFAULT_ENCODING):value.toString()) + "'" );
}catch( UnsupportedEncodingException e){
System.out.println( "info prop '" + key + "' = unsupported encoding!!!!");
}
}
Iterator it = additional_properties.keySet().iterator();
while( it.hasNext()){
String key = (String)it.next();
Object value = additional_properties.get( key );
try{
System.out.println( "prop '" + key + "' = '" +
( value instanceof byte[]?new String((byte[])value, Constants.DEFAULT_ENCODING):value.toString()) + "'" );
}catch( UnsupportedEncodingException e){
System.out.println( "prop '" + key + "' = unsupported encoding!!!!");
}
}
if ( pieces == null ){
System.out.println( "\tpieces = null" );
}else{
for (int i=0;i<pieces.length;i++){
System.out.println( "\t" + ByteFormatter.nicePrint(pieces[i]));
}
}
for (int i=0;i<files.length;i++){
byte[][]path_comps = files[i].getPathComponents();
String path_str = "";
for (int j=0;j<path_comps.length;j++){
try{
path_str += (j==0?"":File.separator) + new String( path_comps[j], Constants.DEFAULT_ENCODING );
}catch( UnsupportedEncodingException e ){
System.out.println( "file - unsupported encoding!!!!");
}
}
System.out.println( "\t" + path_str + " (" + files[i].getLength() + ")" );
}
}catch( TOTorrentException e ){
Debug.printStackTrace( e );
}
}
public AEMonitor
getMonitor()
{
return( this_mon );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -