Chnage in download Artifacts method

revert-dabc3590
Nirothipan 8 years ago
parent 3fec4edf90
commit 6aba44e6c7

@ -30,12 +30,14 @@ import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Scanner; import java.util.Scanner;
@ -113,52 +115,72 @@ public class TryIt {
System.out.println("\nGood Bye!"); System.out.println("\nGood Bye!");
} }
/**
* This method is called when then is an error in getting system properties
*
* @param error - system property name
*/
private void sysPropertyError(String error) {
System.out.println("Unable to get the " + error + " of your system");
System.exit(1);
}
/** /**
* This method downloads the files. * This method downloads the artifacts from remote url.
* *
* @param path - the URL to download from. * @param remotePath - remote url
* @param folderName - the folder location to download the files to. * @param localPath - local path to download
*/ */
private void downloadArtifacts(String path, String folderName) { private void downloadArtifacts(String remotePath, String localPath) {
ReadableByteChannel readableByteChannel = null; BufferedInputStream in = null;
FileOutputStream fileOutputStream = null; FileOutputStream out = null;
long startingTime = System.currentTimeMillis();
try { try {
URL url = new URL(path); URL url = new URL(remotePath);
readableByteChannel = Channels.newChannel(url.openStream()); URLConnection conn = url.openConnection();
fileOutputStream = new FileOutputStream(folderName); int size = conn.getContentLength();
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); in = new BufferedInputStream(url.openStream());
out = new FileOutputStream(localPath);
byte data[] = new byte[1024];
int count;
double sumCount = 0.0;
while ((count = in.read(data, 0, 1024)) != -1) {
out.write(data, 0, count);
sumCount += count;
if ((size > 0 && (System.currentTimeMillis() - startingTime > 5000))
|| (sumCount / size * 100.0) == 100) {
System.out.println("Downloading: "
+ new DecimalFormat("#.##").format((sumCount / size * 100.0)) + " %");
startingTime = System.currentTimeMillis();
}
}
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
System.out.println("Error in download URL of " + folderName); System.out.println("Error in download URL of " + localPath);
System.out.println("URL provided " + path); System.out.println("URL provided " + remotePath);
} catch (IOException e) { } catch (IOException e) {
if (!new File(folderName).delete()) { if (!new File(localPath).delete()) {
System.out.println("Delete " + folderName + " and try again"); System.out.println("Delete " + localPath + " and try again");
} }
handleException("Downloading " + folderName + " failed.", e); handleException("Downloading " + localPath + " failed.", e);
} finally { } finally {
try { if (in != null)
if (fileOutputStream != null) { try {
fileOutputStream.close(); in.close();
} catch (IOException ignored) {
//
} }
if (readableByteChannel != null) { if (out != null)
readableByteChannel.close(); try {
out.close();
} catch (IOException ignored) {
//
} }
} catch (IOException ignored) {
// Exception in finally block
}
} }
} }
/**
* This method is called when then is an error in getting system properties
*
* @param error - system property name
*/
private void sysPropertyError(String error) {
System.out.println("Unable to get the " + error + " of your system");
System.exit(1);
}
/** /**
* This method validates the Android SDK location provided by the user and write it to the file * This method validates the Android SDK location provided by the user and write it to the file
* sdkConfigFile. * sdkConfigFile.

Loading…
Cancel
Save