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