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