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