@Override public void run() { if (iListener != null) iListener.onStart(); if (list != null) { int length = list.size(); for (int i = 0; i < length; i++) { Point p = list.get(i); down(p); } } }
private void down(Point point) { BufferedInputStream in = null; RandomAccessFile raf = null; try { HttpURLConnection conn = (HttpURLConnection) point.URL.openConnection(); conn.connect(); checkRange(conn, point); // Make sure the response code is in the 200 range. if (conn.getResponseCode() / 100 != 2) { setState(State.ERROR); } in = new BufferedInputStream(conn.getInputStream()); // open the output file and seek to the start location raf = new RandomAccessFile(dataOuter.outputFile, "rw"); raf.seek(point.start); byte data[] = new byte[BUFFER_SIZE]; int numRead; while ((mState.isLoading()) && ((numRead = in.read(data, 0, BUFFER_SIZE)) != -1)) { raf.write(data, 0, numRead); // increase the startByte for resume later point.start += numRead; // increase the downloaded size downloaded(numRead); } if (mState.isLoading()) { setState(State.COMPLETED); if (iListener != null) iListener.onStop(); } } catch (Exception e) { setState(State.ERROR); } finally { try { if (raf != null) raf.close(); if (in != null) in.close(); } catch (IOException e) { } } System.out.println("End thread "); }
void downloaded(int progress) { if (iListener != null) iListener.onProgress(progress); }