forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
93 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
public class ConnectionException extends Exception { | ||
|
||
} | ||
} |
9 changes: 2 additions & 7 deletions
9
group20/404130810/src/com/coderising/download/api/ConnectionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
package com.coderising.download.api; | ||
|
||
public interface ConnectionManager { | ||
/** | ||
* ¸ø¶¨Ò»¸öurl , ´ò¿ªÒ»¸öÁ¬½Ó | ||
* @param url | ||
* @return | ||
*/ | ||
public Connection open(String url) throws ConnectionException; | ||
} | ||
public Connection open(String url) throws ConnectionException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
public interface DownloadListener { | ||
public void notifyFinished(); | ||
} | ||
} |
36 changes: 27 additions & 9 deletions
36
group20/404130810/src/com/coderising/download/impl/ConnectionImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,49 @@ | ||
package com.coderising.download.impl; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.RandomAccessFile; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
import com.coderising.download.api.Connection; | ||
|
||
public class ConnectionImpl implements Connection{ | ||
public class ConnectionImpl implements Connection { | ||
|
||
private byte[] fileContent; | ||
private HttpURLConnection httpConn; | ||
|
||
public ConnectionImpl(String urlStr) { | ||
URL url; | ||
try { | ||
url = new URL(urlStr); | ||
httpConn = (HttpURLConnection) url.openConnection(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] read(int startPos, int endPos) throws IOException { | ||
int length = endPos - startPos; | ||
byte[] rtnByte = new byte[length]; | ||
|
||
return null; | ||
RandomAccessFile raf = new RandomAccessFile("test.mp3", "rw"); | ||
raf.seek(startPos); | ||
InputStream is = httpConn.getInputStream(); | ||
while ((length = is.read(rtnByte)) != -1) { | ||
raf.write(rtnByte, 0, length); | ||
} | ||
return rtnByte; | ||
} | ||
|
||
@Override | ||
public int getContentLength() { | ||
return fileContent.length; | ||
return httpConn.getContentLength(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
} | ||
|
||
public void setFileContent(byte[] fileContent) { | ||
this.fileContent = fileContent; | ||
httpConn.disconnect(); | ||
} | ||
|
||
} |
19 changes: 2 additions & 17 deletions
19
group20/404130810/src/com/coderising/download/impl/ConnectionManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,13 @@ | ||
package com.coderising.download.impl; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import com.coderising.download.api.Connection; | ||
import com.coderising.download.api.ConnectionException; | ||
import com.coderising.download.api.ConnectionManager; | ||
|
||
public class ConnectionManagerImpl implements ConnectionManager { | ||
|
||
|
||
@Override | ||
public Connection open(String urlStr) throws ConnectionException { | ||
URL url; | ||
try { | ||
url = new URL(urlStr); | ||
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return null; | ||
Connection conn = new ConnectionImpl(urlStr); | ||
return conn; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
group20/404130810/src/com/coderising/download/utils/FileDownloadUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.coderising.download.utils; | ||
|
||
public class FileDownloadUtil { | ||
|
||
public static int[] generateDownloadPosArr(int length){ | ||
int[] posArr = new int[3]; | ||
int firstPos = length/3; | ||
int secondPos = length/3 * 2; | ||
|
||
posArr[0] = 0; | ||
posArr[1] = firstPos; | ||
posArr[2] = secondPos; | ||
|
||
return posArr; | ||
} | ||
public static void main(String[] args) { | ||
FileDownloadUtil.generateDownloadPosArr(1000); | ||
} | ||
|
||
} |