equal
deleted
inserted
replaced
39 } |
39 } |
40 |
40 |
41 |
41 |
42 /* read */ |
42 /* read */ |
43 |
43 |
44 def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE): Bytes = |
44 def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE, hint: Int = 1024): Bytes = |
45 if (limit == 0) empty |
45 if (limit == 0) empty |
46 else { |
46 else { |
47 val out = new ByteArrayOutputStream(if (limit == Integer.MAX_VALUE) 1024 else limit) |
47 val out = new ByteArrayOutputStream(if (limit == Integer.MAX_VALUE) hint else limit) |
48 val buf = new Array[Byte](1024) |
48 val buf = new Array[Byte](8192) |
49 var m = 0 |
49 var m = 0 |
50 |
50 |
51 do { |
51 do { |
52 m = stream.read(buf, 0, buf.size min (limit - out.size)) |
52 m = stream.read(buf, 0, buf.size min (limit - out.size)) |
53 if (m != -1) out.write(buf, 0, m) |
53 if (m != -1) out.write(buf, 0, m) |
131 def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
131 def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length) |
132 |
132 |
133 |
133 |
134 /* XZ data compression */ |
134 /* XZ data compression */ |
135 |
135 |
136 def uncompress(): Bytes = using(new XZInputStream(stream()))(Bytes.read_stream(_)) |
136 def uncompress(): Bytes = |
|
137 using(new XZInputStream(stream()))(Bytes.read_stream(_, hint = length)) |
137 |
138 |
138 def compress(options: XZ.Options = XZ.options()): Bytes = |
139 def compress(options: XZ.Options = XZ.options()): Bytes = |
139 { |
140 { |
140 val result = new ByteArrayOutputStream(length) |
141 val result = new ByteArrayOutputStream(length) |
141 using(new XZOutputStream(result, options))(write_stream(_)) |
142 using(new XZOutputStream(result, options))(write_stream(_)) |