author | wenzelm |
Sat, 15 Oct 2016 20:47:31 +0200 | |
changeset 64229 | 12aa3980f65c |
parent 64224 | 3ed43cfc8b14 |
child 64370 | 865b39487b5d |
permissions | -rw-r--r-- |
54439 | 1 |
/* Title: Pure/General/bytes.scala |
2 |
Module: PIDE |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Immutable byte vectors versus UTF8 strings. |
|
6 |
*/ |
|
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
64004 | 11 |
import java.io.{File => JFile, ByteArrayOutputStream, ByteArrayInputStream, |
64229 | 12 |
OutputStream, InputStream, FileInputStream, FileOutputStream} |
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 |
||
64 |
||
65 |
/* write */ |
|
66 |
||
67 |
def write(file: JFile, bytes: Bytes) |
|
68 |
{ |
|
69 |
val stream = new FileOutputStream(file) |
|
70 |
try { bytes.write_stream(stream) } finally { stream.close } |
|
71 |
} |
|
72 |
||
73 |
def write(path: Path, bytes: Bytes): Unit = write(path.file, bytes) |
|
54439 | 74 |
} |
75 |
||
76 |
final class Bytes private( |
|
77 |
protected val bytes: Array[Byte], |
|
78 |
protected val offset: Int, |
|
60833 | 79 |
val length: Int) extends CharSequence |
54439 | 80 |
{ |
81 |
/* equality */ |
|
82 |
||
54440 | 83 |
override def equals(that: Any): Boolean = |
84 |
{ |
|
85 |
that match { |
|
86 |
case other: Bytes => |
|
87 |
if (this eq other) true |
|
88 |
else if (length != other.length) false |
|
89 |
else (0 until length).forall(i => bytes(offset + i) == other.bytes(other.offset + i)) |
|
90 |
case _ => false |
|
91 |
} |
|
92 |
} |
|
93 |
||
54439 | 94 |
private lazy val hash: Int = |
95 |
{ |
|
96 |
var h = 0 |
|
97 |
for (i <- offset until offset + length) { |
|
98 |
val b = bytes(i).asInstanceOf[Int] & 0xFF |
|
99 |
h = 31 * h + b |
|
100 |
} |
|
101 |
h |
|
102 |
} |
|
103 |
||
104 |
override def hashCode(): Int = hash |
|
105 |
||
106 |
||
107 |
/* content */ |
|
108 |
||
54512 | 109 |
lazy val sha1_digest: SHA1.Digest = SHA1.digest(bytes) |
54440 | 110 |
|
54444 | 111 |
override def toString: String = |
64224 | 112 |
{ |
113 |
val str = UTF8.decode_chars(s => s, bytes, offset, offset + length).toString |
|
114 |
if (str.contains('\uFFFD')) "Bytes(" + length + ")" else str |
|
115 |
} |
|
54439 | 116 |
|
54440 | 117 |
def isEmpty: Boolean = length == 0 |
54439 | 118 |
|
119 |
def +(other: Bytes): Bytes = |
|
54440 | 120 |
if (other.isEmpty) this |
121 |
else if (isEmpty) other |
|
54439 | 122 |
else { |
123 |
val new_bytes = new Array[Byte](length + other.length) |
|
55618 | 124 |
System.arraycopy(bytes, offset, new_bytes, 0, length) |
125 |
System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length) |
|
54439 | 126 |
new Bytes(new_bytes, 0, new_bytes.length) |
127 |
} |
|
54440 | 128 |
|
129 |
||
60833 | 130 |
/* CharSequence operations */ |
131 |
||
132 |
def charAt(i: Int): Char = |
|
133 |
if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char] |
|
134 |
else throw new IndexOutOfBoundsException |
|
135 |
||
136 |
def subSequence(i: Int, j: Int): Bytes = |
|
137 |
{ |
|
138 |
if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i) |
|
139 |
else throw new IndexOutOfBoundsException |
|
140 |
} |
|
141 |
||
142 |
||
64004 | 143 |
/* streams */ |
144 |
||
145 |
def stream(): ByteArrayInputStream = new ByteArrayInputStream(bytes, offset, length) |
|
146 |
||
147 |
def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
|
148 |
||
149 |
||
150 |
/* XZ data compression */ |
|
54440 | 151 |
|
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
152 |
def uncompress(): Bytes = |
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
153 |
using(new XZInputStream(stream()))(Bytes.read_stream(_, hint = length)) |
64004 | 154 |
|
155 |
def compress(options: XZ.Options = XZ.options()): Bytes = |
|
156 |
{ |
|
157 |
val result = new ByteArrayOutputStream(length) |
|
158 |
using(new XZOutputStream(result, options))(write_stream(_)) |
|
159 |
new Bytes(result.toByteArray, 0, result.size) |
|
160 |
} |
|
54439 | 161 |
} |