34218
|
1 |
/* Title: Pure/General/download.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Download URLs -- with progress monitor.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
import java.io.{BufferedInputStream, BufferedOutputStream, FileOutputStream, File}
|
|
11 |
import java.net.{URL, URLConnection}
|
|
12 |
import java.awt.{Component, HeadlessException}
|
|
13 |
import javax.swing.ProgressMonitorInputStream
|
|
14 |
|
|
15 |
|
|
16 |
object Download
|
|
17 |
{
|
|
18 |
def stream(parent: Component, url: URL): (URLConnection, BufferedInputStream) =
|
|
19 |
{
|
|
20 |
val connection = url.openConnection
|
|
21 |
|
|
22 |
val stream = new ProgressMonitorInputStream(null, "Downloading", connection.getInputStream)
|
|
23 |
val monitor = stream.getProgressMonitor
|
|
24 |
monitor.setNote(connection.getURL.toString)
|
|
25 |
|
|
26 |
val length = connection.getContentLength
|
|
27 |
if (length != -1) monitor.setMaximum(length)
|
|
28 |
|
|
29 |
(connection, new BufferedInputStream(stream))
|
|
30 |
}
|
|
31 |
|
34219
|
32 |
// FIXME error handling (dialogs)
|
|
33 |
def file(parent: Component, url: URL, file: File): Boolean =
|
34218
|
34 |
{
|
|
35 |
val outstream = new BufferedOutputStream(new FileOutputStream(file))
|
|
36 |
|
|
37 |
val (connection, instream) = stream(parent, url)
|
|
38 |
val mod_time = connection.getLastModified
|
|
39 |
|
|
40 |
var c: Int = 0
|
|
41 |
while ({ c = instream.read; c != -1}) outstream.write(c)
|
|
42 |
|
|
43 |
instream.close
|
|
44 |
outstream.close
|
|
45 |
file.setLastModified(mod_time)
|
|
46 |
}
|
|
47 |
}
|
|
48 |
|