Skip to content

Commit 0c97ee1

Browse files
committed
Merge branch 'dev'
2 parents 4116f15 + 43c317f commit 0c97ee1

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.coderising.download;
22

3+
import java.io.File;
34
import java.io.IOException;
5+
import java.io.RandomAccessFile;
6+
import java.util.concurrent.CountDownLatch;
47

58
import com.coderising.download.api.Connection;
69

@@ -9,19 +12,34 @@ public class DownloadThread extends Thread{
912
Connection conn;
1013
int startPos;
1114
int endPos;
15+
CountDownLatch latch;
16+
17+
File file = new File("C://download.mp3");
1218

13-
public DownloadThread( Connection conn, int startPos, int endPos){
14-
19+
public DownloadThread( Connection conn, int startPos, int endPos, CountDownLatch latch){
1520
this.conn = conn;
1621
this.startPos = startPos;
1722
this.endPos = endPos;
23+
this.latch = latch;
1824
}
1925
public void run(){
26+
RandomAccessFile raf = null;
2027
try {
21-
conn.read(startPos, endPos);
28+
byte[] byteRead = conn.read(startPos, endPos);
29+
raf = new RandomAccessFile(file, "rw");;
30+
raf.seek(startPos);
31+
raf.write(byteRead);
32+
2233
} catch (IOException e) {
23-
// TODO Auto-generated catch block
2434
e.printStackTrace();
35+
}finally{
36+
latch.countDown();
37+
try {
38+
raf.close();
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
conn.close();
2543
}
2644
}
2745
}

group20/404130810/src/com/coderising/download/FileDownloader.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.coderising.download;
22

33
import java.io.IOException;
4+
import java.util.concurrent.CountDownLatch;
5+
import java.util.concurrent.CyclicBarrier;
46

57
import com.coderising.download.api.Connection;
68
import com.coderising.download.api.ConnectionException;
@@ -43,22 +45,25 @@ public void execute() throws IOException {
4345
conn = cm.open(url);
4446
int length = conn.getContentLength();
4547
int[] posArr = FileDownloadUtil.generateDownloadPosArr(length);
48+
CountDownLatch latch = new CountDownLatch(3);
4649
for (int i = 0; i < posArr.length; i++) {
47-
if(i == posArr.length - 1){
48-
new DownloadThread(cm.open(url), posArr[i], length).start();
49-
}else{
50-
new DownloadThread(cm.open(url), posArr[i], posArr[i + 1] - 1).start();
50+
if (i == posArr.length - 1) {
51+
new DownloadThread(cm.open(url), posArr[i], length, latch).start();
52+
} else {
53+
new DownloadThread(cm.open(url), posArr[i], posArr[i + 1] - 1, latch).start();
5154
}
5255
}
53-
56+
latch.await();
57+
System.out.println("Download Finished");
5458
} catch (ConnectionException e) {
5559
e.printStackTrace();
60+
} catch (InterruptedException e) {
61+
e.printStackTrace();
5662
} finally {
5763
if (conn != null) {
5864
conn.close();
5965
}
6066
}
61-
6267
}
6368

6469
public void setListener(DownloadListener listener) {

group20/404130810/src/com/coderising/download/FileDownloaderTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void tearDown() throws Exception {
2323
@Test
2424
public void testDownload() throws IOException {
2525

26-
String url = "http://localhost:8080/test.jpg";
26+
String url = "http://localhost:8080/MyServer/Test.mp3";
2727

2828
FileDownloader downloader = new FileDownloader(url);
2929

@@ -38,7 +38,6 @@ public void notifyFinished() {
3838
}
3939

4040
});
41-
4241

4342
downloader.execute();
4443

group20/404130810/src/com/coderising/download/impl/ConnectionImpl.java

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.coderising.download.impl;
22

3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
35
import java.io.IOException;
46
import java.io.InputStream;
57
import java.io.RandomAccessFile;
@@ -24,16 +26,29 @@ public ConnectionImpl(String urlStr) {
2426

2527
@Override
2628
public byte[] read(int startPos, int endPos) throws IOException {
27-
int length = endPos - startPos;
28-
byte[] rtnByte = new byte[length];
29-
30-
RandomAccessFile raf = new RandomAccessFile("test.mp3", "rw");
31-
raf.seek(startPos);
29+
System.out.println("Start Reading");
30+
System.out.println("StartPos: " + startPos);
31+
System.out.println("EndPos: " + endPos);
32+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
3233
InputStream is = httpConn.getInputStream();
33-
while ((length = is.read(rtnByte)) != -1) {
34-
raf.write(rtnByte, 0, length);
35-
}
36-
return rtnByte;
34+
is.skip(startPos);
35+
36+
int downloadLengh = endPos - startPos;
37+
38+
byte[] b = new byte[1024];
39+
int total = 0;
40+
int len = -1;
41+
while ((len = is.read(b)) != -1) {
42+
baos.write(b, 0, len);
43+
total = total + len;
44+
if (total == downloadLengh) {
45+
break;
46+
}
47+
}
48+
is.close();
49+
baos.close();
50+
System.out.println("End Reading");
51+
return baos.toByteArray();
3752
}
3853

3954
@Override

0 commit comments

Comments
 (0)