69448
|
1 |
/* Title: Pure/General/byte_message.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Byte-oriented messages.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
import java.io.{ByteArrayOutputStream, OutputStream, InputStream, IOException}
|
|
10 |
|
|
11 |
|
|
12 |
object Byte_Message
|
|
13 |
{
|
69451
|
14 |
/* output operations */
|
|
15 |
|
|
16 |
def write(stream: OutputStream, bytes: Bytes) { bytes.write_stream(stream) }
|
|
17 |
|
|
18 |
def newline(stream: OutputStream) { stream.write(10) }
|
|
19 |
|
|
20 |
def flush(stream: OutputStream): Unit =
|
|
21 |
try { stream.flush() }
|
|
22 |
catch { case _: IOException => }
|
|
23 |
|
|
24 |
|
|
25 |
/* input operations */
|
|
26 |
|
69452
|
27 |
def read(stream: InputStream, n: Int): Bytes =
|
|
28 |
Bytes.read_stream(stream, limit = n)
|
69449
|
29 |
|
69452
|
30 |
def read_block(stream: InputStream, n: Int): (Option[Bytes], Int) =
|
69449
|
31 |
{
|
69452
|
32 |
val msg = read(stream, n)
|
|
33 |
val len = msg.length
|
|
34 |
(if (len == n) Some(msg) else None, len)
|
69449
|
35 |
}
|
|
36 |
|
69448
|
37 |
def read_line(stream: InputStream): Option[Bytes] =
|
|
38 |
{
|
|
39 |
val line = new ByteArrayOutputStream(100)
|
|
40 |
var c = 0
|
|
41 |
while ({ c = stream.read; c != -1 && c != 10 }) line.write(c)
|
|
42 |
|
|
43 |
if (c == -1 && line.size == 0) None
|
|
44 |
else {
|
|
45 |
val a = line.toByteArray
|
|
46 |
val n = a.length
|
|
47 |
val len = if (n > 0 && a(n - 1) == 13) n - 1 else n
|
|
48 |
Some(Bytes(a, 0, len))
|
|
49 |
}
|
|
50 |
}
|
|
51 |
|
|
52 |
|
69451
|
53 |
/* header with chunk lengths */
|
|
54 |
|
69452
|
55 |
def write_header(stream: OutputStream, ns: List[Int])
|
|
56 |
{
|
|
57 |
stream.write(UTF8.bytes(ns.mkString(",")))
|
|
58 |
newline(stream)
|
|
59 |
}
|
|
60 |
|
69451
|
61 |
private def err_header(line: String): Nothing =
|
|
62 |
error("Malformed message header: " + quote(line))
|
|
63 |
|
|
64 |
private def parse_header(line: String): List[Int] =
|
69452
|
65 |
try { space_explode(',', line).map(Value.Nat.parse) }
|
69451
|
66 |
catch { case ERROR(_) => err_header(line) }
|
|
67 |
|
|
68 |
def read_header(stream: InputStream): Option[List[Int]] =
|
|
69 |
read_line(stream).map(_.text).map(parse_header(_))
|
|
70 |
|
|
71 |
def read_header1(stream: InputStream): Option[Int] =
|
|
72 |
read_line(stream).map(_.text).map(line =>
|
|
73 |
parse_header(line) match {
|
|
74 |
case List(n) => n
|
|
75 |
case _ => err_header(line)
|
|
76 |
})
|
|
77 |
|
|
78 |
|
|
79 |
/* messages with multiple chunks (arbitrary content) */
|
|
80 |
|
|
81 |
def write_message(stream: OutputStream, chunks: List[Bytes])
|
|
82 |
{
|
|
83 |
write_header(stream, chunks.map(_.length))
|
|
84 |
chunks.foreach(write(stream, _))
|
|
85 |
flush(stream)
|
|
86 |
}
|
|
87 |
|
|
88 |
private def read_chunk(stream: InputStream, n: Int): Bytes =
|
69452
|
89 |
read_block(stream, n) match {
|
|
90 |
case (Some(chunk), _) => chunk
|
|
91 |
case (None, len) =>
|
|
92 |
error("Malformed message chunk: unexpected EOF after " + len + " of " + n + " bytes")
|
|
93 |
}
|
69451
|
94 |
|
|
95 |
def read_message(stream: InputStream): Option[List[Bytes]] =
|
|
96 |
read_header(stream).map(ns => ns.map(n => read_chunk(stream, n)))
|
|
97 |
|
|
98 |
|
|
99 |
/* hybrid messages: line or length+block (restricted content) */
|
69448
|
100 |
|
|
101 |
private def is_length(msg: Bytes): Boolean =
|
|
102 |
!msg.is_empty && msg.iterator.forall(b => Symbol.is_ascii_digit(b.toChar))
|
|
103 |
|
69452
|
104 |
private def is_terminated(msg: Bytes): Boolean =
|
69448
|
105 |
{
|
|
106 |
val len = msg.length
|
|
107 |
len > 0 && Symbol.is_ascii_line_terminator(msg.charAt(len - 1))
|
|
108 |
}
|
|
109 |
|
|
110 |
def write_line_message(stream: OutputStream, msg: Bytes)
|
|
111 |
{
|
69452
|
112 |
if (is_length(msg) || is_terminated(msg))
|
69448
|
113 |
error ("Bad content for line message:\n" ++ msg.text.take(100))
|
|
114 |
|
69452
|
115 |
val n = msg.length
|
|
116 |
if (n > 100 || msg.iterator.contains(10)) write_header(stream, List(n + 1))
|
|
117 |
|
69451
|
118 |
write(stream, msg)
|
|
119 |
newline(stream)
|
|
120 |
flush(stream)
|
69448
|
121 |
}
|
|
122 |
|
|
123 |
def read_line_message(stream: InputStream): Option[Bytes] =
|
69451
|
124 |
read_line(stream) match {
|
69452
|
125 |
case None => None
|
69451
|
126 |
case Some(line) =>
|
69452
|
127 |
Value.Nat.unapply(line.text) match {
|
|
128 |
case None => Some(line)
|
|
129 |
case Some(n) => read_block(stream, n)._1.map(_.trim_line)
|
|
130 |
}
|
69448
|
131 |
}
|
|
132 |
}
|