📄 totorrentdeserialiseimpl.java
字号:
/*
* File : TOTorrentDeserialiseImpl.java
* Created : 5 Oct. 2003
* By : Parg
*
* Azureus - a Java Bittorrent client
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details ( see the LICENSE file ).
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.gudy.azureus2.core3.torrent.impl;
import java.io.*;
import java.net.*;
import java.util.*;
import org.gudy.azureus2.core3.html.HTMLUtils;
import org.gudy.azureus2.core3.torrent.*;
import org.gudy.azureus2.core3.util.*;
public class
TOTorrentDeserialiseImpl
extends TOTorrentImpl
{
public
TOTorrentDeserialiseImpl(
File file )
throws TOTorrentException
{
if ( !file.exists()){
throw( new TOTorrentException( "Torrent file '" + file.toString() + "' does not exist",
TOTorrentException.RT_FILE_NOT_FOUND ));
}
if(!file.isFile()) {
throw( new TOTorrentException( "Torrent must be a file ('" + file.toString() + "')",
TOTorrentException.RT_FILE_NOT_FOUND ));
}
if ( file.length() == 0 ){
throw( new TOTorrentException( "Torrent is zero length ('" + file.toString() + "')",
TOTorrentException.RT_ZERO_LENGTH ));
}
// parg: there used to be a check made that the torrent file wasn't larger than 1MB.
// However, this as been exceeded! (see bug 826617)
// As there is no technical reason for this limit I have removed it
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
construct( fis );
}catch( Throwable e ){
throw( new TOTorrentException( "Error reading torrent file '" + file.toString() + " - " + Debug.getNestedExceptionMessage(e),
TOTorrentException.RT_READ_FAILS ));
}finally{
if ( fis != null ){
try{
fis.close();
}catch( IOException e ){
Debug.printStackTrace( e );
}
}
}
}
public
TOTorrentDeserialiseImpl(
InputStream is )
throws TOTorrentException
{
construct( is );
}
public
TOTorrentDeserialiseImpl(
byte[] bytes )
throws TOTorrentException
{
construct( bytes );
}
public
TOTorrentDeserialiseImpl(
Map map )
throws TOTorrentException
{
construct( map );
}
protected void
construct(
InputStream is )
throws TOTorrentException
{
ByteArrayOutputStream metaInfo = new ByteArrayOutputStream();
try{
byte[] buf = new byte[32*1024]; // raised this limit as 2k was rather too small
// do a check to see if it's a BEncode file.
int iFirstByte = is.read();
if ( iFirstByte != 'd' &&
iFirstByte != 'e' &&
iFirstByte != 'i' &&
!(iFirstByte >= '0' && iFirstByte <= '9')){
// often people download an HTML file by accident - if it looks like HTML
// then produce a more informative error
try{
metaInfo.write(iFirstByte);
int nbRead;
while ((nbRead = is.read(buf)) > 0 && metaInfo.size() < 32000 ){
metaInfo.write(buf, 0, nbRead);
}
String char_data = new String( metaInfo.toByteArray());
if ( char_data.toLowerCase().indexOf( "html") != -1 ){
char_data = HTMLUtils.convertHTMLToText2( char_data );
char_data = HTMLUtils.splitWithLineLength( char_data, 80 );
if ( char_data.length() > 400 ){
char_data = char_data.substring(0,400) + "...";
}
throw( new TOTorrentException(
"Contents maybe HTML:\n" + char_data,
TOTorrentException.RT_DECODE_FAILS ));
}
}catch( Throwable e ){
if ( e instanceof TOTorrentException ){
throw((TOTorrentException)e);
}
// ignore this
}
throw( new TOTorrentException( "Contents invalid - bad header",
TOTorrentException.RT_DECODE_FAILS ));
}
metaInfo.write(iFirstByte);
int nbRead;
while ((nbRead = is.read(buf)) > 0){
metaInfo.write(buf, 0, nbRead);
}
}catch( Throwable e ){
throw( new TOTorrentException( "Error reading torrent: " + Debug.getNestedExceptionMessage(e),
TOTorrentException.RT_READ_FAILS ));
}
construct( metaInfo.toByteArray());
}
protected void
construct(
byte[] bytes )
throws TOTorrentException
{
try{
Map meta_data = BDecoder.decode(bytes);
// print( "", "", meta_data );
construct( meta_data );
}catch( IOException e ){
throw( new TOTorrentException( "Error reading torrent: " + Debug.getNestedExceptionMessage(e),
TOTorrentException.RT_DECODE_FAILS, e ));
}
}
protected void
construct(
Map meta_data )
throws TOTorrentException
{
try{
String announce_url = null;
boolean got_announce = false;
boolean got_announce_list = false;
boolean bad_announce = false;
// decode the stuff
Iterator root_it = meta_data.keySet().iterator();
while( root_it.hasNext()){
String key = (String)root_it.next();
if ( key.equalsIgnoreCase( TK_ANNOUNCE )){
got_announce = true;
announce_url = readStringFromMetaData( meta_data, TK_ANNOUNCE );
if ( announce_url == null ){
bad_announce = true;
}else{
announce_url = announce_url.replaceAll( " ", "" );
try{
setAnnounceURL( new URL( announce_url ));
}catch( MalformedURLException e ){
if ( announce_url.indexOf( "://" ) == -1 ){
announce_url = "http:/" + (announce_url.startsWith("/")?"":"/") + announce_url;
}
try{
setAnnounceURL( new URL( announce_url ));
}catch( MalformedURLException f ){
bad_announce = true;
}
}
}
}else if ( key.equalsIgnoreCase( TK_ANNOUNCE_LIST )){
got_announce_list = true;
List announce_list = null;
Object ann_list = meta_data.get( TK_ANNOUNCE_LIST );
if( ann_list instanceof List ) { //some malformed torrents have this key as a zero-sized string instead of a zero-sized list
announce_list = (List)ann_list;
}
if ( announce_list != null && announce_list.size() > 0 ){
announce_url = readStringFromMetaData( meta_data, TK_ANNOUNCE );
if ( announce_url != null ){
announce_url = announce_url.replaceAll( " ", "" );
}
boolean announce_url_found = false;
for (int i=0;i<announce_list.size();i++){
List set = (List)announce_list.get(i);
Vector urls = new Vector();
for (int j=0;j<set.size();j++){
String url_str = readStringFromMetaData((byte[])set.get(j));
url_str=url_str.replaceAll( " ", "" );
//check to see if the announce url is somewhere in the announce-list
try{
urls.add( new URL( url_str ));
if ( url_str.equalsIgnoreCase( announce_url )) {
announce_url_found = true;
}
}catch( MalformedURLException e ){
if ( url_str.indexOf( "://" ) == -1 ){
url_str = "http:/" + (url_str.startsWith("/")?"":"/") + url_str;
}
try{
urls.add( new URL( url_str ));
if ( url_str.equalsIgnoreCase( announce_url )) {
announce_url_found = true;
}
}catch( MalformedURLException f ){
Debug.printStackTrace( f );
}
}
}
if ( urls.size() > 0 ){
URL[] url_array = new URL[urls.size()];
urls.copyInto( url_array );
addTorrentAnnounceURLSet( url_array );
}
}
//if the original announce url isn't found, add it to the list
// watch out for those invalid torrents with announce url missing
if ( !announce_url_found && announce_url != null && announce_url.length() > 0) {
try {
Vector urls = new Vector();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -