76991
|
1 |
/* Title: Pure/ML/ml_heap.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
ML heap operations.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
import java.nio.ByteBuffer
|
|
11 |
import java.nio.channels.FileChannel
|
|
12 |
import java.nio.file.StandardOpenOption
|
|
13 |
|
|
14 |
|
|
15 |
object ML_Heap {
|
|
16 |
/** heap file with SHA1 digest **/
|
|
17 |
|
|
18 |
private val sha1_prefix = "SHA1:"
|
|
19 |
|
77206
|
20 |
def read_digest(heap: Path): Option[SHA1.Digest] = {
|
76991
|
21 |
if (heap.is_file) {
|
|
22 |
using(FileChannel.open(heap.java_path, StandardOpenOption.READ)) { file =>
|
|
23 |
val len = file.size
|
|
24 |
val n = sha1_prefix.length + SHA1.digest_length
|
|
25 |
if (len >= n) {
|
|
26 |
file.position(len - n)
|
|
27 |
|
|
28 |
val buf = ByteBuffer.allocate(n)
|
|
29 |
var i = 0
|
|
30 |
var m = 0
|
|
31 |
while ({
|
|
32 |
m = file.read(buf)
|
|
33 |
if (m != -1) i += m
|
|
34 |
m != -1 && n > i
|
|
35 |
}) ()
|
|
36 |
|
|
37 |
if (i == n) {
|
|
38 |
val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset)
|
|
39 |
val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset)
|
77206
|
40 |
if (prefix == sha1_prefix) Some(SHA1.fake_digest(s)) else None
|
76991
|
41 |
}
|
|
42 |
else None
|
|
43 |
}
|
|
44 |
else None
|
|
45 |
}
|
|
46 |
}
|
|
47 |
else None
|
|
48 |
}
|
|
49 |
|
77206
|
50 |
def write_digest(heap: Path): SHA1.Digest =
|
76992
|
51 |
read_digest(heap) getOrElse {
|
77206
|
52 |
val digest = SHA1.digest(heap)
|
|
53 |
File.append(heap, sha1_prefix + digest.toString)
|
|
54 |
digest
|
76991
|
55 |
}
|
|
56 |
}
|