Skip to content

Commit

Permalink
Merge pull request #46 from maishihang/master
Browse files Browse the repository at this point in the history
complete all  homework
  • Loading branch information
guodongym authored Apr 5, 2017
2 parents fa90c8e + 6be7e88 commit 80ede5d
Show file tree
Hide file tree
Showing 26 changed files with 1,116 additions and 273 deletions.
31 changes: 28 additions & 3 deletions group12/446031103/src/com/coderising/download/DownloadThread.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
package com.coderising.download;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import com.coderising.download.api.Connection;

public class DownloadThread extends Thread{

Connection conn;
int startPos;
int endPos;

public DownloadThread( Connection conn, int startPos, int endPos){
CyclicBarrier barrier;
String localFile;
public DownloadThread( Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){

this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
this.localFile = localFile;
this.barrier = barrier;
}
public void run(){

try {
byte[] data =conn.read(startPos, endPos);
RandomAccessFile file = new RandomAccessFile(localFile,"rw");
file.seek(startPos);
file.write(data);
file.close();
conn.close();
barrier.await();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
66 changes: 62 additions & 4 deletions group12/446031103/src/com/coderising/download/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.coderising.download;


import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.CyclicBarrier;

import com.coderising.download.api.Connection;
import com.coderising.download.api.ConnectionException;
import com.coderising.download.api.ConnectionManager;
import com.coderising.download.api.DownloadListener;

Expand All @@ -14,9 +18,12 @@ public class FileDownloader {

ConnectionManager cm;

private String localFile;
private static final int DOWNLOAD_TRHEAD_NUM = 3;

public FileDownloader(String _url) {
public FileDownloader(String _url,String _localFile) {
this.url = _url;
this.localFile = _localFile;

}

Expand All @@ -34,16 +41,36 @@ public void execute(){
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法

// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){
public void run(){
listener.notifyFinished();
}
});

Connection conn = null;
try {

conn = cm.open(this.url);

int length = conn.getContentLength();

createPlaceHolderFile(this.localFile,length);

int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length);

new DownloadThread(conn,0,length-1).start();
for(int i=0; i< DOWNLOAD_TRHEAD_NUM; i++){

} catch (ConnectionException e) {

DownloadThread thread = new DownloadThread(
cm.open(url),
ranges[i][0],
ranges[i][1],
localFile,
barrier);

thread.start();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn != null){
Expand All @@ -56,6 +83,37 @@ public void execute(){

}

private int[][] allocateDownloadRange(int threadNum, int contentLen) {
int[][] ranges = new int[threadNum][2];

int eachThreadSize = contentLen / threadNum;// 每个线程需要下载的文件大小
int left = contentLen % threadNum;// 剩下的归最后一个线程来处理

for(int i=0;i<threadNum;i++){

int startPos = i * eachThreadSize;

int endPos = (i + 1) * eachThreadSize - 1;

if ((i == (threadNum - 1))) {
endPos += left;
}
ranges[i][0] = startPos;
ranges[i][1] = endPos;

}

return ranges;
}

private void createPlaceHolderFile(String fileName, int length) throws IOException {
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
for (int i = 0; i < length; i++) {
raf.write(0);
}
raf.close();
}

public void setListener(DownloadListener listener) {
this.listener = listener;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
package com.coderising.download.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;

import com.coderising.download.api.Connection;

public class ConnectionImpl implements Connection{

class ConnectionImpl implements Connection{
static final int BUFFER_SIZE = 1024;
URL url;
ConnectionImpl(String theUrl) {
try {
url=new URL(theUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public byte[] read(int startPos, int endPos) throws IOException {

return null;
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
//?????
httpConn.setRequestProperty("Range", "bytes=" + startPos + "-"
+ endPos);
InputStream is = httpConn.getInputStream();
byte[] buff = new byte[BUFFER_SIZE];
int totalLen = endPos - startPos + 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while(baos.size()<totalLen){
int len = is.read();
if(len<0){
break;
}
//????
baos.write(buff,0, len);
}
if(baos.size() > totalLen){
byte[] data = baos.toByteArray();
return Arrays.copyOf(data, totalLen);
}
return baos.toByteArray();
}

@Override
public int getContentLength() {

URLConnection openConnection;
try {
openConnection = url.openConnection();
return openConnection.getContentLength();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ConnectionManagerImpl implements ConnectionManager {
@Override
public Connection open(String url) throws ConnectionException {

return null;
return new ConnectionImpl(url);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.coderising.download.test;



import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.coderising.download.api.Connection;
import com.coderising.download.api.ConnectionManager;
import com.coderising.download.impl.ConnectionManagerImpl;

public class ConnectionTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testContentLength() throws Exception{
ConnectionManager connMan = new ConnectionManagerImpl();
Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg");
Assert.assertEquals(35470, conn.getContentLength());
}

@Test
public void testRead() throws Exception{

ConnectionManager connMan = new ConnectionManagerImpl();
Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg");

byte[] data = conn.read(0, 35469);

Assert.assertEquals(35470, data.length);

data = conn.read(0, 1023);

Assert.assertEquals(1024, data.length);

data = conn.read(1024, 2023);

Assert.assertEquals(1000, data.length);


// 测试不充分,没有断言内容是否正确
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.coderising.download;
package com.coderising.download.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.coderising.download.FileDownloader;
import com.coderising.download.api.ConnectionManager;
import com.coderising.download.api.DownloadListener;
import com.coderising.download.impl.ConnectionManagerImpl;
Expand All @@ -21,9 +22,9 @@ public void tearDown() throws Exception {
@Test
public void testDownload() {

String url = "http://localhost:8080/test.jpg";
String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png";

FileDownloader downloader = new FileDownloader(url);
FileDownloader downloader = new FileDownloader(url,"E:\\TEST\\test.jpg");


ConnectionManager cm = new ConnectionManagerImpl();
Expand Down Expand Up @@ -52,7 +53,6 @@ public void notifyFinished() {
}
System.out.println("下载完成!");



}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.coderising.jvm.loader;

import java.util.ArrayList;
import java.util.List;



public class ClassFileLoader {

private List<String> clzPaths = new ArrayList<String>();

public byte[] readBinaryCode(String className) {

return null;


}

private byte[] loadClassFile(String clzFileName) {

return null;
}



public void addClassPath(String path) {

}

public String getClassPath_V1(){

return null;
}

public String getClassPath(){
return null;
}





}
Loading

0 comments on commit 80ede5d

Please sign in to comment.