author | wenzelm |
Mon, 03 Oct 2016 12:28:36 +0200 | |
changeset 64004 | b4ece7a3f2ca |
parent 64001 | 7ecb22be8f03 |
child 64005 | f6e965cf1617 |
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 |
||
64004 | 44 |
def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE): Bytes = |
45 |
if (limit == 0) empty |
|
46 |
else { |
|
47 |
val out = new ByteArrayOutputStream(if (limit == Integer.MAX_VALUE) 1024 else limit) |
|
48 |
val buf = new Array[Byte](1024) |
|
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 = |
99 |
UTF8.decode_chars(s => s, bytes, offset, offset + length).toString |
|
54439 | 100 |
|
54440 | 101 |
def isEmpty: Boolean = length == 0 |
54439 | 102 |
|
103 |
def +(other: Bytes): Bytes = |
|
54440 | 104 |
if (other.isEmpty) this |
105 |
else if (isEmpty) other |
|
54439 | 106 |
else { |
107 |
val new_bytes = new Array[Byte](length + other.length) |
|
55618 | 108 |
System.arraycopy(bytes, offset, new_bytes, 0, length) |
109 |
System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length) |
|
54439 | 110 |
new Bytes(new_bytes, 0, new_bytes.length) |
111 |
} |
|
54440 | 112 |
|
113 |
||
60833 | 114 |
/* CharSequence operations */ |
115 |
||
116 |
def charAt(i: Int): Char = |
|
117 |
if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char] |
|
118 |
else throw new IndexOutOfBoundsException |
|
119 |
||
120 |
def subSequence(i: Int, j: Int): Bytes = |
|
121 |
{ |
|
122 |
if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i) |
|
123 |
else throw new IndexOutOfBoundsException |
|
124 |
} |
|
125 |
||
126 |
||
64004 | 127 |
/* streams */ |
128 |
||
129 |
def stream(): ByteArrayInputStream = new ByteArrayInputStream(bytes, offset, length) |
|
130 |
||
131 |
def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
|
132 |
||
133 |
||
134 |
/* XZ data compression */ |
|
54440 | 135 |
|
64004 | 136 |
def uncompress(): Bytes = using(new XZInputStream(stream()))(Bytes.read_stream(_)) |
137 |
||
138 |
def compress(options: XZ.Options = XZ.options()): Bytes = |
|
139 |
{ |
|
140 |
val result = new ByteArrayOutputStream(length) |
|
141 |
using(new XZOutputStream(result, options))(write_stream(_)) |
|
142 |
new Bytes(result.toByteArray, 0, result.size) |
|
143 |
} |
|
54439 | 144 |
} |