package com.coderising.download; import com.coderising.download.api.Connection; import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; import com.coderising.download.api.DownloadListener; public class FileDownloader { String url; DownloadListener listener; ConnectionManager cm; public FileDownloader(String _url) { this.url = _url; } public void execute(){ // ������ʵ����Ĵ��룬 ע�⣺ ��Ҫ�ö��߳�ʵ������ // ��������������������ӿ�, ����Ҫд�⼸���ӿڵ�ʵ�ִ��� // (1) ConnectionManager , ���Դ�һ�����ӣ�ͨ��Connection���Զ�ȡ���е�һ�Σ���startPos, endPos��ָ���� // (2) DownloadListener, �����Ƕ��߳����أ� ���������Ŀͻ��˲�֪��ʲôʱ���������������Ҫʵ�ֵ����� // �̶߳�ִ�����Ժ� ����listener��notifiedFinished������ �����ͻ��˾����յ�֪ͨ�� // �����ʵ��˼·�� // 1. ��Ҫ����ConnectionManager��open���������ӣ� Ȼ��ͨ��Connection.getContentLength��������ļ��ij��� // 2. ��������3���߳����أ� ע��ÿ���߳���Ҫ�ȵ���ConnectionManager��open���� // Ȼ�����read������ read�������ж�ȡ�ļ��Ŀ�ʼλ�úͽ���λ�õIJ����� ����ֵ��byte[]���� // 3. ��byte����д�뵽�ļ��� // 4. ���е��̶߳���������Ժ� ��Ҫ����listener��notifiedFinished���� // ����Ĵ�����ʾ�����룬 Ҳ����˵ֻ��һ���̣߳� ����Ҫ����ɶ��̵߳ġ� Connection conn = null; try { conn = cm.open(this.url); int length = conn.getContentLength(); int one=length/3; new DownloadThread(conn,0,one).start(); new DownloadThread(conn,one+1,one*2+1).start(); new DownloadThread(conn,(one+1)*2,length).start(); } catch (ConnectionException e) { e.printStackTrace(); }finally{ if(conn != null){ conn.close(); } } } public void setListener(DownloadListener listener) { this.listener = listener; } public void setConnectionManager(ConnectionManager ucm){ this.cm = ucm; } public DownloadListener getListener(){ return this.listener; } }