src/Pure/System/download.scala
author wenzelm
Thu, 20 May 2010 20:22:00 +0200
changeset 37013 641923374eba
parent 36676 ac7961d42ac3
child 39703 545cc67324d8
permissions -rw-r--r--
Isabelle_System: allow explicit isabelle_home argument;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36676
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 34220
diff changeset
     1
/*  Title:      Pure/System/download.scala
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     3
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     4
Download URLs -- with progress monitor.
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     5
*/
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     6
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     7
package isabelle
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     8
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     9
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    10
import java.io.{BufferedInputStream, BufferedOutputStream, FileOutputStream,
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    11
  File, InterruptedIOException}
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    12
import java.net.{URL, URLConnection}
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    13
import java.awt.{Component, HeadlessException}
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    14
import javax.swing.ProgressMonitorInputStream
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    15
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    16
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    17
object Download
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    18
{
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    19
  def stream(parent: Component, url: URL): (URLConnection, BufferedInputStream) =
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    20
  {
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    21
    val connection = url.openConnection
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    22
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    23
    val stream = new ProgressMonitorInputStream(null, "Downloading", connection.getInputStream)
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    24
    val monitor = stream.getProgressMonitor
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    25
    monitor.setNote(connection.getURL.toString)
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    26
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    27
    val length = connection.getContentLength
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    28
    if (length != -1) monitor.setMaximum(length)
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    29
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    30
    (connection, new BufferedInputStream(stream))
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    31
  }
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    32
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    33
  def file(parent: Component, url: URL, file: File)
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    34
  {
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    35
    val (connection, instream) = stream(parent, url)
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    36
    val mod_time = connection.getLastModified
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    37
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    38
    def read() =
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    39
      try { instream.read }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    40
      catch { case _ : InterruptedIOException => error("Download canceled!") }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    41
    try {
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    42
      val outstream = new BufferedOutputStream(new FileOutputStream(file))
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    43
      try {
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    44
        var c: Int = 0
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    45
        while ({ c = read(); c != -1}) outstream.write(c)
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    46
      }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    47
      finally { outstream.close }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    48
      if (mod_time > 0) file.setLastModified(mod_time)
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    49
    }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    50
    finally { instream.close }
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    51
  }
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    52
}
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    53