📄 totorrentdeserialiseimpl.java
字号:
urls.add( new URL( announce_url ));
URL[] url_array = new URL[ urls.size() ];
urls.copyInto( url_array );
addTorrentAnnounceURLSet( url_array );
}
catch (Exception e) { Debug.printStackTrace( e ); }
}
}
}else if ( key.equalsIgnoreCase( TK_COMMENT )){
setComment((byte[])meta_data.get( TK_COMMENT ));
}else if ( key.equalsIgnoreCase( TK_CREATED_BY )){
setCreatedBy((byte[])meta_data.get( TK_CREATED_BY ));
}else if ( key.equalsIgnoreCase( TK_CREATION_DATE )){
// non standard, don't fail if format wrong
try{
Long creation_date = (Long)meta_data.get( TK_CREATION_DATE );
if ( creation_date != null ){
setCreationDate( creation_date.longValue());
}
}catch( Exception e ){
System.out.println( "creation_date extraction fails, ignoring");
}
}else if ( key.equalsIgnoreCase( TK_INFO )){
// processed later
}else{
Object prop = meta_data.get( key );
if ( prop instanceof byte[] ){
setAdditionalByteArrayProperty( key, (byte[])prop );
}else if ( prop instanceof Long ){
setAdditionalLongProperty( key, (Long)prop );
}else if ( prop instanceof List ){
setAdditionalListProperty( key, (List)prop );
}else{
setAdditionalMapProperty( key, (Map)prop );
}
}
}
if ( bad_announce ){
if ( got_announce_list ){
TOTorrentAnnounceURLSet[] sets = getAnnounceURLGroup().getAnnounceURLSets();
if ( sets.length > 0 ){
setAnnounceURL( sets[0].getAnnounceURLs()[0]);
}else{
throw( new TOTorrentException( "ANNOUNCE_URL malformed ('" + announce_url + "' and no usable announce list)",
TOTorrentException.RT_DECODE_FAILS ));
}
}else{
throw( new TOTorrentException( "ANNOUNCE_URL malformed ('" + announce_url + "'",
TOTorrentException.RT_DECODE_FAILS ));
}
}
if ( ! ( got_announce_list || got_announce )){
setAnnounceURL( TorrentUtils.getDecentralisedEmptyURL());
}
Map info = (Map)meta_data.get( TK_INFO );
if ( info == null ){
throw( new TOTorrentException( "Decode fails, 'info' element not found'",
TOTorrentException.RT_DECODE_FAILS ));
}
setName((byte[])info.get( TK_NAME ));
setHashFromInfo( info );
Long simple_file_length = (Long)info.get( TK_LENGTH );
long total_length = 0;
if ( simple_file_length != null ){
setSimpleTorrent( true );
total_length = simple_file_length.longValue();
setFiles( new TOTorrentFileImpl[]{ new TOTorrentFileImpl( this, total_length, new byte[][]{getName()})});
}else{
setSimpleTorrent( false );
List meta_files = (List)info.get( TK_FILES );
TOTorrentFileImpl[] files = new TOTorrentFileImpl[ meta_files.size()];
for (int i=0;i<files.length;i++){
Map file_map = (Map)meta_files.get(i);
long len = ((Long)file_map.get( TK_LENGTH )).longValue();
total_length += len;
List paths = (List)file_map.get( TK_PATH );
byte[][] path_comps = new byte[paths.size()][];
for (int j=0;j<paths.size();j++){
path_comps[j] = (byte[])paths.get(j);
}
TOTorrentFileImpl file = files[i] = new TOTorrentFileImpl( this, len, path_comps );
// preserve any non-standard attributes
Iterator file_it = file_map.keySet().iterator();
while( file_it.hasNext()){
String key = (String)file_it.next();
if ( key.equals( TK_LENGTH ) ||
key.equals( TK_PATH )){
// standard
}else{
file.setAdditionalProperty( key, file_map.get( key ));
}
}
}
setFiles( files );
}
long piece_length = ((Long)info.get( TK_PIECE_LENGTH )).longValue();
if ( piece_length <= 0 ){
throw( new TOTorrentException( "Decode fails, piece-length is invalid",
TOTorrentException.RT_DECODE_FAILS ));
}
setPieceLength( piece_length );
byte[] flat_pieces = (byte[])info.get( TK_PIECES );
// work out how many pieces we require for the torrent
int pieces_required = (int)((total_length + (piece_length-1)) / piece_length);
int pieces_supplied = flat_pieces.length/20;
if ( pieces_supplied < pieces_required ){
throw( new TOTorrentException( "Decode fails, insufficient pieces supplied",
TOTorrentException.RT_DECODE_FAILS ));
}
if ( pieces_supplied > pieces_required ){
Debug.out( "Torrent '" + new String( getName()) + "' has too many pieces (required=" + pieces_required + ",supplied=" + pieces_supplied + ") - ignoring excess" );
}
byte[][]pieces = new byte[pieces_supplied][20];
for (int i=0;i<pieces.length;i++){
System.arraycopy( flat_pieces, i*20, pieces[i], 0, 20 );
}
setPieces( pieces );
// extract and additional info elements
Iterator info_it = info.keySet().iterator();
while( info_it.hasNext()){
String key = (String)info_it.next();
if ( key.equals( TK_NAME ) ||
key.equals( TK_LENGTH ) ||
key.equals( TK_FILES ) ||
key.equals( TK_PIECE_LENGTH ) ||
key.equals( TK_PIECES )){
// standard attributes
}else{
addAdditionalInfoProperty( key, info.get( key ));
}
}
}catch( Throwable e ){
if ( e instanceof TOTorrentException){
throw((TOTorrentException)e);
}
throw( new TOTorrentException( "Decode fails '" + e.toString() + "'",
TOTorrentException.RT_DECODE_FAILS, e ));
}
}
public void
printMap()
{
try{
print( "", "root", serialiseToMap());
}catch( TOTorrentException e ){
Debug.printStackTrace( e );
}
}
protected void
print(
String indent,
String name,
Map map )
{
System.out.println( indent + name + "{map}" );
Set keys = map.keySet();
Iterator it = keys.iterator();
while( it.hasNext()){
String key = (String)it.next();
Object value = map.get( key );
if ( value instanceof Map ){
print( indent+" ", key, (Map)value);
}else if ( value instanceof List ){
print( indent+" ", key, (List)value );
}else if ( value instanceof Long ){
print( indent+" ", key, (Long)value );
}else{
print( indent+" ", key, (byte[])value);
}
}
}
protected void
print(
String indent,
String name,
List list )
{
System.out.println( indent + name + "{list}" );
Iterator it = list.iterator();
int index = 0;
while( it.hasNext()){
Object value = it.next();
if ( value instanceof Map ){
print( indent+" ", "[" + index + "]", (Map)value);
}else if ( value instanceof List ){
print( indent+" ", "[" + index + "]", (List)value );
}else if ( value instanceof Long ){
print( indent+" ", "[" + index + "]", (Long)value );
}else{
print( indent+" ", "[" + index + "]", (byte[])value);
}
index++;
}
}
protected void
print(
String indent,
String name,
Long value )
{
System.out.println( indent + name + "{long} = " + value.longValue());
}
protected void
print(
String indent,
String name,
byte[] value )
{
String x = new String(value);
boolean print = true;
for (int i=0;i<x.length();i++){
char c = x.charAt(i);
if ( c < 128 ){
}else{
print = false;
break;
}
}
if ( print ){
System.out.println( indent + name + "{byte[]} = " + x );
}else{
System.out.println( indent + name + "{byte[], length " + value.length + "}" );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -