author | wenzelm |
Wed, 03 Jul 2024 20:18:36 +0200 | |
changeset 80491 | b439582efc8a |
parent 80485 | 71aaa7e65d20 |
child 80492 | 43323d886ea3 |
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 |
||
78855 | 10 |
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, FileInputStream, FileOutputStream, |
11 |
InputStream, OutputStream, File => JFile} |
|
77711
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
12 |
import java.nio.ByteBuffer |
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
13 |
import java.nio.channels.FileChannel |
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
14 |
import java.nio.file.StandardOpenOption |
77712
dd4bb80dbc3a
tuned performance: much faster low-level operation;
wenzelm
parents:
77711
diff
changeset
|
15 |
import java.util.Arrays |
76353 | 16 |
import org.tukaani.xz |
17 |
import com.github.luben.zstd |
|
54440 | 18 |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
19 |
import scala.collection.mutable.ArrayBuffer |
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
20 |
import scala.collection.mutable |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
21 |
|
54440 | 22 |
|
75393 | 23 |
object Bytes { |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
24 |
/* internal sizes */ |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
25 |
|
80385 | 26 |
private val array_size: Long = Int.MaxValue - 8 // see java.io.InputStream.MAX_BUFFER_SIZE |
80389 | 27 |
private val block_size: Int = 16384 // see java.io.InputStream.DEFAULT_BUFFER_SIZE |
80385 | 28 |
private val chunk_size: Long = Space.MiB(100).bytes |
29 |
||
30 |
private def make_size(chunks: Array[Array[Byte]], buffer: Array[Byte]): Long = |
|
31 |
chunks.foldLeft(buffer.length.toLong)((n, chunk) => n + chunk.length) |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
32 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
33 |
class Too_Large(size: Long) extends IndexOutOfBoundsException { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
34 |
override def getMessage: String = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
35 |
"Bytes too large for particular operation: " + |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
36 |
Space.bytes(size).print + " > " + Space.bytes(array_size).print |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
37 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
38 |
|
54439 | 39 |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
40 |
/* main constructors */ |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
41 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
42 |
private def reuse_array(bytes: Array[Byte]): Bytes = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
43 |
if (bytes.length <= chunk_size) new Bytes(None, bytes, 0L, bytes.length.toLong) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
44 |
else apply(bytes) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
45 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
46 |
val empty: Bytes = reuse_array(new Array(0)) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
47 |
|
80491 | 48 |
def apply(s: String): Bytes = |
80373 | 49 |
if (s.isEmpty) empty |
50 |
else Builder.use(hint = s.length) { builder => builder += s } |
|
54440 | 51 |
|
63779 | 52 |
def apply(a: Array[Byte]): Bytes = apply(a, 0, a.length) |
53 |
||
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
54 |
def apply(a: Array[Byte], offset: Int, length: Int): Bytes = |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
55 |
Builder.use(hint = length) { builder => builder += (a, offset, length) } |
54442
c39972ddd672
more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents:
54440
diff
changeset
|
56 |
|
69454 | 57 |
val newline: Bytes = apply("\n") |
54440 | 58 |
|
75579 | 59 |
|
54440 | 60 |
/* read */ |
61 |
||
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
62 |
def read_stream(stream: InputStream, limit: Long = -1L, hint: Long = 0L): Bytes = { |
64004 | 63 |
if (limit == 0) empty |
64 |
else { |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
65 |
Builder.use(hint = if (limit > 0) limit else hint) { builder => |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
66 |
val buf_size = block_size |
80384 | 67 |
val buf = new Array[Byte](block_size) |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
68 |
var m = 0 |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
69 |
var n = 0L |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
70 |
while ({ |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
71 |
val l = if (limit > 0) (limit - n).min(buf_size).toInt else buf_size |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
72 |
m = stream.read(buf, 0, l) |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
73 |
if (m > 0) { |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
74 |
builder += (buf, 0, m) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
75 |
n += m |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
76 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
77 |
m != -1 && (limit < 0 || limit > n) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
78 |
}) () |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
79 |
} |
54440 | 80 |
} |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
81 |
} |
64001
7ecb22be8f03
more general read_stream: return actual byte count;
wenzelm
parents:
63779
diff
changeset
|
82 |
|
79510
d8330439823a
clarified signature: explicit type isabelle.Url to avoid oddities of java.net.URL (e.g. its "equals" method);
wenzelm
parents:
79509
diff
changeset
|
83 |
def read_url(name: String): Bytes = using(Url(name).open_stream())(read_stream(_)) |
77717 | 84 |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
85 |
def read_file(path: Path, offset: Long = 0L, limit: Long = -1L): Bytes = { |
78956 | 86 |
val length = File.size(path) |
77711
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
87 |
val start = offset.max(0L) |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
88 |
val len = (length - start).max(0L).min(if (limit < 0) Long.MaxValue else limit) |
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
89 |
if (len == 0L) empty |
77711
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
90 |
else { |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
91 |
Builder.use(hint = len) { builder => |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
92 |
using(FileChannel.open(path.java_path, StandardOpenOption.READ)) { channel => |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
93 |
channel.position(start) |
80473
09afc244bb5e
proper limit for read operation (amending ac4d53bc8f6b);
wenzelm
parents:
80428
diff
changeset
|
94 |
val buf_size = block_size |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
95 |
val buf = ByteBuffer.allocate(buf_size) |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
96 |
var m = 0 |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
97 |
var n = 0L |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
98 |
while ({ |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
99 |
val l = (len - n).min(buf_size).toInt |
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
100 |
buf.limit(l) |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
101 |
m = channel.read(buf) |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
102 |
if (m > 0) { |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
103 |
builder += (buf.array(), 0, m) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
104 |
buf.clear() |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
105 |
n += m |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
106 |
} |
80474
66ebeae3acdc
misc tuning: more uniform read_stream vs. read_file;
wenzelm
parents:
80473
diff
changeset
|
107 |
m != -1 && len > n |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
108 |
}) () |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
109 |
} |
77711
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
110 |
} |
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
111 |
} |
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
112 |
} |
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
76361
diff
changeset
|
113 |
|
78953 | 114 |
def read(path: Path): Bytes = read_file(path) |
115 |
def read(file: JFile): Bytes = read_file(File.path(file)) |
|
77718 | 116 |
|
64229 | 117 |
|
118 |
/* write */ |
|
119 |
||
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69365
diff
changeset
|
120 |
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
|
121 |
using(new FileOutputStream(file))(bytes.write_stream(_)) |
64229 | 122 |
|
123 |
def write(path: Path, bytes: Bytes): Unit = write(path.file, bytes) |
|
78194 | 124 |
|
125 |
||
126 |
/* append */ |
|
127 |
||
128 |
def append(file: JFile, bytes: Bytes): Unit = |
|
129 |
using(new FileOutputStream(file, true))(bytes.write_stream(_)) |
|
130 |
||
131 |
def append(path: Path, bytes: Bytes): Unit = append(path.file, bytes) |
|
80351 | 132 |
|
133 |
||
80355 | 134 |
/* vector of short unsigned integers */ |
80351 | 135 |
|
136 |
trait Vec { |
|
137 |
def size: Long |
|
80355 | 138 |
def apply(i: Long): Char |
80351 | 139 |
} |
140 |
||
141 |
class Vec_String(string: String) extends Vec { |
|
142 |
override def size: Long = string.length.toLong |
|
80355 | 143 |
override def apply(i: Long): Char = |
144 |
if (0 <= i && i < size) string(i.toInt) |
|
80351 | 145 |
else throw new IndexOutOfBoundsException |
146 |
} |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
147 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
148 |
|
80380
94d903234f6b
Bytes.Builder is unsynchronized, like java.io.OutputBuffer;
wenzelm
parents:
80379
diff
changeset
|
149 |
/* incremental builder: unsynchronized! */ |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
150 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
151 |
object Builder { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
152 |
def use(hint: Long = 0L)(body: Builder => Unit): Bytes = { |
80375 | 153 |
val builder = new Builder(hint) |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
154 |
body(builder) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
155 |
builder.done() |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
156 |
} |
80377
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
157 |
|
80394 | 158 |
def use_stream(hint: Long = 0L)(body: OutputStream => Unit): Bytes = { |
159 |
val stream = new Stream(hint = hint) |
|
160 |
body(stream) |
|
161 |
stream.builder.done() |
|
162 |
} |
|
163 |
||
164 |
private class Stream(hint: Long = 0L) extends OutputStream { |
|
80377
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
165 |
val builder = new Builder(hint) |
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
166 |
|
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
167 |
override def write(b: Int): Unit = |
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
168 |
{ builder += b.toByte } |
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
169 |
|
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
170 |
override def write(array: Array[Byte], offset: Int, length: Int): Unit = |
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
171 |
{ builder += (array, offset, length) } |
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
172 |
} |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
173 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
174 |
|
80375 | 175 |
final class Builder private[Bytes](hint: Long) { |
80485 | 176 |
builder => |
177 |
||
80375 | 178 |
private var chunks = |
179 |
new ArrayBuffer[Array[Byte]](if (hint <= 0) 16 else (hint / chunk_size).toInt) |
|
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
180 |
|
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
181 |
private var buffer_list: mutable.ListBuffer[Array[Byte]] = null |
80375 | 182 |
private var buffer = |
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
183 |
new Array[Byte](if (hint <= 0) 1024 else (hint min chunk_size min array_size).toInt) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
184 |
private var buffer_index = 0 |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
185 |
private var buffer_total = 0 |
80370 | 186 |
|
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
187 |
private def buffer_content(): Array[Byte] = |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
188 |
if (buffer_list != null) { |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
189 |
val array = new Array[Byte](buffer_total) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
190 |
var i = 0 |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
191 |
for (b <- buffer_list) { |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
192 |
val n = b.length |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
193 |
System.arraycopy(b, 0, array, i, n) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
194 |
i += n |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
195 |
} |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
196 |
System.arraycopy(buffer, 0, array, i, buffer_index) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
197 |
array |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
198 |
} |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
199 |
else if (buffer_index == buffer.length) buffer else Arrays.copyOf(buffer, buffer_index) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
200 |
|
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
201 |
private def buffer_check(request: Int = 0): Unit = |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
202 |
if (buffer_index == buffer.length) { |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
203 |
if (buffer_total == chunk_size) { |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
204 |
chunks += buffer_content() |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
205 |
buffer_list = null |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
206 |
buffer = new Array[Byte](chunk_size.toInt) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
207 |
buffer_total = 0 |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
208 |
buffer_index = 0 |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
209 |
} |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
210 |
else { |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
211 |
if (buffer_list == null) { buffer_list = new mutable.ListBuffer } |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
212 |
buffer_list += buffer |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
213 |
buffer_index = 0 |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
214 |
val limit = (chunk_size - buffer_total).toInt |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
215 |
buffer = new Array[Byte]((buffer_total max request) min limit) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
216 |
} |
80370 | 217 |
} |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
218 |
|
80380
94d903234f6b
Bytes.Builder is unsynchronized, like java.io.OutputBuffer;
wenzelm
parents:
80379
diff
changeset
|
219 |
def += (b: Byte): Unit = { |
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
220 |
buffer(buffer_index) = b |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
221 |
buffer_total += 1 |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
222 |
buffer_index += 1 |
80377
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
223 |
buffer_check() |
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
224 |
} |
28dd9b91dfe5
more scalable compression, using Bytes.Builder.Stream;
wenzelm
parents:
80376
diff
changeset
|
225 |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
226 |
def += (array: Array[Byte], offset: Int, length: Int): Unit = { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
227 |
if (offset < 0 || length < 0 || offset.toLong + length.toLong > array.length) { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
228 |
throw new IndexOutOfBoundsException |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
229 |
} |
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
230 |
else { |
80380
94d903234f6b
Bytes.Builder is unsynchronized, like java.io.OutputBuffer;
wenzelm
parents:
80379
diff
changeset
|
231 |
var i = offset |
94d903234f6b
Bytes.Builder is unsynchronized, like java.io.OutputBuffer;
wenzelm
parents:
80379
diff
changeset
|
232 |
var n = length |
94d903234f6b
Bytes.Builder is unsynchronized, like java.io.OutputBuffer;
wenzelm
parents:
80379
diff
changeset
|
233 |
while (n > 0) { |
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
234 |
val l = n min (buffer.length - buffer_index) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
235 |
System.arraycopy(array, i, buffer, buffer_index, l) |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
236 |
buffer_total += l |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
237 |
buffer_index += l |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
238 |
i += l |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
239 |
n -= l |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
240 |
buffer_check(request = n) |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
241 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
242 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
243 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
244 |
|
80491 | 245 |
def += (s: String): Unit = |
246 |
if (s.length > 0) { builder += UTF8.bytes(s) } |
|
80428 | 247 |
|
80485 | 248 |
def += (array: Array[Byte]): Unit = { builder += (array, 0, array.length) } |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
249 |
|
80485 | 250 |
def += (a: Subarray): Unit = { builder += (a.array, a.offset, a.length) } |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
251 |
|
80394 | 252 |
private def done(): Bytes = { |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
253 |
val cs = chunks.toArray |
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
254 |
val b = buffer_content() |
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
255 |
val size = cs.foldLeft(b.length.toLong)({ case (n, a) => n + a.length }) |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
256 |
chunks = null |
80426
7d2922f0ae2b
performance tuning: multi-stage buffer with fewer array copies;
wenzelm
parents:
80394
diff
changeset
|
257 |
buffer_list = null |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
258 |
buffer = null |
80374 | 259 |
new Bytes(if (cs.isEmpty) None else Some(cs), b, 0L, size) |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
260 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
261 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
262 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
263 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
264 |
/* subarray */ |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
265 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
266 |
object Subarray { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
267 |
val empty: Subarray = new Subarray(new Array[Byte](0), 0, 0) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
268 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
269 |
def apply(array: Array[Byte], offset: Int, length: Int): Subarray = { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
270 |
val n = array.length |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
271 |
if (0 <= offset && offset < n && 0 <= length && offset + length <= n) { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
272 |
if (length == 0) empty |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
273 |
else new Subarray(array, offset, length) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
274 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
275 |
else throw new IndexOutOfBoundsException |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
276 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
277 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
278 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
279 |
final class Subarray private( |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
280 |
val array: Array[Byte], |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
281 |
val offset: Int, |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
282 |
val length: Int |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
283 |
) { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
284 |
override def toString: String = "Bytes.Subarray(" + Space.bytes(length).print + ")" |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
285 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
286 |
def byte_iterator: Iterator[Byte] = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
287 |
if (length == 0) Iterator.empty |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
288 |
else { for (i <- (offset until (offset + length)).iterator) yield array(i) } |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
289 |
} |
54439 | 290 |
} |
291 |
||
292 |
final class Bytes private( |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
293 |
protected val chunks: Option[Array[Array[Byte]]], |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
294 |
protected val chunk0: Array[Byte], |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
295 |
protected val offset: Long, |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
296 |
val size: Long |
80480
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
297 |
) extends Bytes.Vec with YXML.Source { |
80485 | 298 |
bytes => |
299 |
||
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
300 |
assert( |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
301 |
(chunks.isEmpty || |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
302 |
chunks.get.nonEmpty && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
303 |
chunks.get.forall(chunk => chunk.length == Bytes.chunk_size)) && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
304 |
chunk0.length < Bytes.chunk_size) |
80361 | 305 |
|
80385 | 306 |
def small_size: Int = |
307 |
if (size > Bytes.array_size) throw new Bytes.Too_Large(size) |
|
308 |
else size.toInt |
|
309 |
||
80480
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
310 |
override def is_empty: Boolean = size == 0 |
80361 | 311 |
|
80485 | 312 |
def proper: Option[Bytes] = if (is_empty) None else Some(bytes) |
80385 | 313 |
|
80361 | 314 |
def is_sliced: Boolean = |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
315 |
offset != 0L || { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
316 |
chunks match { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
317 |
case None => size != chunk0.length |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
318 |
case Some(cs) => size != Bytes.make_size(cs, chunk0) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
319 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
320 |
} |
80361 | 321 |
|
322 |
override def toString: String = |
|
323 |
if (is_empty) "Bytes.empty" |
|
324 |
else "Bytes(" + Space.bytes(size).print + if_proper(is_sliced, ", sliced") + ")" |
|
325 |
||
326 |
||
327 |
/* elements: signed Byte or unsigned Char */ |
|
328 |
||
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
329 |
protected def byte_unchecked(i: Long): Byte = { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
330 |
val a = offset + i |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
331 |
chunks match { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
332 |
case None => chunk0(a.toInt) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
333 |
case Some(cs) => |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
334 |
val b = a % Bytes.chunk_size |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
335 |
val c = a / Bytes.chunk_size |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
336 |
if (c < cs.length) cs(c.toInt)(b.toInt) else chunk0(b.toInt) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
337 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
338 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
339 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
340 |
def byte(i: Long): Byte = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
341 |
if (0 <= i && i < size) byte_unchecked(i) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
342 |
else throw new IndexOutOfBoundsException |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
343 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
344 |
def apply(i: Long): Char = (byte(i).toInt & 0xff).toChar |
80361 | 345 |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
346 |
protected def subarray_iterator: Iterator[Bytes.Subarray] = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
347 |
if (is_empty) Iterator.empty |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
348 |
else if (chunks.isEmpty) Iterator(Bytes.Subarray(chunk0, offset.toInt, size.toInt)) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
349 |
else { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
350 |
val end_offset = offset + size |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
351 |
for ((array, index) <- (chunks.get.iterator ++ Iterator(chunk0)).zipWithIndex) yield { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
352 |
val array_start = Bytes.chunk_size * index |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
353 |
val array_stop = array_start + array.length |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
354 |
if (offset < array_stop && array_start < end_offset) { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
355 |
val i = (array_start max offset) - array_start |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
356 |
val j = (array_stop min end_offset) - array_start |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
357 |
Bytes.Subarray(array, i.toInt, (j - i).toInt) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
358 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
359 |
else Bytes.Subarray.empty |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
360 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
361 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
362 |
|
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
363 |
def byte_iterator: Iterator[Byte] = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
364 |
for { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
365 |
a <- subarray_iterator |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
366 |
b <- a.byte_iterator |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
367 |
} yield b |
80361 | 368 |
|
369 |
||
80385 | 370 |
/* slice */ |
371 |
||
372 |
def slice(i: Long, j: Long): Bytes = |
|
373 |
if (0 <= i && i <= j && j <= size) { |
|
374 |
if (i == j) Bytes.empty |
|
375 |
else new Bytes(chunks, chunk0, offset + i, j - i) |
|
376 |
} |
|
377 |
else throw new IndexOutOfBoundsException |
|
378 |
||
379 |
def unslice: Bytes = |
|
380 |
if (is_sliced) { |
|
381 |
Bytes.Builder.use(hint = size) { builder => |
|
382 |
for (a <- subarray_iterator) { builder += a } |
|
383 |
} |
|
384 |
} |
|
80485 | 385 |
else bytes |
80385 | 386 |
|
387 |
def trim_line: Bytes = |
|
388 |
if (size >= 2 && byte_unchecked(size - 2) == 13 && byte_unchecked(size - 1) == 10) { |
|
389 |
slice(0, size - 2) |
|
390 |
} |
|
391 |
else if (size >= 1 && (byte_unchecked(size - 1) == 13 || byte_unchecked(size - 1) == 10)) { |
|
392 |
slice(0, size - 1) |
|
393 |
} |
|
80485 | 394 |
else bytes |
80385 | 395 |
|
396 |
||
80479 | 397 |
/* separated chunks */ |
398 |
||
399 |
def separated_chunks(sep: Byte): Iterator[Bytes] = |
|
400 |
new Iterator[Bytes] { |
|
401 |
private var start: Long = -1 |
|
402 |
private var stop: Long = -1 |
|
403 |
||
404 |
private def end(i: Long): Long = { |
|
405 |
var j = i |
|
80485 | 406 |
while (j < bytes.size && byte_unchecked(j) != sep) { j += 1 } |
80479 | 407 |
j |
408 |
} |
|
409 |
||
410 |
// init |
|
80485 | 411 |
if (!bytes.is_empty) { start = 0; stop = end(0) } |
80479 | 412 |
|
413 |
def hasNext: Boolean = |
|
80485 | 414 |
0 <= start && start <= stop && stop <= bytes.size |
80479 | 415 |
|
416 |
def next(): Bytes = |
|
417 |
if (hasNext) { |
|
80485 | 418 |
val chunk = bytes.slice(start, stop) |
419 |
if (stop < bytes.size) { start = stop + 1; stop = end(start) } |
|
80479 | 420 |
else { start = -1; stop = -1 } |
421 |
chunk |
|
422 |
} |
|
423 |
else throw new NoSuchElementException |
|
424 |
} |
|
425 |
||
426 |
def space_explode(sep: Byte): List[Bytes] = separated_chunks(sep).toList |
|
427 |
||
428 |
def split_lines: List[Bytes] = space_explode(10) |
|
429 |
||
80480
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
430 |
// YXML.Source operations |
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
431 |
override def is_X: Boolean = size == 1 && byte_unchecked(0) == YXML.X_byte |
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
432 |
override def is_Y: Boolean = size == 1 && byte_unchecked(0) == YXML.Y_byte |
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
433 |
override def iterator_X: Iterator[Bytes] = separated_chunks(YXML.X_byte) |
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
434 |
override def iterator_Y: Iterator[Bytes] = separated_chunks(YXML.Y_byte) |
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
435 |
|
80479 | 436 |
|
80363
306f273c91ec
clarified hash and equality: depend on sha1 digest to be collision-free;
wenzelm
parents:
80362
diff
changeset
|
437 |
/* hash and equality */ |
306f273c91ec
clarified hash and equality: depend on sha1 digest to be collision-free;
wenzelm
parents:
80362
diff
changeset
|
438 |
|
306f273c91ec
clarified hash and equality: depend on sha1 digest to be collision-free;
wenzelm
parents:
80362
diff
changeset
|
439 |
lazy val sha1_digest: SHA1.Digest = |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
440 |
if (is_empty) SHA1.digest_empty |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
441 |
else { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
442 |
SHA1.make_digest { sha => |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
443 |
for (a <- subarray_iterator if a.length > 0) { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
444 |
sha.update(a.array, a.offset, a.length) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
445 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
446 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
447 |
} |
80363
306f273c91ec
clarified hash and equality: depend on sha1 digest to be collision-free;
wenzelm
parents:
80362
diff
changeset
|
448 |
|
306f273c91ec
clarified hash and equality: depend on sha1 digest to be collision-free;
wenzelm
parents:
80362
diff
changeset
|
449 |
override def hashCode(): Int = sha1_digest.hashCode() |
54439 | 450 |
|
75393 | 451 |
override def equals(that: Any): Boolean = { |
54440 | 452 |
that match { |
453 |
case other: Bytes => |
|
80485 | 454 |
if (bytes.eq(other)) true |
80363
306f273c91ec
clarified hash and equality: depend on sha1 digest to be collision-free;
wenzelm
parents:
80362
diff
changeset
|
455 |
else if (size != other.size) false |
80378
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
456 |
else { |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
457 |
if (chunks.isEmpty && other.chunks.isEmpty) { |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
458 |
Arrays.equals(chunk0, offset.toInt, (offset + size).toInt, |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
459 |
other.chunk0, other.offset.toInt, (other.offset + other.size).toInt) |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
460 |
} |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
461 |
else if (!is_sliced && !other.is_sliced) { |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
462 |
(subarray_iterator zip other.subarray_iterator) |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
463 |
.forall((a, b) => Arrays.equals(a.array, b.array)) |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
464 |
} |
ab4badc7db7f
more efficient equals: avoid somewhat slow sha1_digest (see also 29b761e290c5, 306f273c91ec);
wenzelm
parents:
80377
diff
changeset
|
465 |
else sha1_digest == other.sha1_digest |
80363
306f273c91ec
clarified hash and equality: depend on sha1 digest to be collision-free;
wenzelm
parents:
80362
diff
changeset
|
466 |
} |
54440 | 467 |
case _ => false |
468 |
} |
|
469 |
} |
|
470 |
||
54439 | 471 |
|
472 |
/* content */ |
|
473 |
||
80385 | 474 |
def + (other: Bytes): Bytes = |
80485 | 475 |
if (other.is_empty) bytes |
80385 | 476 |
else if (is_empty) other |
477 |
else { |
|
478 |
Bytes.Builder.use(hint = size + other.size) { builder => |
|
479 |
for (a <- subarray_iterator ++ other.subarray_iterator) { |
|
480 |
builder += a |
|
481 |
} |
|
482 |
} |
|
483 |
} |
|
484 |
||
80368 | 485 |
def make_array: Array[Byte] = { |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
486 |
val buf = new ByteArrayOutputStream(small_size) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
487 |
for (a <- subarray_iterator) { buf.write(a.array, a.offset, a.length) } |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
488 |
buf.toByteArray |
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
|
489 |
} |
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
|
490 |
|
80480
972f7a4cdc0e
clarified YXML.Source: more direct support for String and Bytes, instead of CharSequence;
wenzelm
parents:
80479
diff
changeset
|
491 |
override def text: String = |
80352 | 492 |
if (is_empty) "" |
80379
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
493 |
else { |
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
494 |
var i = 0L |
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
495 |
var utf8 = false |
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
496 |
while (i < size && !utf8) { |
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
497 |
if (byte_unchecked(i) < 0) { utf8 = true } |
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
498 |
i += 1 |
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
499 |
} |
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
500 |
utf8 |
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
501 |
|
80485 | 502 |
if (utf8) UTF8.decode_permissive_bytes(bytes) |
80379
1f1ce661c71c
notable performance tuning: avoid overhead of higher-order functions;
wenzelm
parents:
80378
diff
changeset
|
503 |
else new String(make_array, UTF8.charset) |
80352 | 504 |
} |
65279
fa62e095d8f1
clarified signature (again, see also 3ed43cfc8b14);
wenzelm
parents:
65070
diff
changeset
|
505 |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
506 |
def wellformed_text: Option[String] = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
507 |
try { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
508 |
val s = text |
80485 | 509 |
if (bytes == Bytes(s)) Some(s) else None |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
510 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
511 |
catch { case ERROR(_) => None } |
76236 | 512 |
|
80393
6138c5b803be
Base64: proper support for large Bytes, with subtle change of types (Bytes instead of String);
wenzelm
parents:
80389
diff
changeset
|
513 |
|
6138c5b803be
Base64: proper support for large Bytes, with subtle change of types (Bytes instead of String);
wenzelm
parents:
80389
diff
changeset
|
514 |
/* Base64 data representation */ |
6138c5b803be
Base64: proper support for large Bytes, with subtle change of types (Bytes instead of String);
wenzelm
parents:
80389
diff
changeset
|
515 |
|
80394 | 516 |
def encode_base64: Bytes = |
517 |
Bytes.Builder.use_stream(hint = (size * 1.5).round) { out => |
|
518 |
using(Base64.encode_stream(out))(write_stream(_)) |
|
519 |
} |
|
80393
6138c5b803be
Base64: proper support for large Bytes, with subtle change of types (Bytes instead of String);
wenzelm
parents:
80389
diff
changeset
|
520 |
|
6138c5b803be
Base64: proper support for large Bytes, with subtle change of types (Bytes instead of String);
wenzelm
parents:
80389
diff
changeset
|
521 |
def decode_base64: Bytes = |
6138c5b803be
Base64: proper support for large Bytes, with subtle change of types (Bytes instead of String);
wenzelm
parents:
80389
diff
changeset
|
522 |
using(Base64.decode_stream(stream()))(Bytes.read_stream(_, hint = (size / 1.2).round)) |
68094 | 523 |
|
76236 | 524 |
def maybe_encode_base64: (Boolean, String) = |
525 |
wellformed_text match { |
|
526 |
case Some(s) => (false, s) |
|
80393
6138c5b803be
Base64: proper support for large Bytes, with subtle change of types (Bytes instead of String);
wenzelm
parents:
80389
diff
changeset
|
527 |
case None => (true, encode_base64.text) |
76236 | 528 |
} |
68106 | 529 |
|
54440 | 530 |
|
64004 | 531 |
/* streams */ |
532 |
||
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
533 |
def stream(): InputStream = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
534 |
if (chunks.isEmpty) new ByteArrayInputStream(chunk0, offset.toInt, size.toInt) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
535 |
else { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
536 |
new InputStream { |
80367 | 537 |
private var index = 0L |
538 |
||
539 |
def read(): Int = { |
|
540 |
if (index < size) { |
|
541 |
val res = byte_unchecked(index).toInt & 0xff |
|
542 |
index += 1 |
|
543 |
res |
|
544 |
} |
|
545 |
else -1 |
|
546 |
} |
|
547 |
||
80388 | 548 |
override def read(buffer: Array[Byte], start: Int, length: Int): Int = { |
549 |
if (length < 16) super.read(buffer, start, length) |
|
550 |
else { |
|
551 |
val index0 = index |
|
552 |
index = (index + length) min size |
|
553 |
val n = (index - index0).toInt |
|
554 |
if (n == 0) -1 |
|
555 |
else { |
|
556 |
var i = start |
|
557 |
for (a <- slice(index0, index).subarray_iterator) { |
|
558 |
val l = a.length |
|
559 |
if (l > 0) { |
|
560 |
System.arraycopy(a.array, a.offset, buffer, i, l) |
|
561 |
i += l |
|
562 |
} |
|
563 |
} |
|
564 |
n |
|
565 |
} |
|
566 |
} |
|
567 |
} |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
568 |
} |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
569 |
} |
64004 | 570 |
|
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
571 |
def write_stream(stream: OutputStream): Unit = |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
572 |
for (a <- subarray_iterator if a.length > 0) { |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
573 |
stream.write(a.array, a.offset, a.length) |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
574 |
} |
64004 | 575 |
|
576 |
||
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
577 |
/* XZ / Zstd data compression */ |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
578 |
|
76358 | 579 |
def detect_xz: Boolean = |
80357
fe123d033e76
clarified signature: pro-forma support for Bytes with size: Long;
wenzelm
parents:
80356
diff
changeset
|
580 |
size >= 6 && |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
581 |
byte_unchecked(0) == 0xFD.toByte && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
582 |
byte_unchecked(1) == 0x37.toByte && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
583 |
byte_unchecked(2) == 0x7A.toByte && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
584 |
byte_unchecked(3) == 0x58.toByte && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
585 |
byte_unchecked(4) == 0x5A.toByte && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
586 |
byte_unchecked(5) == 0x00.toByte |
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
587 |
|
76358 | 588 |
def detect_zstd: Boolean = |
80357
fe123d033e76
clarified signature: pro-forma support for Bytes with size: Long;
wenzelm
parents:
80356
diff
changeset
|
589 |
size >= 4 && |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
590 |
byte_unchecked(0) == 0x28.toByte && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
591 |
byte_unchecked(1) == 0xB5.toByte && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
592 |
byte_unchecked(2) == 0x2F.toByte && |
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
593 |
byte_unchecked(3) == 0xFD.toByte |
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
594 |
|
76358 | 595 |
def uncompress_xz(cache: Compress.Cache = Compress.Cache.none): Bytes = |
80366
ac4d53bc8f6b
support large byte arrays, using multiple "chunks";
wenzelm
parents:
80364
diff
changeset
|
596 |
using(new xz.XZInputStream(stream(), cache.for_xz))(Bytes.read_stream(_, hint = size)) |
76358 | 597 |
|
598 |
def uncompress_zstd(cache: Compress.Cache = Compress.Cache.none): Bytes = { |
|
599 |
Zstd.init() |
|
80376 | 600 |
using(new zstd.ZstdInputStream(stream(), cache.for_zstd))(Bytes.read_stream(_, hint = size)) |
76358 | 601 |
} |
54440 | 602 |
|
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
603 |
def uncompress(cache: Compress.Cache = Compress.Cache.none): Bytes = |
76358 | 604 |
if (detect_xz) uncompress_xz(cache = cache) |
605 |
else if (detect_zstd) uncompress_zstd(cache = cache) |
|
606 |
else error("Cannot detect compression scheme") |
|
64004 | 607 |
|
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
608 |
def compress( |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
609 |
options: Compress.Options = Compress.Options(), |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
610 |
cache: Compress.Cache = Compress.Cache.none |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
611 |
): Bytes = { |
80394 | 612 |
Bytes.Builder.use_stream(hint = size) { out => |
613 |
using( |
|
614 |
options match { |
|
615 |
case options_xz: Compress.Options_XZ => |
|
616 |
new xz.XZOutputStream(out, options_xz.make, cache.for_xz) |
|
617 |
case options_zstd: Compress.Options_Zstd => |
|
618 |
new zstd.ZstdOutputStream(out, cache.for_zstd, options_zstd.level) |
|
619 |
} |
|
620 |
) { s => for (a <- subarray_iterator) s.write(a.array, a.offset, a.length) } |
|
621 |
} |
|
64004 | 622 |
} |
68167 | 623 |
|
75393 | 624 |
def maybe_compress( |
76351
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
625 |
options: Compress.Options = Compress.Options(), |
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
wenzelm
parents:
76350
diff
changeset
|
626 |
cache: Compress.Cache = Compress.Cache.none |
75393 | 627 |
) : (Boolean, Bytes) = { |
68167 | 628 |
val compressed = compress(options = options, cache = cache) |
80485 | 629 |
if (compressed.size < size) (true, compressed) else (false, bytes) |
68167 | 630 |
} |
54439 | 631 |
} |