-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadableMetadata.java
268 lines (202 loc) · 6.98 KB
/
DownloadableMetadata.java
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
/**
* Describes a file's metadata: URL, file name, size, and which parts already downloaded to disk.
*
* The metadata (or at least which parts already downloaded to disk) is constantly stored safely in disk.
* When constructing a new metadata object, we first check the disk to load existing metadata.
*
* CHALLENGE: try to avoid metadata disk footprint of O(n) in the average case
* HINT: avoid the obvious bitmap solution, and think about ranges...
*/
class DownloadableMetadata {
private int content_length;
private String metadataFilename;
private String filename;
private String url;
private ArrayList<Range> rangeList;
public ArrayBlockingQueue<Range> rangeQueue;
private final int BYTE_CHUNK_SIZE = 4096;
private final int NUM_BYTE_CHUNK_RANGES = 100;
private final int numBytesPerRange;
private final int numRanges;
DownloadableMetadata(String url) throws IOException {
this.url = url;
this.filename = getName(url);
this.metadataFilename = getMetadataName(filename);
// content_length is the expected filesize
this.content_length = getContentLength(url);
this.numBytesPerRange = getBytesPerRange();
this.numRanges = calcNumRanges();
// ranges of filebytes
this.rangeList = null;
// a queue of file parts to download
this.rangeQueue = new ArrayBlockingQueue<Range>(numRanges);
}
/**
* This method splits the file into numRanges ranges and puts them into an ArrayList<Range>
* @return ranges
*/
private ArrayList<Range> initializeRanges() {
ArrayList<Range> ranges = new ArrayList<Range>(numRanges);
Range range = null;
// add the ranges into the array list
for(int i = 0; i < numRanges; i++) {
// in the first chunksPerRange-1 ranges, in each line we write:
// Start_Long - End Long , which describes a single missing range
if(i != numRanges - 1) {
range = new Range((long)i*(numBytesPerRange), (long)(i+1)*(numBytesPerRange) - 1);
ranges.add(range);
// the last range gets the rest of the file
} else {
range = new Range((long)i*(numBytesPerRange), (long)this.content_length);
ranges.add(range);
}
}
return ranges;
}
/**
* gets max number of chunks in a range.
* spread file out over 100 byte chunk ranges
* @return
*/
private int getBytesPerRange() {
int chunksPerRange = (this.content_length / BYTE_CHUNK_SIZE) / NUM_BYTE_CHUNK_RANGES;
// download file too small, one chunk per range
if(chunksPerRange < 1) {
chunksPerRange = 1;
}
return chunksPerRange * BYTE_CHUNK_SIZE;
}
private int calcNumRanges() {
// get the number of ranges for chunk sets of chunksPerRange chunks
if(numBytesPerRange > this.content_length) {
return 1;
} else {
return this.content_length / numBytesPerRange;
}
}
private String getMetadataName(String filename) {
return filename + ".metadata";
}
public String getMetadataFileName() {
return this.metadataFilename;
}
private String getName(String path) {
return path.substring(path.lastIndexOf('/') + 1, path.length());
}
/**
* Opens an HTTP connection and queries the connection for the content length
* of the file to be downloaded.
* @param url - the URL of the file to be downloaded
* @return length - int
* @throws IOException
*/
private static int getContentLength(String url) throws IOException {
// open the HTTP connection using the URL
URL url_url;
try {
url_url = new URL(url);
} catch (MalformedURLException e1) {
System.err.println("Please check the URL and try again.");
throw new IOException();
}
HttpURLConnection conn = (HttpURLConnection)url_url.openConnection();
try {
conn.connect();
} catch (IOException e) {
System.err.println("Couldn't connect to the internet. Please check your connection.");
throw new IOException();
}
// query the connection to find the file size
int length = conn.getContentLength();
conn.disconnect();
return length;
}
String getFilename() {
return this.filename;
}
void delete(String filename) {
File file = new File(filename);
file.delete();
}
String getUrl() {
return this.url;
}
/**
* This returns the size of the expected file (content length)
* NOT for the current size of the Data file
* For size of the data file, see method getFileSize()
* @return
*/
int getSize() {
return this.content_length;
}
/**
* write the missing ranges into a file given by filename
* @param filename - the file in which to write the missing ranges
* @throws IOException
*/
void writeMissingRanges(String filename) throws IOException {
// we write our rangeList to the metadata file
FileOutputStream fout = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(rangeList);
fout.close();
oos.close();
}
public boolean isEmptyRanges() {
return rangeQueue.isEmpty();
}
public Range getMissingRange() {
return this.rangeQueue.poll();
}
public int getNumRangesLeft() {
return this.rangeList.size();
}
public void removeFromRanges(Range range) {
this.rangeList.remove(range);
}
// we suppress the unchecked warning for this exercise since we are sure that
// the only object in our file is an ArrayList unless someone maliciously mishandled the file
@SuppressWarnings("unchecked")
public void openFile() throws IOException {
File metadata = new File(this.metadataFilename);
if(metadata.exists()) {
// load the metadata from the file into the object
FileInputStream fin = new FileInputStream(this.getMetadataFileName());
ObjectInputStream ois = new ObjectInputStream(fin);
try {
this.rangeList = (ArrayList<Range>)ois.readObject();
} catch (ClassNotFoundException e) {
System.err.println("Well, shit.");
}
Iterator<Range> iter = this.rangeList.iterator();
while(iter.hasNext()) {
this.rangeQueue.add(iter.next());
}
fin.close();
ois.close();
} else {
this.rangeList = initializeRanges();
writeMissingRanges(this.metadataFilename);
Iterator<Range> iter = this.rangeList.iterator();
while(iter.hasNext()) {
this.rangeQueue.add(iter.next());
}
}
}
public int numRanges() {
return this.numRanges;
}
}