Skip to content

Commit 731f995

Browse files
committed
Checkpoint.
1 parent 434d8bc commit 731f995

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/main/java/com/stringcompressor/FourBitAsciiCompressor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final byte[] compress(byte[] str) {
4242
encode(str, len);
4343

4444
final int halfLen = len >> 1;
45-
final byte[] compressed = new byte[halfLen + (len & 1) + 1];
45+
final byte[] compressed = new byte[halfLen + (len & 1) + (-len >>> 31)];
4646

4747
for (int i = 0; i < halfLen; i++)
4848
compressed[i] = (byte) (str[i << 1] << 4 | str[(i << 1) + 1]);
@@ -61,6 +61,10 @@ public final byte[] compress(byte[] str) {
6161
@Override
6262
public final byte[] decompress(final byte[] compressed) {
6363
int cLen = compressed.length - 1;
64+
65+
if (cLen <= 0)
66+
return new byte[0];
67+
6468
final int odd = compressed[cLen];
6569
final int dLen = odd == 1 ? (--cLen << 1) + 1 : cLen << 1;
6670
final byte[] decompressed = new byte[dLen];

0 commit comments

Comments
 (0)