Skip to content

Commit 568ecdb

Browse files
committed
Tweaks.
1 parent a9c1e1c commit 568ecdb

File tree

3 files changed

+22
-47
lines changed

3 files changed

+22
-47
lines changed

src/main/java/com/stringcompressor/FiveBitAsciiCompressor.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ public FiveBitAsciiCompressor(byte[] supportedCharset) {
2626
super(supportedCharset);
2727
}
2828

29-
public FiveBitAsciiCompressor(byte[] supportedCharset, boolean throwException) {
30-
super(supportedCharset);
31-
this.throwException = throwException;
32-
}
33-
3429
public FiveBitAsciiCompressor(boolean throwException) {
3530
super(DEFAULT_5BIT_CHARSET);
3631
this.throwException = throwException;
@@ -51,13 +46,10 @@ public final byte[] compress(byte[] str) {
5146
if (preserveOriginal)
5247
str = str.clone();
5348

54-
if (len == 0)
55-
return str;
56-
5749
encode(str, len);
5850

5951
final int compressedLen = len * 5 + 7 >>> 3;
60-
final byte[] compressed = new byte[compressedLen + 1];
52+
final byte[] compressed = new byte[compressedLen + (-len >>> 31)];
6153
int buffer = 0;
6254
int bitsInBuffer = 0;
6355
int j = 0;

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ public FourBitAsciiCompressor(byte[] supportedCharset) {
1919
super(supportedCharset);
2020
}
2121

22-
public FourBitAsciiCompressor(byte[] supportedCharset, boolean throwException) {
23-
super(supportedCharset);
24-
this.throwException = throwException;
25-
}
26-
2722
public FourBitAsciiCompressor(boolean throwException) {
2823
super(DEFAULT_4BIT_CHARSET);
2924
this.throwException = throwException;
@@ -38,25 +33,16 @@ public FourBitAsciiCompressor(boolean throwException) {
3833
* @return A compressed byte array.
3934
*/
4035
@Override
41-
public byte[] compress(byte[] str) {
42-
int len = str.length;
36+
public final byte[] compress(byte[] str) {
37+
final int len = str.length;
4338

44-
if (preserveOriginal) {
45-
byte[] temp = new byte[len];
46-
System.arraycopy(str, 0, temp, 0, len);
47-
str = temp;
48-
}
39+
if (preserveOriginal)
40+
str = str.clone();
4941

5042
encode(str, len);
5143

52-
// This is the bit pattern applied by the algorithm:
53-
// 0000 0001
54-
// 0010 0011
55-
// 0100 0101
56-
// ...
57-
58-
int halfLen = len >> 1;
59-
byte[] compressed = new byte[halfLen + (len & 1) + 1];
44+
final int halfLen = len >> 1;
45+
final byte[] compressed = new byte[halfLen + (len & 1) + (-len >>> 31)];
6046

6147
for (int i = 0; i < halfLen; i++)
6248
compressed[i] = (byte) (str[i << 1] << 4 | str[(i << 1) + 1]);
@@ -73,20 +59,25 @@ public byte[] compress(byte[] str) {
7359
* {@inheritDoc}
7460
*/
7561
@Override
76-
public byte[] decompress(byte[] compressed) {
77-
int cLen = compressed.length - 1;
78-
int odd = compressed[cLen];
79-
int dLen = odd == 1 ? (--cLen << 1) + 1 : cLen << 1;
80-
byte[] decompressed = new byte[dLen];
81-
82-
for (int i = 0, j = 0; i < cLen; i++) {
83-
byte bite = compressed[i];
62+
public final byte[] decompress(final byte[] compressed) {
63+
final int compressedLen = compressed.length - 1;
64+
65+
if (compressedLen <= 0)
66+
return new byte[0];
67+
68+
int cLenMinus = compressed.length - 1;
69+
final int odd = compressed[cLenMinus];
70+
final int dLen = odd == 1 ? (--cLenMinus << 1) + 1 : cLenMinus << 1;
71+
final byte[] decompressed = new byte[dLen];
72+
73+
for (int i = 0, j = 0; i < cLenMinus; i++) {
74+
final byte bite = compressed[i];
8475
decompressed[j++] = supportedCharset[(bite & 0xF0) >> 4];
8576
decompressed[j++] = supportedCharset[bite & 0x0F];
8677
}
8778

8879
if (odd == 1)
89-
decompressed[dLen - 1] = supportedCharset[compressed[cLen]];
80+
decompressed[dLen - 1] = supportedCharset[compressed[cLenMinus]];
9081

9182
return decompressed;
9283
}

src/main/java/com/stringcompressor/SixBitAsciiCompressor.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ public SixBitAsciiCompressor(byte[] supportedCharset) {
3434
super(supportedCharset);
3535
}
3636

37-
public SixBitAsciiCompressor(byte[] supportedCharset, boolean throwException) {
38-
super(supportedCharset);
39-
this.throwException = throwException;
40-
}
41-
4237
public SixBitAsciiCompressor(boolean throwException) {
4338
super(DEFAULT_6BIT_CHARSET);
4439
this.throwException = throwException;
@@ -59,13 +54,10 @@ public final byte[] compress(byte[] str) {
5954
if (preserveOriginal)
6055
str = str.clone();
6156

62-
if (len == 0)
63-
return str;
64-
6557
encode(str, len);
6658

6759
final int compressedLen = len * 6 + 7 >>> 3;
68-
final byte[] compressed = new byte[compressedLen + 1];
60+
final byte[] compressed = new byte[compressedLen + (-len >>> 31)];
6961
int buffer = 0;
7062
int bitsInBuffer = 0;
7163
int j = 0;

0 commit comments

Comments
 (0)