author | wenzelm |
Sat, 23 Nov 2019 11:19:18 +0100 | |
changeset 71151 | 69a22ccd1817 |
parent 69454 | ef051edd4d10 |
child 71152 | f2d848a596d1 |
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} |
65070 | 12 |
import java.net.URL |
68094 | 13 |
import java.util.Base64 |
64004 | 14 |
|
15 |
import org.tukaani.xz.{XZInputStream, XZOutputStream} |
|
54440 | 16 |
|
17 |
||
54439 | 18 |
object Bytes |
19 |
{ |
|
20 |
val empty: Bytes = new Bytes(Array[Byte](), 0, 0) |
|
21 |
||
22 |
def apply(s: CharSequence): Bytes = |
|
23 |
{ |
|
24 |
val str = s.toString |
|
25 |
if (str.isEmpty) empty |
|
26 |
else { |
|
62527 | 27 |
val b = UTF8.bytes(str) |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
28 |
new Bytes(b, 0, b.length) |
54439 | 29 |
} |
30 |
} |
|
54440 | 31 |
|
63779 | 32 |
def apply(a: Array[Byte]): Bytes = apply(a, 0, a.length) |
33 |
||
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
34 |
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
|
35 |
if (length == 0) empty |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
36 |
else { |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
37 |
val b = new Array[Byte](length) |
55618 | 38 |
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
|
39 |
new Bytes(b, 0, b.length) |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
40 |
} |
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
41 |
|
69454 | 42 |
val newline: Bytes = apply("\n") |
54440 | 43 |
|
68108 | 44 |
def base64(s: String): Bytes = |
45 |
{ |
|
46 |
val a = Base64.getDecoder.decode(s) |
|
47 |
new Bytes(a, 0, a.length) |
|
48 |
} |
|
49 |
||
68087 | 50 |
|
54440 | 51 |
/* read */ |
52 |
||
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
53 |
def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE, hint: Int = 1024): Bytes = |
64004 | 54 |
if (limit == 0) empty |
55 |
else { |
|
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
56 |
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
|
57 |
val buf = new Array[Byte](8192) |
64004 | 58 |
var m = 0 |
54440 | 59 |
|
60 |
do { |
|
64004 | 61 |
m = stream.read(buf, 0, buf.size min (limit - out.size)) |
62 |
if (m != -1) out.write(buf, 0, m) |
|
63 |
} while (m != -1 && limit > out.size) |
|
64 |
||
65 |
new Bytes(out.toByteArray, 0, out.size) |
|
54440 | 66 |
} |
64001
7ecb22be8f03
more general read_stream: return actual byte count;
wenzelm
parents:
63779
diff
changeset
|
67 |
|
7ecb22be8f03
more general read_stream: return actual byte count;
wenzelm
parents:
63779
diff
changeset
|
68 |
def read(file: JFile): Bytes = |
71151 | 69 |
using(new FileInputStream(file))(read_stream(_, limit = file.length.toInt)) |
64229 | 70 |
|
71 |
def read(path: Path): Bytes = read(path.file) |
|
72 |
||
65070 | 73 |
def read(url: URL): Bytes = using(url.openStream)(read_stream(_)) |
74 |
||
64229 | 75 |
|
76 |
/* write */ |
|
77 |
||
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69365
diff
changeset
|
78 |
def write(file: JFile, bytes: Bytes): Unit = |
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69365
diff
changeset
|
79 |
using(new FileOutputStream(file))(bytes.write_stream(_)) |
64229 | 80 |
|
81 |
def write(path: Path, bytes: Bytes): Unit = write(path.file, bytes) |
|
54439 | 82 |
} |
83 |
||
84 |
final class Bytes private( |
|
85 |
protected val bytes: Array[Byte], |
|
86 |
protected val offset: Int, |
|
60833 | 87 |
val length: Int) extends CharSequence |
54439 | 88 |
{ |
89 |
/* equality */ |
|
90 |
||
54440 | 91 |
override def equals(that: Any): Boolean = |
92 |
{ |
|
93 |
that match { |
|
94 |
case other: Bytes => |
|
95 |
if (this eq other) true |
|
96 |
else if (length != other.length) false |
|
97 |
else (0 until length).forall(i => bytes(offset + i) == other.bytes(other.offset + i)) |
|
98 |
case _ => false |
|
99 |
} |
|
100 |
} |
|
101 |
||
54439 | 102 |
private lazy val hash: Int = |
103 |
{ |
|
104 |
var h = 0 |
|
105 |
for (i <- offset until offset + length) { |
|
106 |
val b = bytes(i).asInstanceOf[Int] & 0xFF |
|
107 |
h = 31 * h + b |
|
108 |
} |
|
109 |
h |
|
110 |
} |
|
111 |
||
112 |
override def hashCode(): Int = hash |
|
113 |
||
114 |
||
115 |
/* content */ |
|
116 |
||
54512 | 117 |
lazy val sha1_digest: SHA1.Digest = SHA1.digest(bytes) |
54440 | 118 |
|
69448 | 119 |
def is_empty: Boolean = length == 0 |
120 |
||
121 |
def iterator: Iterator[Byte] = |
|
122 |
for (i <- (offset until (offset + length)).iterator) |
|
123 |
yield bytes(i) |
|
124 |
||
69365
c5b3860d29ef
avoid loading of font file, to eliminate "Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (iText-2.1.5.jar) to method java.nio.DirectByteBuffer.cleaner()" -- due to com.lowagie.text.pdf.TrueTypeFont.process() / RandomAccessFileOrArray;
wenzelm
parents:
68167
diff
changeset
|
125 |
def array: Array[Byte] = |
c5b3860d29ef
avoid loading of font file, to eliminate "Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (iText-2.1.5.jar) to method java.nio.DirectByteBuffer.cleaner()" -- due to com.lowagie.text.pdf.TrueTypeFont.process() / RandomAccessFileOrArray;
wenzelm
parents:
68167
diff
changeset
|
126 |
{ |
c5b3860d29ef
avoid loading of font file, to eliminate "Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (iText-2.1.5.jar) to method java.nio.DirectByteBuffer.cleaner()" -- due to com.lowagie.text.pdf.TrueTypeFont.process() / RandomAccessFileOrArray;
wenzelm
parents:
68167
diff
changeset
|
127 |
val a = new Array[Byte](length) |
c5b3860d29ef
avoid loading of font file, to eliminate "Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (iText-2.1.5.jar) to method java.nio.DirectByteBuffer.cleaner()" -- due to com.lowagie.text.pdf.TrueTypeFont.process() / RandomAccessFileOrArray;
wenzelm
parents:
68167
diff
changeset
|
128 |
System.arraycopy(bytes, offset, a, 0, length) |
c5b3860d29ef
avoid loading of font file, to eliminate "Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (iText-2.1.5.jar) to method java.nio.DirectByteBuffer.cleaner()" -- due to com.lowagie.text.pdf.TrueTypeFont.process() / RandomAccessFileOrArray;
wenzelm
parents:
68167
diff
changeset
|
129 |
a |
c5b3860d29ef
avoid loading of font file, to eliminate "Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (iText-2.1.5.jar) to method java.nio.DirectByteBuffer.cleaner()" -- due to com.lowagie.text.pdf.TrueTypeFont.process() / RandomAccessFileOrArray;
wenzelm
parents:
68167
diff
changeset
|
130 |
} |
c5b3860d29ef
avoid loading of font file, to eliminate "Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (iText-2.1.5.jar) to method java.nio.DirectByteBuffer.cleaner()" -- due to com.lowagie.text.pdf.TrueTypeFont.process() / RandomAccessFileOrArray;
wenzelm
parents:
68167
diff
changeset
|
131 |
|
65279
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
132 |
def text: String = |
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
133 |
UTF8.decode_chars(s => s, bytes, offset, offset + length).toString |
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
134 |
|
68094 | 135 |
def base64: String = |
136 |
{ |
|
137 |
val b = |
|
138 |
if (offset == 0 && length == bytes.length) bytes |
|
139 |
else Bytes(bytes, offset, length).bytes |
|
140 |
Base64.getEncoder.encodeToString(b) |
|
141 |
} |
|
142 |
||
68106 | 143 |
def maybe_base64: (Boolean, String) = |
144 |
{ |
|
145 |
val s = text |
|
146 |
if (this == Bytes(s)) (false, s) else (true, base64) |
|
147 |
} |
|
148 |
||
68150
f0f34cbed539
clarified output: avoid costly operations on huge blobs;
wenzelm
parents:
68149
diff
changeset
|
149 |
override def toString: String = "Bytes(" + length + ")" |
54439 | 150 |
|
54440 | 151 |
def isEmpty: Boolean = length == 0 |
54439 | 152 |
|
65630 | 153 |
def proper: Option[Bytes] = if (isEmpty) None else Some(this) |
154 |
def proper_text: Option[String] = if (isEmpty) None else Some(text) |
|
155 |
||
54439 | 156 |
def +(other: Bytes): Bytes = |
54440 | 157 |
if (other.isEmpty) this |
158 |
else if (isEmpty) other |
|
54439 | 159 |
else { |
160 |
val new_bytes = new Array[Byte](length + other.length) |
|
55618 | 161 |
System.arraycopy(bytes, offset, new_bytes, 0, length) |
162 |
System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length) |
|
54439 | 163 |
new Bytes(new_bytes, 0, new_bytes.length) |
164 |
} |
|
54440 | 165 |
|
166 |
||
60833 | 167 |
/* CharSequence operations */ |
168 |
||
169 |
def charAt(i: Int): Char = |
|
170 |
if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char] |
|
171 |
else throw new IndexOutOfBoundsException |
|
172 |
||
173 |
def subSequence(i: Int, j: Int): Bytes = |
|
174 |
{ |
|
175 |
if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i) |
|
176 |
else throw new IndexOutOfBoundsException |
|
177 |
} |
|
178 |
||
69448 | 179 |
def trim_line: Bytes = |
180 |
if (length >= 2 && charAt(length - 2) == 13 && charAt(length - 1) == 10) |
|
181 |
subSequence(0, length - 2) |
|
182 |
else if (length >= 1 && (charAt(length - 1) == 13 || charAt(length - 1) == 10)) |
|
183 |
subSequence(0, length - 1) |
|
184 |
else this |
|
185 |
||
60833 | 186 |
|
64004 | 187 |
/* streams */ |
188 |
||
189 |
def stream(): ByteArrayInputStream = new ByteArrayInputStream(bytes, offset, length) |
|
190 |
||
191 |
def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
|
192 |
||
193 |
||
194 |
/* XZ data compression */ |
|
54440 | 195 |
|
68018 | 196 |
def uncompress(cache: XZ.Cache = XZ.cache()): Bytes = |
197 |
using(new XZInputStream(stream(), cache))(Bytes.read_stream(_, hint = length)) |
|
64004 | 198 |
|
68018 | 199 |
def compress(options: XZ.Options = XZ.options(), cache: XZ.Cache = XZ.cache()): Bytes = |
64004 | 200 |
{ |
201 |
val result = new ByteArrayOutputStream(length) |
|
68018 | 202 |
using(new XZOutputStream(result, options, cache))(write_stream(_)) |
64004 | 203 |
new Bytes(result.toByteArray, 0, result.size) |
204 |
} |
|
68167 | 205 |
|
206 |
def maybe_compress(options: XZ.Options = XZ.options(), cache: XZ.Cache = XZ.cache()) |
|
207 |
: (Boolean, Bytes) = |
|
208 |
{ |
|
209 |
val compressed = compress(options = options, cache = cache) |
|
210 |
if (compressed.length < length) (true, compressed) else (false, this) |
|
211 |
} |
|
54439 | 212 |
} |