return;
}
PEPeerControl control = getPeerControl();
DiskManager dm = control.getDiskManager();
if ( dm == null ){
Debug.out( "Disk manager is null" );
throw( new IOException( "Disk manager unavailable" ));
}
TOTorrent to_torrent = dm.getTorrent();
char[] chars = header.toCharArray();
int last_pos = 0;
int line_num = 0;
String target_str = null;
DiskManagerFileInfo target_file = null;
long file_offset = 0;
List<long[]> ranges = new ArrayList<long[]>();
boolean keep_alive = false;
for (int i=1;i<chars.length;i++){
if ( chars[i-1] == '\r' && chars[i] == '\n' ){
String line = new String( chars, last_pos, i - last_pos ).trim();
last_pos = i;
line_num++;
// System.out.println( "line " + line_num + " -> " + line );
if ( line_num == 1 ){
line = line.substring( line.indexOf( "files/" ) + 6 );
int hash_end = line.indexOf( "/" );
final byte[] old_hash = control.getHash();
final byte[] new_hash = URLDecoder.decode(line.substring(0, hash_end), "ISO-8859-1").getBytes( "ISO-8859-1" );
if ( !Arrays.equals( new_hash, old_hash )){
switching = true;
decoder.pauseInternally();
flushRequests(
new flushListener()
{
private boolean triggered;
public void
flushed()
{
synchronized( this ){
if ( triggered ){
return;
}
triggered = true;
}
getManager().reRoute(
HTTPNetworkConnectionFile.this,
old_hash, new_hash, header );
}
});
return;
}
line = line.substring( hash_end + 1 );
line = line.substring( 0, line.lastIndexOf( ' ' ));
String file = line;
if ( to_torrent.isSimpleTorrent()){
// optimise for simple torrents. also support the case where
// client has the hash but doesn't know the file name
target_file = dm.getFiles()[0];
}else{
target_str = file;
StringTokenizer tok = new StringTokenizer( file, "/" );
List<byte[]> bits = new ArrayList<byte[]>();
while( tok.hasMoreTokens()){
bits.add( URLDecoder.decode(tok.nextToken(), "ISO-8859-1").getBytes( "ISO-8859-1" ));
}
// latest spec has torrent file name encoded first for non-simple torrents
// remove it if we find it so we have some backward compat
if ( !to_torrent.isSimpleTorrent() && bits.size() > 1 ){
if ( Arrays.equals( to_torrent.getName(), (byte[])bits.get(0))){
bits.remove(0);
}
}
DiskManagerFileInfo[] files = dm.getFiles();
file_offset = 0;
for (int j=0;j<files.length;j++){