src/Pure/General/bytes.scala
changeset 73024 337e1b135d2f
parent 72885 1b0f81e556a2
child 73414 7411d71b9fb8
equal deleted inserted replaced
73023:e15621aa8c72 73024:337e1b135d2f
   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   }