author | wenzelm |
Sat, 05 May 2018 21:44:18 +0200 | |
changeset 68087 | dac267cd51fe |
parent 68018 | 3747fe57eb67 |
child 68094 | 0b66aca9c965 |
permissions | -rw-r--r-- |
54439 | 1 |
/* Title: Pure/General/bytes.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Immutable byte vectors versus UTF8 strings. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
64004 | 10 |
import java.io.{File => JFile, ByteArrayOutputStream, ByteArrayInputStream, |
64229 | 11 |
OutputStream, InputStream, FileInputStream, FileOutputStream} |
65070 | 12 |
import java.net.URL |
64004 | 13 |
|
14 |
import org.tukaani.xz.{XZInputStream, XZOutputStream} |
|
54440 | 15 |
|
16 |
||
54439 | 17 |
object Bytes |
18 |
{ |
|
19 |
val empty: Bytes = new Bytes(Array[Byte](), 0, 0) |
|
20 |
||
21 |
def apply(s: CharSequence): Bytes = |
|
22 |
{ |
|
23 |
val str = s.toString |
|
24 |
if (str.isEmpty) empty |
|
25 |
else { |
|
62527 | 26 |
val b = UTF8.bytes(str) |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
27 |
new Bytes(b, 0, b.length) |
54439 | 28 |
} |
29 |
} |
|
54440 | 30 |
|
63779 | 31 |
def apply(a: Array[Byte]): Bytes = apply(a, 0, a.length) |
32 |
||
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
33 |
def apply(a: Array[Byte], offset: Int, length: Int): Bytes = |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
34 |
if (length == 0) empty |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
35 |
else { |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
36 |
val b = new Array[Byte](length) |
55618 | 37 |
System.arraycopy(a, offset, b, 0, length) |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
38 |
new Bytes(b, 0, b.length) |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
39 |
} |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
40 |
|
54440 | 41 |
|
68087 | 42 |
def hex(s: String): Bytes = |
43 |
{ |
|
44 |
def err(): Nothing = error("Malformed hexadecimal representation of bytes\n" + s) |
|
45 |
val len = s.length |
|
46 |
if (len % 2 != 0) err() |
|
47 |
||
48 |
val n = len / 2 |
|
49 |
val a = new Array[Byte](n) |
|
50 |
for (i <- 0 until n) { |
|
51 |
val j = 2 * i |
|
52 |
try { a(i) = Integer.parseInt(s.substring(j, j + 2), 16).toByte } |
|
53 |
catch { case _: NumberFormatException => err() } |
|
54 |
} |
|
55 |
new Bytes(a, 0, n) |
|
56 |
} |
|
57 |
||
58 |
||
54440 | 59 |
/* read */ |
60 |
||
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
61 |
def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE, hint: Int = 1024): Bytes = |
64004 | 62 |
if (limit == 0) empty |
63 |
else { |
|
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
64 |
val out = new ByteArrayOutputStream(if (limit == Integer.MAX_VALUE) hint else limit) |
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
65 |
val buf = new Array[Byte](8192) |
64004 | 66 |
var m = 0 |
54440 | 67 |
|
68 |
do { |
|
64004 | 69 |
m = stream.read(buf, 0, buf.size min (limit - out.size)) |
70 |
if (m != -1) out.write(buf, 0, m) |
|
71 |
} while (m != -1 && limit > out.size) |
|
72 |
||
73 |
new Bytes(out.toByteArray, 0, out.size) |
|
54440 | 74 |
} |
64001
7ecb22be8f03
more general read_stream: return actual byte count;
wenzelm
parents:
63779
diff
changeset
|
75 |
|
67805
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
76 |
def read_block(stream: InputStream, length: Int): Option[Bytes] = |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
77 |
{ |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
78 |
val bytes = read_stream(stream, limit = length) |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
79 |
if (bytes.length == length) Some(bytes) else None |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
80 |
} |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
81 |
|
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
82 |
def read_line(stream: InputStream): Option[Bytes] = |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
83 |
{ |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
84 |
val out = new ByteArrayOutputStream(100) |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
85 |
var c = 0 |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
86 |
while ({ c = stream.read; c != -1 && c != 10 }) out.write(c) |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
87 |
|
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
88 |
if (c == -1 && out.size == 0) None |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
89 |
else { |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
90 |
val a = out.toByteArray |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
91 |
val n = a.length |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
92 |
val b = if (n > 0 && a(n - 1) == 13) a.take(n - 1) else a |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
93 |
Some(new Bytes(b, 0, b.length)) |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
94 |
} |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
95 |
} |
2d9a265b294e
more uniform Bytes.read_line/read_block operations;
wenzelm
parents:
65630
diff
changeset
|
96 |
|
64001
7ecb22be8f03
more general read_stream: return actual byte count;
wenzelm
parents:
63779
diff
changeset
|
97 |
def read(file: JFile): Bytes = |
64004 | 98 |
using(new FileInputStream(file))(read_stream(_, file.length.toInt)) |
64229 | 99 |
|
100 |
def read(path: Path): Bytes = read(path.file) |
|
101 |
||
65070 | 102 |
def read(url: URL): Bytes = using(url.openStream)(read_stream(_)) |
103 |
||
64229 | 104 |
|
105 |
/* write */ |
|
106 |
||
107 |
def write(file: JFile, bytes: Bytes) |
|
108 |
{ |
|
109 |
val stream = new FileOutputStream(file) |
|
110 |
try { bytes.write_stream(stream) } finally { stream.close } |
|
111 |
} |
|
112 |
||
113 |
def write(path: Path, bytes: Bytes): Unit = write(path.file, bytes) |
|
54439 | 114 |
} |
115 |
||
116 |
final class Bytes private( |
|
117 |
protected val bytes: Array[Byte], |
|
118 |
protected val offset: Int, |
|
60833 | 119 |
val length: Int) extends CharSequence |
54439 | 120 |
{ |
121 |
/* equality */ |
|
122 |
||
54440 | 123 |
override def equals(that: Any): Boolean = |
124 |
{ |
|
125 |
that match { |
|
126 |
case other: Bytes => |
|
127 |
if (this eq other) true |
|
128 |
else if (length != other.length) false |
|
129 |
else (0 until length).forall(i => bytes(offset + i) == other.bytes(other.offset + i)) |
|
130 |
case _ => false |
|
131 |
} |
|
132 |
} |
|
133 |
||
54439 | 134 |
private lazy val hash: Int = |
135 |
{ |
|
136 |
var h = 0 |
|
137 |
for (i <- offset until offset + length) { |
|
138 |
val b = bytes(i).asInstanceOf[Int] & 0xFF |
|
139 |
h = 31 * h + b |
|
140 |
} |
|
141 |
h |
|
142 |
} |
|
143 |
||
144 |
override def hashCode(): Int = hash |
|
145 |
||
146 |
||
147 |
/* content */ |
|
148 |
||
54512 | 149 |
lazy val sha1_digest: SHA1.Digest = SHA1.digest(bytes) |
54440 | 150 |
|
65279
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
151 |
def text: String = |
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
152 |
UTF8.decode_chars(s => s, bytes, offset, offset + length).toString |
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
153 |
|
54444 | 154 |
override def toString: String = |
64224 | 155 |
{ |
65279
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
156 |
val str = text |
64224 | 157 |
if (str.contains('\uFFFD')) "Bytes(" + length + ")" else str |
158 |
} |
|
54439 | 159 |
|
54440 | 160 |
def isEmpty: Boolean = length == 0 |
54439 | 161 |
|
65630 | 162 |
def proper: Option[Bytes] = if (isEmpty) None else Some(this) |
163 |
def proper_text: Option[String] = if (isEmpty) None else Some(text) |
|
164 |
||
54439 | 165 |
def +(other: Bytes): Bytes = |
54440 | 166 |
if (other.isEmpty) this |
167 |
else if (isEmpty) other |
|
54439 | 168 |
else { |
169 |
val new_bytes = new Array[Byte](length + other.length) |
|
55618 | 170 |
System.arraycopy(bytes, offset, new_bytes, 0, length) |
171 |
System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length) |
|
54439 | 172 |
new Bytes(new_bytes, 0, new_bytes.length) |
173 |
} |
|
54440 | 174 |
|
175 |
||
60833 | 176 |
/* CharSequence operations */ |
177 |
||
178 |
def charAt(i: Int): Char = |
|
179 |
if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char] |
|
180 |
else throw new IndexOutOfBoundsException |
|
181 |
||
182 |
def subSequence(i: Int, j: Int): Bytes = |
|
183 |
{ |
|
184 |
if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i) |
|
185 |
else throw new IndexOutOfBoundsException |
|
186 |
} |
|
187 |
||
188 |
||
64004 | 189 |
/* streams */ |
190 |
||
191 |
def stream(): ByteArrayInputStream = new ByteArrayInputStream(bytes, offset, length) |
|
192 |
||
193 |
def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
|
194 |
||
195 |
||
196 |
/* XZ data compression */ |
|
54440 | 197 |
|
68018 | 198 |
def uncompress(cache: XZ.Cache = XZ.cache()): Bytes = |
199 |
using(new XZInputStream(stream(), cache))(Bytes.read_stream(_, hint = length)) |
|
64004 | 200 |
|
68018 | 201 |
def compress(options: XZ.Options = XZ.options(), cache: XZ.Cache = XZ.cache()): Bytes = |
64004 | 202 |
{ |
203 |
val result = new ByteArrayOutputStream(length) |
|
68018 | 204 |
using(new XZOutputStream(result, options, cache))(write_stream(_)) |
64004 | 205 |
new Bytes(result.toByteArray, 0, result.size) |
206 |
} |
|
54439 | 207 |
} |