| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package org.apache.commons.compress.compressors.xz; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.io.InputStream; |
| 23 |
|
| 24 |
import org.apache.commons.compress.MemoryLimitException; |
| 25 |
import org.apache.commons.compress.compressors.CompressorInputStream; |
| 26 |
import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream; |
| 27 |
import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream; |
| 28 |
import org.apache.commons.compress.utils.InputStreamStatistics; |
| 29 |
import org.apache.commons.io.build.AbstractStreamBuilder; |
| 30 |
import org.apache.commons.io.input.BoundedInputStream; |
| 31 |
import org.tukaani.xz.LZMA2Options; |
| 32 |
import org.tukaani.xz.SingleXZInputStream; |
| 33 |
import org.tukaani.xz.XZ; |
| 34 |
import org.tukaani.xz.XZInputStream; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class XZCompressorInputStream extends CompressorInputStream implements InputStreamStatistics { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static class Builder extends AbstractStreamBuilder<XZCompressorInputStream, Builder> { |
| 77 |
|
| 78 |
private int memoryLimitKiB = -1; |
| 79 |
private boolean decompressConcatenated; |
| 80 |
|
| 81 |
@Override |
| 82 |
public XZCompressorInputStream get() throws IOException { |
| 83 |
return new XZCompressorInputStream(this); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public Builder setDecompressConcatenated(final boolean decompressConcatenated) { |
| 94 |
this.decompressConcatenated = decompressConcatenated; |
| 95 |
return this; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public Builder setMemoryLimitKiB(final int memoryLimitKiB) { |
| 107 |
this.memoryLimitKiB = memoryLimitKiB; |
| 108 |
return this; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public static Builder builder() { |
| 119 |
return new Builder(); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static boolean matches(final byte[] signature, final int length) { |
| 131 |
if (length < XZ.HEADER_MAGIC.length) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
for (int i = 0; i < XZ.HEADER_MAGIC.length; ++i) { |
| 136 |
if (signature[i] != XZ.HEADER_MAGIC[i]) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
private final BoundedInputStream countingStream; |
| 145 |
|
| 146 |
private final InputStream in; |
| 147 |
|
| 148 |
@SuppressWarnings("resource") |
| 149 |
private XZCompressorInputStream(final Builder builder) throws IOException { |
| 150 |
countingStream = BoundedInputStream.builder().setInputStream(builder.getInputStream()).get(); |
| 151 |
if (builder.decompressConcatenated) { |
| 152 |
in = new XZInputStream(countingStream, builder.memoryLimitKiB); |
| 153 |
} else { |
| 154 |
in = new SingleXZInputStream(countingStream, builder.memoryLimitKiB); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public XZCompressorInputStream(final InputStream inputStream) throws IOException { |
| 166 |
this(builder().setInputStream(inputStream)); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@Deprecated |
| 181 |
public XZCompressorInputStream(final InputStream inputStream, final boolean decompressConcatenated) throws IOException { |
| 182 |
this(builder().setInputStream(inputStream).setDecompressConcatenated(decompressConcatenated)); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
@Deprecated |
| 202 |
public XZCompressorInputStream(final InputStream inputStream, final boolean decompressConcatenated, final int memoryLimitKiB) throws IOException { |
| 203 |
this(builder().setInputStream(inputStream).setDecompressConcatenated(decompressConcatenated).setMemoryLimitKiB(memoryLimitKiB)); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
@Override |
| 208 |
public int available() throws IOException { |
| 209 |
return in.available(); |
| 210 |
} |
| 211 |
|
| 212 |
@Override |
| 213 |
public void close() throws IOException { |
| 214 |
in.close(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
@Override |
| 221 |
public long getCompressedCount() { |
| 222 |
return countingStream.getCount(); |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public int read() throws IOException { |
| 227 |
try { |
| 228 |
final int ret = in.read(); |
| 229 |
count(ret == -1 ? -1 : 1); |
| 230 |
return ret; |
| 231 |
} catch (final org.tukaani.xz.MemoryLimitException e) { |
| 232 |
throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), (Throwable) e); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public int read(final byte[] buf, final int off, final int len) throws IOException { |
| 238 |
if (len == 0) { |
| 239 |
return 0; |
| 240 |
} |
| 241 |
try { |
| 242 |
final int ret = in.read(buf, off, len); |
| 243 |
count(ret); |
| 244 |
return ret; |
| 245 |
} catch (final org.tukaani.xz.MemoryLimitException e) { |
| 246 |
|
| 247 |
throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), (Throwable) e); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public long skip(final long n) throws IOException { |
| 253 |
try { |
| 254 |
return org.apache.commons.io.IOUtils.skip(in, n); |
| 255 |
} catch (final org.tukaani.xz.MemoryLimitException e) { |
| 256 |
|
| 257 |
throw new MemoryLimitException(e.getMemoryNeeded(), e.getMemoryLimit(), (Throwable) e); |
| 258 |
} |
| 259 |
} |
| 260 |
} |