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