| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.tukaani.xz; |
| 6 |
|
| 7 |
import java.io.OutputStream; |
| 8 |
import java.io.ByteArrayOutputStream; |
| 9 |
import java.io.IOException; |
| 10 |
import org.tukaani.xz.common.EncoderUtil; |
| 11 |
import org.tukaani.xz.check.Check; |
| 12 |
|
| 13 |
class BlockOutputStream extends FinishableOutputStream { |
| 14 |
private final OutputStream out; |
| 15 |
private final CountingOutputStream outCounted; |
| 16 |
private FinishableOutputStream filterChain; |
| 17 |
private final Check check; |
| 18 |
|
| 19 |
private final int headerSize; |
| 20 |
private final long compressedSizeLimit; |
| 21 |
private long uncompressedSize = 0; |
| 22 |
|
| 23 |
private final byte[] tempBuf = new byte[1]; |
| 24 |
|
| 25 |
public BlockOutputStream(OutputStream out, FilterEncoder[] filters, |
| 26 |
Check check, ArrayCache arrayCache) |
| 27 |
throws IOException { |
| 28 |
this.out = out; |
| 29 |
this.check = check; |
| 30 |
|
| 31 |
|
| 32 |
outCounted = new CountingOutputStream(out); |
| 33 |
filterChain = outCounted; |
| 34 |
for (int i = filters.length - 1; i >= 0; --i) |
| 35 |
filterChain = filters[i].getOutputStream(filterChain, arrayCache); |
| 36 |
|
| 37 |
|
| 38 |
ByteArrayOutputStream bufStream = new ByteArrayOutputStream(); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
bufStream.write(0x00); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
bufStream.write(filters.length - 1); |
| 47 |
|
| 48 |
|
| 49 |
for (int i = 0; i < filters.length; ++i) { |
| 50 |
EncoderUtil.encodeVLI(bufStream, filters[i].getFilterID()); |
| 51 |
byte[] filterProps = filters[i].getFilterProps(); |
| 52 |
EncoderUtil.encodeVLI(bufStream, filterProps.length); |
| 53 |
bufStream.write(filterProps); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
while ((bufStream.size() & 3) != 0) |
| 58 |
bufStream.write(0x00); |
| 59 |
|
| 60 |
byte[] buf = bufStream.toByteArray(); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
headerSize = buf.length + 4; |
| 65 |
|
| 66 |
|
| 67 |
if (headerSize > EncoderUtil.BLOCK_HEADER_SIZE_MAX) |
| 68 |
throw new UnsupportedOptionsException(); |
| 69 |
|
| 70 |
|
| 71 |
buf[0] = (byte)(buf.length / 4); |
| 72 |
|
| 73 |
|
| 74 |
out.write(buf); |
| 75 |
EncoderUtil.writeCRC32(out, buf); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
compressedSizeLimit = (EncoderUtil.VLI_MAX & ~3) |
| 80 |
- headerSize - check.getSize(); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void write(int b) throws IOException { |
| 85 |
tempBuf[0] = (byte)b; |
| 86 |
write(tempBuf, 0, 1); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void write(byte[] buf, int off, int len) throws IOException { |
| 91 |
filterChain.write(buf, off, len); |
| 92 |
check.update(buf, off, len); |
| 93 |
uncompressedSize += len; |
| 94 |
validate(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void flush() throws IOException { |
| 99 |
filterChain.flush(); |
| 100 |
validate(); |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void finish() throws IOException { |
| 105 |
|
| 106 |
filterChain.finish(); |
| 107 |
validate(); |
| 108 |
|
| 109 |
|
| 110 |
for (long i = outCounted.getSize(); (i & 3) != 0; ++i) |
| 111 |
out.write(0x00); |
| 112 |
|
| 113 |
|
| 114 |
out.write(check.finish()); |
| 115 |
} |
| 116 |
|
| 117 |
private void validate() throws IOException { |
| 118 |
long compressedSize = outCounted.getSize(); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if (compressedSize < 0 || compressedSize > compressedSizeLimit |
| 123 |
|| uncompressedSize < 0) |
| 124 |
throw new XZIOException("XZ Stream has grown too big"); |
| 125 |
} |
| 126 |
|
| 127 |
public long getUnpaddedSize() { |
| 128 |
return headerSize + outCounted.getSize() + check.getSize(); |
| 129 |
} |
| 130 |
|
| 131 |
public long getUncompressedSize() { |
| 132 |
return uncompressedSize; |
| 133 |
} |
| 134 |
} |