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