author | wenzelm |
Thu, 16 Mar 2017 23:26:16 +0100 | |
changeset 65279 | fa62e095d8f1 |
parent 65070 | 1222c010bff7 |
child 65630 | c41bbf657310 |
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 |
|
42 |
/* read */ |
|
43 |
||
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
44 |
def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE, hint: Int = 1024): Bytes = |
64004 | 45 |
if (limit == 0) empty |
46 |
else { |
|
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
47 |
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
|
48 |
val buf = new Array[Byte](8192) |
64004 | 49 |
var m = 0 |
54440 | 50 |
|
51 |
do { |
|
64004 | 52 |
m = stream.read(buf, 0, buf.size min (limit - out.size)) |
53 |
if (m != -1) out.write(buf, 0, m) |
|
54 |
} while (m != -1 && limit > out.size) |
|
55 |
||
56 |
new Bytes(out.toByteArray, 0, out.size) |
|
54440 | 57 |
} |
64001
7ecb22be8f03
more general read_stream: return actual byte count;
wenzelm
parents:
63779
diff
changeset
|
58 |
|
7ecb22be8f03
more general read_stream: return actual byte count;
wenzelm
parents:
63779
diff
changeset
|
59 |
def read(file: JFile): Bytes = |
64004 | 60 |
using(new FileInputStream(file))(read_stream(_, file.length.toInt)) |
64229 | 61 |
|
62 |
def read(path: Path): Bytes = read(path.file) |
|
63 |
||
65070 | 64 |
def read(url: URL): Bytes = using(url.openStream)(read_stream(_)) |
65 |
||
64229 | 66 |
|
67 |
/* write */ |
|
68 |
||
69 |
def write(file: JFile, bytes: Bytes) |
|
70 |
{ |
|
71 |
val stream = new FileOutputStream(file) |
|
72 |
try { bytes.write_stream(stream) } finally { stream.close } |
|
73 |
} |
|
74 |
||
75 |
def write(path: Path, bytes: Bytes): Unit = write(path.file, bytes) |
|
54439 | 76 |
} |
77 |
||
78 |
final class Bytes private( |
|
79 |
protected val bytes: Array[Byte], |
|
80 |
protected val offset: Int, |
|
60833 | 81 |
val length: Int) extends CharSequence |
54439 | 82 |
{ |
83 |
/* equality */ |
|
84 |
||
54440 | 85 |
override def equals(that: Any): Boolean = |
86 |
{ |
|
87 |
that match { |
|
88 |
case other: Bytes => |
|
89 |
if (this eq other) true |
|
90 |
else if (length != other.length) false |
|
91 |
else (0 until length).forall(i => bytes(offset + i) == other.bytes(other.offset + i)) |
|
92 |
case _ => false |
|
93 |
} |
|
94 |
} |
|
95 |
||
54439 | 96 |
private lazy val hash: Int = |
97 |
{ |
|
98 |
var h = 0 |
|
99 |
for (i <- offset until offset + length) { |
|
100 |
val b = bytes(i).asInstanceOf[Int] & 0xFF |
|
101 |
h = 31 * h + b |
|
102 |
} |
|
103 |
h |
|
104 |
} |
|
105 |
||
106 |
override def hashCode(): Int = hash |
|
107 |
||
108 |
||
109 |
/* content */ |
|
110 |
||
54512 | 111 |
lazy val sha1_digest: SHA1.Digest = SHA1.digest(bytes) |
54440 | 112 |
|
65279
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
113 |
def text: String = |
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
114 |
UTF8.decode_chars(s => s, bytes, offset, offset + length).toString |
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
115 |
|
54444 | 116 |
override def toString: String = |
64224 | 117 |
{ |
65279
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
118 |
val str = text |
64224 | 119 |
if (str.contains('\uFFFD')) "Bytes(" + length + ")" else str |
120 |
} |
|
54439 | 121 |
|
54440 | 122 |
def isEmpty: Boolean = length == 0 |
54439 | 123 |
|
124 |
def +(other: Bytes): Bytes = |
|
54440 | 125 |
if (other.isEmpty) this |
126 |
else if (isEmpty) other |
|
54439 | 127 |
else { |
128 |
val new_bytes = new Array[Byte](length + other.length) |
|
55618 | 129 |
System.arraycopy(bytes, offset, new_bytes, 0, length) |
130 |
System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length) |
|
54439 | 131 |
new Bytes(new_bytes, 0, new_bytes.length) |
132 |
} |
|
54440 | 133 |
|
134 |
||
60833 | 135 |
/* CharSequence operations */ |
136 |
||
137 |
def charAt(i: Int): Char = |
|
138 |
if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char] |
|
139 |
else throw new IndexOutOfBoundsException |
|
140 |
||
141 |
def subSequence(i: Int, j: Int): Bytes = |
|
142 |
{ |
|
143 |
if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i) |
|
144 |
else throw new IndexOutOfBoundsException |
|
145 |
} |
|
146 |
||
147 |
||
64004 | 148 |
/* streams */ |
149 |
||
150 |
def stream(): ByteArrayInputStream = new ByteArrayInputStream(bytes, offset, length) |
|
151 |
||
152 |
def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
|
153 |
||
154 |
||
155 |
/* XZ data compression */ |
|
54440 | 156 |
|
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
157 |
def uncompress(): Bytes = |
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
158 |
using(new XZInputStream(stream()))(Bytes.read_stream(_, hint = length)) |
64004 | 159 |
|
160 |
def compress(options: XZ.Options = XZ.options()): Bytes = |
|
161 |
{ |
|
162 |
val result = new ByteArrayOutputStream(length) |
|
163 |
using(new XZOutputStream(result, options))(write_stream(_)) |
|
164 |
new Bytes(result.toByteArray, 0, result.size) |
|
165 |
} |
|
54439 | 166 |
} |