@@ -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 }
0 commit comments