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.z;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.ByteOrder;
24
25 import org.apache.commons.compress.compressors.lzw.LZWInputStream;
26
27
28
29
30
31
32
33 public class ZCompressorInputStream extends LZWInputStream {
34 private static final int MAGIC_1 = 0x1f;
35 private static final int MAGIC_2 = 0x9d;
36 private static final int BLOCK_MODE_MASK = 0x80;
37 private static final int MAX_CODE_SIZE_MASK = 0x1f;
38
39
40
41
42
43
44
45
46
47
48 public static boolean matches(final byte[] signature, final int length) {
49 return length > 3 && signature[0] == MAGIC_1 && signature[1] == (byte) MAGIC_2;
50 }
51
52 private final boolean blockMode;
53 private final int maxCodeSize;
54
55 private long totalCodesRead;
56
57 public ZCompressorInputStream(final InputStream inputStream) throws IOException {
58 this(inputStream, -1);
59 }
60
61 public ZCompressorInputStream(final InputStream inputStream, final int memoryLimitInKb) throws IOException {
62 super(inputStream, ByteOrder.LITTLE_ENDIAN);
63 final int firstByte = (int) in.readBits(8);
64 final int secondByte = (int) in.readBits(8);
65 final int thirdByte = (int) in.readBits(8);
66 if (firstByte != MAGIC_1 || secondByte != MAGIC_2 || thirdByte < 0) {
67 throw new IOException("Input is not in .Z format");
68 }
69 blockMode = (thirdByte & BLOCK_MODE_MASK) != 0;
70 maxCodeSize = thirdByte & MAX_CODE_SIZE_MASK;
71 if (blockMode) {
72 setClearCode(DEFAULT_CODE_SIZE);
73 }
74 initializeTables(maxCodeSize, memoryLimitInKb);
75 clearEntries();
76 }
77
78
79
80
81
82
83
84
85 @Override
86 protected int addEntry(final int previousCode, final byte character) throws IOException {
87 final int maxTableSize = 1 << getCodeSize();
88 final int r = addEntry(previousCode, character, maxTableSize);
89 if (getTableSize() == maxTableSize && getCodeSize() < maxCodeSize) {
90 reAlignReading();
91 incrementCodeSize();
92 }
93 return r;
94 }
95
96 private void clearEntries() {
97 setTableSize((1 << 8) + (blockMode ? 1 : 0));
98 }
99
100
101
102
103
104
105
106
107 @Override
108 protected int decompressNextSymbol() throws IOException {
109
110
111
112
113
114
115
116
117
118
119
120
121 final int code = readNextCode();
122 if (code < 0) {
123 return -1;
124 }
125 if (blockMode && code == getClearCode()) {
126 clearEntries();
127 reAlignReading();
128 resetCodeSize();
129 resetPreviousCode();
130 return 0;
131 }
132 boolean addedUnfinishedEntry = false;
133 if (code == getTableSize()) {
134 addRepeatOfPreviousCode();
135 addedUnfinishedEntry = true;
136 } else if (code > getTableSize()) {
137 throw new IOException(String.format("Invalid %d bit code 0x%x", getCodeSize(), code));
138 }
139 return expandCodeToOutputStack(code, addedUnfinishedEntry);
140 }
141
142
143
144
145
146
147
148
149 @Override
150 protected int readNextCode() throws IOException {
151 final int code = super.readNextCode();
152 if (code >= 0) {
153 ++totalCodesRead;
154 }
155 return code;
156 }
157
158 private void reAlignReading() throws IOException {
159
160
161
162
163 long codeReadsToThrowAway = 8 - totalCodesRead % 8;
164 if (codeReadsToThrowAway == 8) {
165 codeReadsToThrowAway = 0;
166 }
167 for (long i = 0; i < codeReadsToThrowAway; i++) {
168 readNextCode();
169 }
170 in.clearBitCache();
171 }
172
173 }