193 def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
193 def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
194 |
194 |
195 |
195 |
196 /* XZ data compression */ |
196 /* XZ data compression */ |
197 |
197 |
198 def uncompress(cache: XZ.Cache = XZ.cache()): Bytes = |
198 def uncompress(cache: XZ.Cache = XZ.Cache()): Bytes = |
199 using(new XZInputStream(stream(), cache))(Bytes.read_stream(_, hint = length)) |
199 using(new XZInputStream(stream(), cache))(Bytes.read_stream(_, hint = length)) |
200 |
200 |
201 def compress(options: XZ.Options = XZ.options(), cache: XZ.Cache = XZ.cache()): Bytes = |
201 def compress(options: XZ.Options = XZ.options(), cache: XZ.Cache = XZ.Cache()): Bytes = |
202 { |
202 { |
203 val result = new ByteArrayOutputStream(length) |
203 val result = new ByteArrayOutputStream(length) |
204 using(new XZOutputStream(result, options, cache))(write_stream(_)) |
204 using(new XZOutputStream(result, options, cache))(write_stream(_)) |
205 new Bytes(result.toByteArray, 0, result.size) |
205 new Bytes(result.toByteArray, 0, result.size) |
206 } |
206 } |
207 |
207 |
208 def maybe_compress(options: XZ.Options = XZ.options(), cache: XZ.Cache = XZ.cache()) |
208 def maybe_compress(options: XZ.Options = XZ.options(), cache: XZ.Cache = XZ.Cache()) |
209 : (Boolean, Bytes) = |
209 : (Boolean, Bytes) = |
210 { |
210 { |
211 val compressed = compress(options = options, cache = cache) |
211 val compressed = compress(options = options, cache = cache) |
212 if (compressed.length < length) (true, compressed) else (false, this) |
212 if (compressed.length < length) (true, compressed) else (false, this) |
213 } |
213 } |