-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebWorker.java
55 lines (46 loc) · 1.29 KB
/
WebWorker.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
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class WebWorker extends Thread {
/*
This is the core web/download i/o code...
download() {
InputStream input = null;
StringBuilder contents = null;
try {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
// Set connect() to throw an IOException
// if connection does not succeed in this many msecs.
connection.setConnectTimeout(5000);
connection.connect();
input = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
char[] array = new char[1000];
int len;
contents = new StringBuilder(1000);
while ((len = reader.read(array, 0, array.length)) > 0) {
contents.append(array, 0, len);
Thread.sleep(100);
}
// Successful download if we get here
}
// Otherwise control jumps to a catch...
catch(MalformedURLException ignored) {}
catch(InterruptedException exception) {
// YOUR CODE HERE
// deal with interruption
}
catch(IOException ignored) {}
// "finally" clause, to close the input stream
// in any case
finally {
try{
if (input != null) input.close();
}
catch(IOException ignored) {}
}
*/
}