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