Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Setting connection timeout and read timeout on file/json download. Th…
Browse files Browse the repository at this point in the history
…anks to @kenvunz.

#107
  • Loading branch information
nikDemyankov committed Mar 7, 2016
1 parent c8e7e82 commit 093dd9c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
29 changes: 19 additions & 10 deletions src/android/src/com/nordnetab/chcp/main/network/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
public class FileDownloader {

// connection timeout in milliseconds
private static final int CONNECTION_TIMEOUT = 30000;

// data read timeout in milliseconds
private static final int READ_TIMEOUT = 30000;

/**
* Download list of files.
* Full url to the file is constructed from the contentFolderUrl and ManifestFile#hash (relative path).
Expand Down Expand Up @@ -55,28 +61,31 @@ public static void downloadFiles(final String downloadFolder, final String conte
* @param checkSum checksum of the file
* @throws IOException
*/
public static void download(String urlFrom, String filePath, String checkSum) throws IOException {
public static void download(final String urlFrom, final String filePath, final String checkSum) throws IOException {
Log.d("CHCP", "Loading file: " + urlFrom);

File downloadFile = new File(filePath);
final File downloadFile = new File(filePath);
FilesUtility.delete(downloadFile);
FilesUtility.ensureDirectoryExists(downloadFile.getParentFile());

MD5 md5 = new MD5();
final MD5 md5 = new MD5();

URL downloadUrl = URLUtility.stringToUrl(urlFrom);
final URL downloadUrl = URLUtility.stringToUrl(urlFrom);
if (downloadUrl == null) {
throw new IOException("Invalid url format");
}

URLConnection connection = downloadUrl.openConnection();
connection.setConnectTimeout(60000);
// create connection
final URLConnection connection = downloadUrl.openConnection();
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.setReadTimeout(READ_TIMEOUT);
connection.connect();

InputStream input = new BufferedInputStream(downloadUrl.openStream());
OutputStream output = new BufferedOutputStream(new FileOutputStream(filePath, false));
// create streams
final InputStream input = new BufferedInputStream(downloadUrl.openStream());
final OutputStream output = new BufferedOutputStream(new FileOutputStream(filePath, false));

byte data[] = new byte[1024];
final byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
Expand All @@ -87,7 +96,7 @@ public static void download(String urlFrom, String filePath, String checkSum) th
output.close();
input.close();

String downloadedFileHash = md5.calculateHash();
final String downloadedFileHash = md5.calculateHash();
if (!downloadedFileHash.equals(checkSum)) {
throw new IOException("File is corrupted: checksum " + checkSum + " doesn't match hash " + downloadedFileHash + " of the downloaded file");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ abstract class JsonDownloader<T> {

private String downloadUrl;

// connection timeout in milliseconds
private static final int CONNECTION_TIMEOUT = 30000;

// data read timeout in milliseconds
private static final int READ_TIMEOUT = 30000;

/**
* Create instance of the object from json string.
*
Expand Down Expand Up @@ -60,18 +66,21 @@ public DownloadResult<T> download() {
}

private String downloadJson() throws Exception {
StringBuilder jsonContent = new StringBuilder();
final StringBuilder jsonContent = new StringBuilder();

URL url = URLUtility.stringToUrl(downloadUrl);
final URL url = URLUtility.stringToUrl(downloadUrl);
if (url == null) {
throw new Exception("Invalid url format:" + downloadUrl);
}

URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(60000);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
final URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
urlConnection.setReadTimeout(READ_TIMEOUT);

final InputStreamReader streamReader = new InputStreamReader(urlConnection.getInputStream());
final BufferedReader bufferedReader = new BufferedReader(streamReader);

char data[] = new char[1024];
final char data[] = new char[1024];
int count;
while ((count = bufferedReader.read(data)) != -1) {
jsonContent.append(data, 0, count);
Expand Down

0 comments on commit 093dd9c

Please sign in to comment.