Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提交第3次作业及第4次作业框架 #29

Merged
merged 5 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class ArrayUtil {
*/
public static int[] reverseArray(int[] origin){
int length = origin.length;
if(origin == null || length == 0){
return null;
}

for(int i=0;i<length/2;i++){
int tmp = origin[i];
origin[i] = origin[length - 1 - i];
Expand Down Expand Up @@ -69,7 +73,7 @@ private static Integer[] convertToIntegerArray(int[] array){
return returnArray;
}

private static int[] returnByIntArray(Collection<Integer> collection){
public static int[] returnByIntArray(Collection<Integer> collection){
int[] returnArray = new int[collection.size()];
int i = 0;
for(Iterator<Integer> it = collection.iterator(); it.hasNext();){
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.chaoswang.learning.java.downloader;

import java.io.IOException;
import java.io.RandomAccessFile;

import com.github.chaoswang.learning.java.downloader.api.Connection;

public class DownloadThread extends Thread{

private Connection conn;
private int startPos;
private int endPos;

public DownloadThread(Connection conn, int totalSection, int sectionIndex){
this.conn = conn;
int contentLength = conn.getContentLength();
initStartPosAndEndPos(contentLength, totalSection, sectionIndex);
}

private void initStartPosAndEndPos(int contentLength, int totalSection, int sectionIndex){
int sectionLength = contentLength / totalSection;
startPos = (sectionIndex - 1) * sectionLength;
if(sectionIndex == totalSection){
endPos = contentLength - 1;
}else{
endPos = sectionIndex * sectionLength - 1;
}
}

public void run(){
try {
writeByteArrayToFile(conn.read(startPos, endPos), "F:\\tmp\\6977.png");
} catch (IOException e) {
e.printStackTrace();
}
}

private void writeByteArrayToFile(byte[] buf, String destFilePath){
try {
// ����һ���ɶ���д����������ļ�
RandomAccessFile out = new RandomAccessFile(destFilePath, "rw");
out.seek(startPos);// ��ָ��λ�ÿ�ʼд
out.write(buf);
out.close();// ���ﵽ��ر��ļ�
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.github.chaoswang.learning.java.downloader;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import com.github.chaoswang.learning.java.downloader.api.Connection;
import com.github.chaoswang.learning.java.downloader.api.ConnectionException;
import com.github.chaoswang.learning.java.downloader.api.ConnectionManager;
import com.github.chaoswang.learning.java.downloader.api.DownloadListener;

public class FileDownloader {
String url;
DownloadListener listener;
ConnectionManager cm;
int threadNum = 10;

public FileDownloader(String _url, int threadNum) {
this.url = _url;
this.threadNum = threadNum;
}

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����

// ����Ĵ�����ʾ�����룬 Ҳ����˵ֻ��һ���̣߳� ����Ҫ����ɶ��̵߳ġ�
// ���http://blog.csdn.net/yan8024/article/details/46474239

long startTime = System.currentTimeMillis();
//�ж������߳��Ƿ��������
ArrayList<Thread> list = new ArrayList<Thread>();

try{
for(int i=1; i<=threadNum; i++){
Connection conn = cm.open(url);
Thread dt = new DownloadThread(conn, threadNum, i);
dt.start();
list.add(dt);
}
}catch(ConnectionException e){
e.printStackTrace();
return;
}

while(true){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
if(!isAllFinished(list)){
continue;
}
System.out.println("finished, cost time:" + (System.currentTimeMillis() - startTime));
listener.notifyFinished();
break;
}
}

private boolean isAllFinished(ArrayList<Thread> list){
for(Thread t : list){
if(t.getState() != Thread.State.TERMINATED){
return false;
}
}
return true;
}

public void setListener(DownloadListener listener) {
this.listener = listener;
}

public void setConnectionManager(ConnectionManager ucm){
this.cm = ucm;
}

public DownloadListener getListener(){
return this.listener;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.chaoswang.learning.java.downloader.api;

import java.io.IOException;

public interface Connection {
/**
* ������ʼ�ͽ���λ�ã� ��ȡ���ݣ� ����ֵ���ֽ�����
* @param startPos ��ʼλ�ã� ��0��ʼ
* @param endPos �����
* @return
*/
public byte[] read(int startPos,int endPos) throws IOException;
/**
* �õ��������ݵij���
* @return
*/
public int getContentLength();

/**
* �ر�����
*/
public void close();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.chaoswang.learning.java.downloader.api;

public class ConnectionException extends Exception {
private static final long serialVersionUID = -6832188361613061488L;

public ConnectionException(String message){
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.chaoswang.learning.java.downloader.api;

public interface ConnectionManager {
/**
* ����һ��url , ��һ������
* @param url
* @return
*/
public Connection open(String url) throws ConnectionException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.chaoswang.learning.java.downloader.api;

public interface DownloadListener {
public void notifyFinished();
}
Loading