src/Pure/System/download.scala
author blanchet
Tue, 20 Mar 2012 10:06:35 +0100
changeset 47039 1b36a05a070d
parent 45673 cd41e3903fbf
permissions -rw-r--r--
added "metis_advisory_simp" option to orient as many equations as possible in Metis the right way (cf. "More SPASS with Isabelle")
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
45673
cd41e3903fbf separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents: 45667
diff changeset
     2
    Module:     PIDE
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     4
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     5
Download URLs -- with progress monitor.
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     6
*/
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     7
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     8
package isabelle
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
     9
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    10
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    11
import java.io.{BufferedInputStream, BufferedOutputStream, FileOutputStream,
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    12
  File, InterruptedIOException}
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    13
import java.net.{URL, URLConnection}
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    14
import java.awt.{Component, HeadlessException}
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    15
import javax.swing.ProgressMonitorInputStream
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    16
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    17
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    18
object Download
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    19
{
39703
545cc67324d8 tuned signatures and messages;
wenzelm
parents: 36676
diff changeset
    20
  def stream(parent: Component, title: String, url: URL): (URLConnection, BufferedInputStream) =
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    21
  {
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    22
    val connection = url.openConnection
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    23
39703
545cc67324d8 tuned signatures and messages;
wenzelm
parents: 36676
diff changeset
    24
    val stream = new ProgressMonitorInputStream(parent, title, connection.getInputStream)
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    25
    val monitor = stream.getProgressMonitor
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    26
    monitor.setNote(connection.getURL.toString)
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    27
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    28
    val length = connection.getContentLength
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    29
    if (length != -1) monitor.setMaximum(length)
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    30
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    31
    (connection, new BufferedInputStream(stream))
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    32
  }
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    33
39703
545cc67324d8 tuned signatures and messages;
wenzelm
parents: 36676
diff changeset
    34
  def file(parent: Component, title: String, url: URL, file: File)
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    35
  {
39703
545cc67324d8 tuned signatures and messages;
wenzelm
parents: 36676
diff changeset
    36
    val (connection, instream) = stream(parent, title, url)
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    37
    val mod_time = connection.getLastModified
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    38
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    39
    def read() =
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    40
      try { instream.read }
39703
545cc67324d8 tuned signatures and messages;
wenzelm
parents: 36676
diff changeset
    41
      catch { case _ : InterruptedIOException => error("Canceled by user") }
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    42
    try {
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    43
      val outstream = new BufferedOutputStream(new FileOutputStream(file))
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    44
      try {
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    45
        var c: Int = 0
39730
e4e1e3b69cba tuned whitespace;
wenzelm
parents: 39703
diff changeset
    46
        while ({ c = read(); c != -1 }) outstream.write(c)
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    47
      }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    48
      finally { outstream.close }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    49
      if (mod_time > 0) file.setLastModified(mod_time)
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    50
    }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    51
    finally { instream.close }
34218
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    52
  }
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    53
}
f65c717952c0 Download URLs -- with progress monitor.
wenzelm
parents:
diff changeset
    54