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