Child: [r25] (diff)

Download this file

DownloadBigFile.java    187 lines (149 with data), 6.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* This class provides support for download huge files (over 2Gb) down to disk.
* It should be able of supporting resume and verification that the end result
* matches a specific signature (md5 or simple similar).
*
* This class is mostly intended to assist in the effort of downloading a large
* ISO file from the Internet onto the local disk.
*
* The downloaded parts are immediately flushed to the disk. This should
* hopefully avoid issues with memory usage.
*/
package script;
import main.start;
import definitions.is;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import main.core;
import script.log;
/**
*
* @author Nuno Brito, 20th of January 2013 in Darmstadt, Germany
*/
public class DownloadBigFile {
// the private variable for this method
private String
sourceURL,
contentType,
title = "download" // the identifier for this download entry
;
private File
file;
private Boolean
isDownloading = false;
private BigInteger
expectedSize;
private long
lastPercent = 0;
public DownloadBigFile(String sourceURL, String targetFile) {
this.file = new File(targetFile);
this.sourceURL = sourceURL;
}
public DownloadBigFile(String sourceURL, File targetFile) {
this.file = targetFile;
this.sourceURL = sourceURL;
}
/**
* Download the file to disk.
*/
public void getFile(){
isDownloading = true;
boolean fileAlreadyExists = this.file.exists();
// if the file already exists and has the size that we expect
if(fileAlreadyExists)
// && (file.length() == this.expectedSize.longValue()))
{
//System.out.println("File already exists with the right size");
return;
}
try{
RandomAccessFile out = new RandomAccessFile(file, "rw");
// go back to the previous position if we can
out.seek(file.length());
URL url = new URL(sourceURL);
URLConnection urlCon = url.openConnection();
InputStream fileInputStream = urlCon.getInputStream();
// skip the first bytes of the file that were already downloaded
fileInputStream.skip(file.length());
// HttpIInStream fileInputStream = new HttpIInStream(sourceURL);
// fileInputStream.seek(file.length(), 0);
byte[] buf=new byte[8192];
int bytesread, bytesBuffered = 0;
long bytesTotal = 0;
while( (bytesread = fileInputStream.read( buf )) > -1 ) {
out.write( buf, 0, bytesread );
bytesBuffered += bytesread;
bytesTotal += bytesread;
if (bytesBuffered > 2024 * 1024) { //flush after each 2 megabytes
//out.flush();
bytesBuffered = 0;
// use the temporary holder to write the status of this thing
core.temp.put(sourceURL, bytesTotal);
// calculate the percentage since last count
// long currentPercent =
// ( file.length() * 100) / expectedSize.longValue();
//
// long dif = currentPercent - lastPercent;
// show status at a specific difference of percentage
//TODO missing to add a scale factor between a short download
// and a big download, where we adjust message frequency
// if(dif > 0){
// String readableSize = utils.files.humanReadableSize( file.length());
// String fullSize = utils.files.humanReadableSize(expectedSize.longValue());
//
// log.write(is.INFO, "%1 -> %2 (%3% complete)"
// ,title
// ,readableSize + " / " + fullSize
// ,"" + currentPercent);
// }
// // all done, update the counter
// lastPercent = currentPercent;
}
}
}catch (IOException e){
log.write(is.ERROR, "Error occurred while downloading file: %1",
e.getLocalizedMessage());
}
// all done, finish here
String fullSize = utils.files.humanReadableSize(file.length());
log.write(is.INFO, "Download complete, %1"
,fullSize);
isDownloading = false;
}
public String getContentType() {
return contentType;
}
public String getSourceURL() {
return sourceURL;
}
public File getTargetFile() {
return file;
}
public Boolean getIsDownloading() {
return isDownloading;
}
public BigInteger getExpectedSize() {
return expectedSize;
}
public void setExpectedSize(BigInteger expectedSize) {
this.expectedSize = expectedSize;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// create a new class, use two values as example and test the result
String targetFile = "last.exe";
// String sourceURL = "http://msft-dnl.digitalrivercontent.net/msvista/pub/X15-65804/X15-65804.iso";
String sourceURL = "http://appldnld.apple.com.edgesuite.net/content.info.apple.com/QuickTime/061-5376.20090121.BD7E9/QuickTimeInstaller.exe";
DownloadBigFile download = new DownloadBigFile(sourceURL, targetFile);
download.setExpectedSize(new BigInteger("21878064"));
download.getFile();
//System.out.println(download.getTargetFile().length());
}
}