author | wenzelm |
Sun, 04 Sep 2016 17:38:22 +0200 | |
changeset 63779 | 9da65bc75610 |
parent 62527 | aae9a2a855e0 |
child 64001 | 7ecb22be8f03 |
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 |
||
54440 | 11 |
import java.io.{File => JFile, OutputStream, FileInputStream} |
12 |
||
13 |
||
54439 | 14 |
object Bytes |
15 |
{ |
|
16 |
val empty: Bytes = new Bytes(Array[Byte](), 0, 0) |
|
17 |
||
18 |
def apply(s: CharSequence): Bytes = |
|
19 |
{ |
|
20 |
val str = s.toString |
|
21 |
if (str.isEmpty) empty |
|
22 |
else { |
|
62527 | 23 |
val b = UTF8.bytes(str) |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
24 |
new Bytes(b, 0, b.length) |
54439 | 25 |
} |
26 |
} |
|
54440 | 27 |
|
63779 | 28 |
def apply(a: Array[Byte]): Bytes = apply(a, 0, a.length) |
29 |
||
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
30 |
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
|
31 |
if (length == 0) empty |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
32 |
else { |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
33 |
val b = new Array[Byte](length) |
55618 | 34 |
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
|
35 |
new Bytes(b, 0, b.length) |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
36 |
} |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
37 |
|
54440 | 38 |
|
39 |
/* read */ |
|
40 |
||
41 |
def read(file: JFile): Bytes = |
|
42 |
{ |
|
43 |
var i = 0 |
|
44 |
var m = 0 |
|
45 |
val n = file.length.toInt |
|
46 |
val bytes = new Array[Byte](n) |
|
47 |
||
48 |
val stream = new FileInputStream(file) |
|
49 |
try { |
|
50 |
do { |
|
51 |
m = stream.read(bytes, i, n - i) |
|
52 |
if (m != -1) i += m |
|
53 |
} while (m != -1 && n > i) |
|
54 |
} |
|
55 |
finally { stream.close } |
|
56 |
||
57 |
new Bytes(bytes, 0, bytes.length) |
|
58 |
} |
|
54439 | 59 |
} |
60 |
||
61 |
final class Bytes private( |
|
62 |
protected val bytes: Array[Byte], |
|
63 |
protected val offset: Int, |
|
60833 | 64 |
val length: Int) extends CharSequence |
54439 | 65 |
{ |
66 |
/* equality */ |
|
67 |
||
54440 | 68 |
override def equals(that: Any): Boolean = |
69 |
{ |
|
70 |
that match { |
|
71 |
case other: Bytes => |
|
72 |
if (this eq other) true |
|
73 |
else if (length != other.length) false |
|
74 |
else (0 until length).forall(i => bytes(offset + i) == other.bytes(other.offset + i)) |
|
75 |
case _ => false |
|
76 |
} |
|
77 |
} |
|
78 |
||
54439 | 79 |
private lazy val hash: Int = |
80 |
{ |
|
81 |
var h = 0 |
|
82 |
for (i <- offset until offset + length) { |
|
83 |
val b = bytes(i).asInstanceOf[Int] & 0xFF |
|
84 |
h = 31 * h + b |
|
85 |
} |
|
86 |
h |
|
87 |
} |
|
88 |
||
89 |
override def hashCode(): Int = hash |
|
90 |
||
91 |
||
92 |
/* content */ |
|
93 |
||
54512 | 94 |
lazy val sha1_digest: SHA1.Digest = SHA1.digest(bytes) |
54440 | 95 |
|
54444 | 96 |
override def toString: String = |
97 |
UTF8.decode_chars(s => s, bytes, offset, offset + length).toString |
|
54439 | 98 |
|
54440 | 99 |
def isEmpty: Boolean = length == 0 |
54439 | 100 |
|
101 |
def +(other: Bytes): Bytes = |
|
54440 | 102 |
if (other.isEmpty) this |
103 |
else if (isEmpty) other |
|
54439 | 104 |
else { |
105 |
val new_bytes = new Array[Byte](length + other.length) |
|
55618 | 106 |
System.arraycopy(bytes, offset, new_bytes, 0, length) |
107 |
System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length) |
|
54439 | 108 |
new Bytes(new_bytes, 0, new_bytes.length) |
109 |
} |
|
54440 | 110 |
|
111 |
||
60833 | 112 |
/* CharSequence operations */ |
113 |
||
114 |
def charAt(i: Int): Char = |
|
115 |
if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char] |
|
116 |
else throw new IndexOutOfBoundsException |
|
117 |
||
118 |
def subSequence(i: Int, j: Int): Bytes = |
|
119 |
{ |
|
120 |
if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i) |
|
121 |
else throw new IndexOutOfBoundsException |
|
122 |
} |
|
123 |
||
124 |
||
54440 | 125 |
/* write */ |
126 |
||
127 |
def write(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
|
54439 | 128 |
} |
129 |