TimeoutWorker worker = new TimeoutWorker((Object) url) {
URL finalUrl = null; String HTTPProxy = trackDatabase.getHTTPProxy(); if ((HTTPProxy!=null) && (!HTTPProxy.trim().equals("")) ) { System.out.println("\nUsing HTTPProxy:"+HTTPProxy+"\n"); finalUrl = new URL ("http",HTTPProxy,trackDatabase.getHTTPProxyPort(),url.toString());   } else { System.out.println("\nNo HTTP proxy in use.\n"); finalUrl = url; }   TimeoutWorker worker = new TimeoutWorker((Object) finalUrl) {
  public void download(Track track) throws IOException {    try {      try {        URL url = track.getURL();        String urlString = url.toString();        int index = urlString.lastIndexOf('/');        if (index > 0)          urlString = urlString.substring(index + 1);        while (true) {          index = urlString.indexOf("%20");          if (index < 0)            break;          urlString = urlString.substring(0, index) + " " + urlString.substring(index + 3);        }        File file = new File(downloadDir, urlString);        setState("Connecting " + track.getName());        //0 second timeout for the impatient        long timeout = 60000;        TimeoutWorker worker = new TimeoutWorker((Object) url) {          public void run() {            try {              URLConnection conn = ((URL) input).openConnection();              conn.connect();              Vector v = new Vector();              v.add(conn);              v.add(new Integer(conn.getContentLength()));              setOutput(v);            }            catch (IOException e) {              setException(e);            }          }        };        URLConnection conn;        Integer intContentLength;        try {          Vector v = (Vector) worker.runOrTimeout(timeout);          conn = (URLConnection) v.elementAt(0);          intContentLength = (Integer) v.elementAt(1);        }        catch (Exception e) {          setState("Could not connect: " + e);          e.printStackTrace();          return;        }        //get rid of the problem where tracks are downloaded but in reality they are 404 messages or some other html crud		String contentType = conn.getContentType();        if (contentType.indexOf("text") != -1) {          track.setBroken();          track.setRating(0);          return;        }        worker = null;        int contentLength = intContentLength.intValue();        setState("Downloading " + track.getName());        final InputStream is = conn.getInputStream();        OutputStream os = new FileOutputStream(file);        final byte buf[] = new byte[128000];        int totalBytes = 0;        worker = new TimeoutWorker((Object) is){          public void run() {            int n;            try {              n = is.read(buf);            } catch(Exception e) {              setException(e);              return;            }            setOutput(new Integer(n));          }        };        while (true) {          int nbytes;          try {            nbytes = ((Integer) worker.runOrTimeout(timeout)).intValue();          } catch(Exception e) {            e.printStackTrace();            return;          }          if (nbytes < 0)            break;          os.write(buf, 0, nbytes);          if (contentLength >= 0) {            totalBytes += nbytes;            int percent = totalBytes * 100 / contentLength;            if (percent != percentComplete) {              percentComplete = percent;              notifyUpdateListeners();            }          }        }        os.close();        is.close();        track.setFile(file);        trackDatabase.save();      }      finally {        //if (is != null) try { is.close(); } catch (IOException e) { e.printStackTrace(); }        //if (os != null) try { os.close(); } catch (IOException e) { e.printStackTrace(); }        percentComplete = 0;      }    }    catch (FileNotFoundException fnfe) {      currentTrack.setBroken();    }    catch (IOException e) {      e.printStackTrace();    }  }