author | wenzelm |
Tue, 28 Feb 2017 17:51:49 +0100 | |
changeset 65070 | 1222c010bff7 |
parent 64370 | 865b39487b5d |
child 65279 | fa62e095d8f1 |
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 |
|
54444 | 113 |
override def toString: String = |
64224 | 114 |
{ |
115 |
val str = UTF8.decode_chars(s => s, bytes, offset, offset + length).toString |
|
116 |
if (str.contains('\uFFFD')) "Bytes(" + length + ")" else str |
|
117 |
} |
|
54439 | 118 |
|
54440 | 119 |
def isEmpty: Boolean = length == 0 |
54439 | 120 |
|
121 |
def +(other: Bytes): Bytes = |
|
54440 | 122 |
if (other.isEmpty) this |
123 |
else if (isEmpty) other |
|
54439 | 124 |
else { |
125 |
val new_bytes = new Array[Byte](length + other.length) |
|
55618 | 126 |
System.arraycopy(bytes, offset, new_bytes, 0, length) |
127 |
System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length) |
|
54439 | 128 |
new Bytes(new_bytes, 0, new_bytes.length) |
129 |
} |
|
54440 | 130 |
|
131 |
||
60833 | 132 |
/* CharSequence operations */ |
133 |
||
134 |
def charAt(i: Int): Char = |
|
135 |
if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char] |
|
136 |
else throw new IndexOutOfBoundsException |
|
137 |
||
138 |
def subSequence(i: Int, j: Int): Bytes = |
|
139 |
{ |
|
140 |
if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i) |
|
141 |
else throw new IndexOutOfBoundsException |
|
142 |
} |
|
143 |
||
144 |
||
64004 | 145 |
/* streams */ |
146 |
||
147 |
def stream(): ByteArrayInputStream = new ByteArrayInputStream(bytes, offset, length) |
|
148 |
||
149 |
def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
|
150 |
||
151 |
||
152 |
/* XZ data compression */ |
|
54440 | 153 |
|
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
154 |
def uncompress(): Bytes = |
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
155 |
using(new XZInputStream(stream()))(Bytes.read_stream(_, hint = length)) |
64004 | 156 |
|
157 |
def compress(options: XZ.Options = XZ.options()): Bytes = |
|
158 |
{ |
|
159 |
val result = new ByteArrayOutputStream(length) |
|
160 |
using(new XZOutputStream(result, options))(write_stream(_)) |
|
161 |
new Bytes(result.toByteArray, 0, result.size) |
|
162 |
} |
|
54439 | 163 |
} |