| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz; |
| 7 |
|
| 8 |
import java.io.OutputStream; |
| 9 |
import java.io.IOException; |
| 10 |
import org.tukaani.xz.lz.LZEncoder; |
| 11 |
import org.tukaani.xz.rangecoder.RangeEncoderToStream; |
| 12 |
import org.tukaani.xz.lzma.LZMAEncoder; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class LZMAOutputStream extends FinishableOutputStream { |
| 20 |
private OutputStream out; |
| 21 |
|
| 22 |
private final ArrayCache arrayCache; |
| 23 |
|
| 24 |
private LZEncoder lz; |
| 25 |
private final RangeEncoderToStream rc; |
| 26 |
private LZMAEncoder lzma; |
| 27 |
|
| 28 |
private final int props; |
| 29 |
private final boolean useEndMarker; |
| 30 |
private final long expectedUncompressedSize; |
| 31 |
private long currentUncompressedSize = 0; |
| 32 |
|
| 33 |
private boolean finished = false; |
| 34 |
private IOException exception = null; |
| 35 |
|
| 36 |
private final byte[] tempBuf = new byte[1]; |
| 37 |
|
| 38 |
private LZMAOutputStream(OutputStream out, LZMA2Options options, |
| 39 |
boolean useHeader, boolean useEndMarker, |
| 40 |
long expectedUncompressedSize, |
| 41 |
ArrayCache arrayCache) |
| 42 |
throws IOException { |
| 43 |
if (out == null) |
| 44 |
throw new NullPointerException(); |
| 45 |
|
| 46 |
|
| 47 |
if (expectedUncompressedSize < -1) |
| 48 |
throw new IllegalArgumentException( |
| 49 |
"Invalid expected input size (less than -1)"); |
| 50 |
|
| 51 |
this.useEndMarker = useEndMarker; |
| 52 |
this.expectedUncompressedSize = expectedUncompressedSize; |
| 53 |
|
| 54 |
this.arrayCache = arrayCache; |
| 55 |
|
| 56 |
this.out = out; |
| 57 |
rc = new RangeEncoderToStream(out); |
| 58 |
|
| 59 |
int dictSize = options.getDictSize(); |
| 60 |
lzma = LZMAEncoder.getInstance(rc, |
| 61 |
options.getLc(), options.getLp(), options.getPb(), |
| 62 |
options.getMode(), |
| 63 |
dictSize, 0, options.getNiceLen(), |
| 64 |
options.getMatchFinder(), options.getDepthLimit(), |
| 65 |
arrayCache); |
| 66 |
|
| 67 |
lz = lzma.getLZEncoder(); |
| 68 |
|
| 69 |
byte[] presetDict = options.getPresetDict(); |
| 70 |
if (presetDict != null && presetDict.length > 0) { |
| 71 |
if (useHeader) |
| 72 |
throw new UnsupportedOptionsException( |
| 73 |
"Preset dictionary cannot be used in .lzma files " |
| 74 |
+ "(try a raw LZMA stream instead)"); |
| 75 |
|
| 76 |
lz.setPresetDict(dictSize, presetDict); |
| 77 |
} |
| 78 |
|
| 79 |
props = (options.getPb() * 5 + options.getLp()) * 9 + options.getLc(); |
| 80 |
|
| 81 |
if (useHeader) { |
| 82 |
|
| 83 |
out.write(props); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
for (int i = 0; i < 4; ++i) { |
| 88 |
out.write(dictSize & 0xFF); |
| 89 |
dictSize >>>= 8; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
for (int i = 0; i < 8; ++i) |
| 96 |
out.write((int)(expectedUncompressedSize >>> (8 * i)) & 0xFF); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public LZMAOutputStream(OutputStream out, LZMA2Options options, |
| 123 |
long inputSize) |
| 124 |
throws IOException { |
| 125 |
this(out, options, inputSize, ArrayCache.getDefaultCache()); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public LZMAOutputStream(OutputStream out, LZMA2Options options, |
| 151 |
long inputSize, ArrayCache arrayCache) |
| 152 |
throws IOException { |
| 153 |
this(out, options, true, inputSize == -1, inputSize, arrayCache); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public LZMAOutputStream(OutputStream out, LZMA2Options options, |
| 176 |
boolean useEndMarker) throws IOException { |
| 177 |
this(out, options, useEndMarker, ArrayCache.getDefaultCache()); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public LZMAOutputStream(OutputStream out, LZMA2Options options, |
| 203 |
boolean useEndMarker, ArrayCache arrayCache) |
| 204 |
throws IOException { |
| 205 |
this(out, options, false, useEndMarker, -1, arrayCache); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public int getProps() { |
| 214 |
return props; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public long getUncompressedSize() { |
| 223 |
return currentUncompressedSize; |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public void write(int b) throws IOException { |
| 228 |
tempBuf[0] = (byte)b; |
| 229 |
write(tempBuf, 0, 1); |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public void write(byte[] buf, int off, int len) throws IOException { |
| 234 |
if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length) |
| 235 |
throw new IndexOutOfBoundsException(); |
| 236 |
|
| 237 |
if (exception != null) |
| 238 |
throw exception; |
| 239 |
|
| 240 |
if (finished) |
| 241 |
throw new XZIOException("Stream finished or closed"); |
| 242 |
|
| 243 |
if (expectedUncompressedSize != -1 |
| 244 |
&& expectedUncompressedSize - currentUncompressedSize < len) |
| 245 |
throw new XZIOException("Expected uncompressed input size (" |
| 246 |
+ expectedUncompressedSize + " bytes) was exceeded"); |
| 247 |
|
| 248 |
currentUncompressedSize += len; |
| 249 |
|
| 250 |
try { |
| 251 |
while (len > 0) { |
| 252 |
int used = lz.fillWindow(buf, off, len); |
| 253 |
off += used; |
| 254 |
len -= used; |
| 255 |
lzma.encodeForLZMA1(); |
| 256 |
} |
| 257 |
} catch (IOException e) { |
| 258 |
exception = e; |
| 259 |
throw e; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
@Override |
| 267 |
public void flush() throws IOException { |
| 268 |
throw new XZIOException("LZMAOutputStream does not support flushing"); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
@Override |
| 275 |
public void finish() throws IOException { |
| 276 |
if (!finished) { |
| 277 |
if (exception != null) |
| 278 |
throw exception; |
| 279 |
|
| 280 |
try { |
| 281 |
if (expectedUncompressedSize != -1 |
| 282 |
&& expectedUncompressedSize != currentUncompressedSize) |
| 283 |
throw new XZIOException("Expected uncompressed size (" |
| 284 |
+ expectedUncompressedSize + ") doesn't equal " |
| 285 |
+ "the number of bytes written to the stream (" |
| 286 |
+ currentUncompressedSize + ")"); |
| 287 |
|
| 288 |
lz.setFinishing(); |
| 289 |
lzma.encodeForLZMA1(); |
| 290 |
|
| 291 |
if (useEndMarker) |
| 292 |
lzma.encodeLZMA1EndMarker(); |
| 293 |
|
| 294 |
rc.finish(); |
| 295 |
} catch (IOException e) { |
| 296 |
exception = e; |
| 297 |
throw e; |
| 298 |
} |
| 299 |
|
| 300 |
finished = true; |
| 301 |
|
| 302 |
lzma.putArraysToCache(arrayCache); |
| 303 |
lzma = null; |
| 304 |
lz = null; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
@Override |
| 312 |
public void close() throws IOException { |
| 313 |
if (out != null) { |
| 314 |
try { |
| 315 |
finish(); |
| 316 |
} catch (IOException e) {} |
| 317 |
|
| 318 |
try { |
| 319 |
out.close(); |
| 320 |
} catch (IOException e) { |
| 321 |
if (exception == null) |
| 322 |
exception = e; |
| 323 |
} |
| 324 |
|
| 325 |
out = null; |
| 326 |
} |
| 327 |
|
| 328 |
if (exception != null) |
| 329 |
throw exception; |
| 330 |
} |
| 331 |
} |