author | wenzelm |
Fri, 21 Oct 2022 21:39:38 +0200 | |
changeset 76358 | cff0828c374f |
parent 76353 | 3698d0f3da18 |
child 76361 | 3b9f36ef7365 |
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 |
||
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
10 |
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, FileInputStream, FileOutputStream, InputStream, OutputStream, File as JFile} |
65070 | 11 |
import java.net.URL |
76353 | 12 |
import org.tukaani.xz |
13 |
import com.github.luben.zstd |
|
54440 | 14 |
|
15 |
||
75393 | 16 |
object Bytes { |
54439 | 17 |
val empty: Bytes = new Bytes(Array[Byte](), 0, 0) |
18 |
||
75393 | 19 |
def apply(s: CharSequence): Bytes = { |
54439 | 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 |
|
69454 | 38 |
val newline: Bytes = apply("\n") |
54440 | 39 |
|
73576 | 40 |
|
41 |
/* base64 */ |
|
42 |
||
75587 | 43 |
def decode_base64(s: String): Bytes = { |
75620 | 44 |
val a = Base64.decode(s) |
68108 | 45 |
new Bytes(a, 0, a.length) |
46 |
} |
|
47 |
||
75579 | 48 |
|
54440 | 49 |
/* read */ |
50 |
||
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
51 |
def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE, hint: Int = 1024): Bytes = |
64004 | 52 |
if (limit == 0) empty |
53 |
else { |
|
73414 | 54 |
val out_size = (if (limit == Integer.MAX_VALUE) hint else limit) max 1024 |
55 |
val out = new ByteArrayOutputStream(out_size) |
|
64005
f6e965cf1617
clarified magic values (see also java/io/BufferedInputStream.java);
wenzelm
parents:
64004
diff
changeset
|
56 |
val buf = new Array[Byte](8192) |
64004 | 57 |
var m = 0 |
54440 | 58 |
|
75709 | 59 |
while ({ |
73554 | 60 |
m = stream.read(buf, 0, buf.length min (limit - out.size)) |
64004 | 61 |
if (m != -1) out.write(buf, 0, m) |
75709 | 62 |
m != -1 && limit > out.size |
63 |
}) () |
|
64004 | 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 |
|
75393 | 68 |
def read(file: JFile): Bytes = { |
71152
f2d848a596d1
more robust: file length can be invalid in odd situations;
wenzelm
parents:
71151
diff
changeset
|
69 |
val length = file.length |
f2d848a596d1
more robust: file length can be invalid in odd situations;
wenzelm
parents:
71151
diff
changeset
|
70 |
val limit = if (length < 0 || length > Integer.MAX_VALUE) Integer.MAX_VALUE else length.toInt |
f2d848a596d1
more robust: file length can be invalid in odd situations;
wenzelm
parents:
71151
diff
changeset
|
71 |
using(new FileInputStream(file))(read_stream(_, limit = limit)) |
f2d848a596d1
more robust: file length can be invalid in odd situations;
wenzelm
parents:
71151
diff
changeset
|
72 |
} |
64229 | 73 |
|
74 |
def read(path: Path): Bytes = read(path.file) |
|
75 |
||
65070 | 76 |
def read(url: URL): Bytes = using(url.openStream)(read_stream(_)) |
77 |
||
64229 | 78 |
|
79 |
/* write */ |
|
80 |
||
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69365
diff
changeset
|
81 |
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
|
82 |
using(new FileOutputStream(file))(bytes.write_stream(_)) |
64229 | 83 |
|
84 |
def write(path: Path, bytes: Bytes): Unit = write(path.file, bytes) |
|
54439 | 85 |
} |
86 |
||
87 |
final class Bytes private( |
|
88 |
protected val bytes: Array[Byte], |
|
89 |
protected val offset: Int, |
|
75393 | 90 |
val length: Int) extends CharSequence { |
54439 | 91 |
/* equality */ |
92 |
||
75393 | 93 |
override def equals(that: Any): Boolean = { |
54440 | 94 |
that match { |
95 |
case other: Bytes => |
|
96 |
if (this eq other) true |
|
97 |
else if (length != other.length) false |
|
98 |
else (0 until length).forall(i => bytes(offset + i) == other.bytes(other.offset + i)) |
|
99 |
case _ => false |
|
100 |
} |
|
101 |
} |
|
102 |
||
75393 | 103 |
private lazy val hash: Int = { |
54439 | 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 |
||
75393 | 125 |
def array: Array[Byte] = { |
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
|
126 |
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
|
127 |
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
|
128 |
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
|
129 |
} |
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 |
|
73561
c83152933579
clarified signature: Bytes extends CharSequence already (see d201996f72a8);
wenzelm
parents:
73559
diff
changeset
|
131 |
def text: String = UTF8.decode_permissive(this) |
65279
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
132 |
|
76236 | 133 |
def wellformed_text: Option[String] = { |
134 |
val s = text |
|
135 |
if (this == Bytes(s)) Some(s) else None |
|
136 |
} |
|
137 |
||
75587 | 138 |
def encode_base64: String = { |
68094 | 139 |
val b = |
140 |
if (offset == 0 && length == bytes.length) bytes |
|
141 |
else Bytes(bytes, offset, length).bytes |
|
75620 | 142 |
Base64.encode(b) |
68094 | 143 |
} |
144 |
||
76236 | 145 |
def maybe_encode_base64: (Boolean, String) = |
146 |
wellformed_text match { |
|
147 |
case Some(s) => (false, s) |
|
148 |
case None => (true, encode_base64) |
|
149 |
} |
|
68106 | 150 |
|
68150
f0f34cbed539
clarified output: avoid costly operations on huge blobs;
wenzelm
parents:
68149
diff
changeset
|
151 |
override def toString: String = "Bytes(" + length + ")" |
54439 | 152 |
|
72885 | 153 |
def proper: Option[Bytes] = if (is_empty) None else Some(this) |
154 |
def proper_text: Option[String] = if (is_empty) None else Some(text) |
|
65630 | 155 |
|
54439 | 156 |
def +(other: Bytes): Bytes = |
72885 | 157 |
if (other.is_empty) this |
158 |
else if (is_empty) 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 |
||
75393 | 173 |
def subSequence(i: Int, j: Int): Bytes = { |
60833 | 174 |
if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i) |
175 |
else throw new IndexOutOfBoundsException |
|
176 |
} |
|
177 |
||
69448 | 178 |
def trim_line: Bytes = |
179 |
if (length >= 2 && charAt(length - 2) == 13 && charAt(length - 1) == 10) |
|
180 |
subSequence(0, length - 2) |
|
181 |
else if (length >= 1 && (charAt(length - 1) == 13 || charAt(length - 1) == 10)) |
|
182 |
subSequence(0, length - 1) |
|
183 |
else this |
|
184 |
||
60833 | 185 |
|
64004 | 186 |
/* streams */ |
187 |
||
188 |
def stream(): ByteArrayInputStream = new ByteArrayInputStream(bytes, offset, length) |
|
189 |
||
190 |
def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
|
191 |
||
192 |
||
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
193 |
/* XZ / Zstd data compression */ |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
194 |
|
76358 | 195 |
def detect_xz: Boolean = |
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
196 |
length >= 6 && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
197 |
bytes(offset) == 0xFD.toByte && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
198 |
bytes(offset + 1) == 0x37.toByte && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
199 |
bytes(offset + 2) == 0x7A.toByte && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
200 |
bytes(offset + 3) == 0x58.toByte && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
201 |
bytes(offset + 4) == 0x5A.toByte && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
202 |
bytes(offset + 5) == 0x00.toByte |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
203 |
|
76358 | 204 |
def detect_zstd: Boolean = |
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
205 |
length >= 4 && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
206 |
bytes(offset) == 0x28.toByte && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
207 |
bytes(offset + 1) == 0xB5.toByte && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
208 |
bytes(offset + 2) == 0x2F.toByte && |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
209 |
bytes(offset + 3) == 0xFD.toByte |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
210 |
|
76358 | 211 |
def uncompress_xz(cache: Compress.Cache = Compress.Cache.none): Bytes = |
212 |
using(new xz.XZInputStream(stream(), cache.for_xz))(Bytes.read_stream(_, hint = length)) |
|
213 |
||
214 |
def uncompress_zstd(cache: Compress.Cache = Compress.Cache.none): Bytes = { |
|
215 |
Zstd.init() |
|
216 |
val n = zstd.Zstd.decompressedSize(bytes, offset, length) |
|
217 |
if (n > 0 && n < Integer.MAX_VALUE) { |
|
218 |
Bytes(zstd.Zstd.decompress(array, n.toInt)) |
|
219 |
} |
|
220 |
else { |
|
221 |
using(new zstd.ZstdInputStream(stream(), cache.for_zstd))(Bytes.read_stream(_, hint = length)) |
|
222 |
} |
|
223 |
} |
|
54440 | 224 |
|
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
225 |
def uncompress(cache: Compress.Cache = Compress.Cache.none): Bytes = |
76358 | 226 |
if (detect_xz) uncompress_xz(cache = cache) |
227 |
else if (detect_zstd) uncompress_zstd(cache = cache) |
|
228 |
else error("Cannot detect compression scheme") |
|
64004 | 229 |
|
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
230 |
def compress( |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
231 |
options: Compress.Options = Compress.Options(), |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
232 |
cache: Compress.Cache = Compress.Cache.none |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
233 |
): Bytes = { |
76358 | 234 |
options match { |
235 |
case options_xz: Compress.Options_XZ => |
|
236 |
val result = new ByteArrayOutputStream(length) |
|
237 |
using(new xz.XZOutputStream(result, options_xz.make, cache.for_xz))(write_stream) |
|
238 |
new Bytes(result.toByteArray, 0, result.size) |
|
239 |
case options_zstd: Compress.Options_Zstd => |
|
240 |
Zstd.init() |
|
241 |
Bytes(zstd.Zstd.compress(array, options_zstd.level)) |
|
242 |
} |
|
64004 | 243 |
} |
68167 | 244 |
|
75393 | 245 |
def maybe_compress( |
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
246 |
options: Compress.Options = Compress.Options(), |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
247 |
cache: Compress.Cache = Compress.Cache.none |
75393 | 248 |
) : (Boolean, Bytes) = { |
68167 | 249 |
val compressed = compress(options = options, cache = cache) |
250 |
if (compressed.length < length) (true, compressed) else (false, this) |
|
251 |
} |
|
54439 | 252 |
} |