Skip to content

Commit f32fb68

Browse files
authored
chore: Refactor (#5)
1 parent ebf13b4 commit f32fb68

File tree

8 files changed

+138
-143
lines changed

8 files changed

+138
-143
lines changed

src/main/java/prog/lzw/Lunzipping.java

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package prog.lzw;
22

3+
import prog.util.CommonUtil;
34
import java.io.DataInputStream;
45
import java.io.DataOutputStream;
56
import java.io.EOFException;
@@ -52,38 +53,6 @@ public static void pre() {
5253
* =========================================================================
5354
*/
5455

55-
/*
56-
* =========================================================================
57-
* = byte to int conversion
58-
* =========================================================================
59-
*/
60-
public static int btoi(Byte bt) {
61-
int ret = bt;
62-
if (ret < 0)
63-
ret += 256;
64-
return ret;
65-
66-
}
67-
68-
/** ====================================================================== */
69-
70-
/*
71-
* =========================================================================
72-
* = byte to int conversion
73-
* =========================================================================
74-
*/
75-
public static int stoi(String s) {
76-
int ret = 0, i;
77-
for (i = 0; i < s.length(); i++) {
78-
ret *= 2;
79-
if (s.charAt(i) == '1')
80-
ret++;
81-
}
82-
return ret;
83-
}
84-
85-
/** ====================================================================== */
86-
8756
public static void Lunzip(String fileis) {
8857
int k;
8958
int dictSize = 256;
@@ -110,7 +79,7 @@ public static void Lunzip(String fileis) {
11079
while (true) {
11180
try {
11281
c = data_in.readByte();
113-
big1 += bttost[btoi(c)];
82+
big1 += bttost[CommonUtil.byteToUnsignedInt(c)];
11483
if (big1.length() >= bitsz1)
11584
break;
11685
} catch (EOFException eof) {
@@ -120,7 +89,7 @@ public static void Lunzip(String fileis) {
12089
}
12190

12291
if (big1.length() >= bitsz1) {
123-
k = stoi(big1.substring(0, bitsz1));
92+
k = CommonUtil.binaryStringToInt(big1.substring(0, bitsz1));
12493
big1 = big1.substring(bitsz1, big1.length());
12594
} else {
12695
data_in.close();
@@ -137,9 +106,9 @@ public static void Lunzip(String fileis) {
137106
try {
138107
while (big1.length() < bitsz1) {
139108
c = data_in.readByte();
140-
big1 += bttost[btoi(c)];
109+
big1 += bttost[CommonUtil.byteToUnsignedInt(c)];
141110
}
142-
k = stoi(big1.substring(0, bitsz1));
111+
k = CommonUtil.binaryStringToInt(big1.substring(0, bitsz1));
143112
big1 = big1.substring(bitsz1, big1.length());
144113

145114
String entry = "";

src/main/java/prog/lzw/Lzipping.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package prog.lzw;
22

3+
import prog.util.CommonUtil;
34
import java.io.DataInputStream;
45
import java.io.DataOutputStream;
56
import java.io.EOFException;
@@ -15,14 +16,6 @@ public class Lzipping {
1516
public static int btsz;
1617
public static String big;
1718

18-
public static int btoi(Byte bt) {
19-
int ret = bt;
20-
if (ret < 0) {
21-
ret += 256;
22-
}
23-
return ret;
24-
25-
}
2619

2720
/*
2821
* =========================================================================
@@ -105,7 +98,7 @@ public static void precalc(String fileis) {
10598
while (true) {
10699
try {
107100
c = data_in.readByte();
108-
ch = btoi(c);
101+
ch = CommonUtil.byteToUnsignedInt(c);
109102
String wc = w + (char) ch;
110103
if (dictionary.containsKey(wc))
111104
w = wc;
@@ -178,7 +171,7 @@ public static void Lamzip(String fileis) {
178171
while (true) {
179172
try {
180173
c = data_in.readByte();
181-
ch = btoi(c);
174+
ch = CommonUtil.byteToUnsignedInt(c);
182175

183176
String wc = w + (char) ch;
184177
if (dictionary.containsKey(wc))

src/main/java/prog/lzw/LzwUtils.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package prog.util;
2+
3+
/**
4+
* Common utility class for byte and binary string conversions.
5+
*
6+
* This utility class provides essential conversion methods used throughout the
7+
* file compression application, particularly for the LZW compression algorithm.
8+
* It handles the conversion between different data representations that are
9+
* crucial for encoding and decoding compressed data.
10+
*
11+
*/
12+
public class CommonUtil {
13+
14+
/**
15+
* Converts a signed byte to an unsigned integer value (0-255).
16+
*
17+
* In Java, bytes are signed (-128 to 127), but in many compression algorithms,
18+
* we need to treat bytes as unsigned values (0 to 255). This method performs
19+
* that conversion by adding 256 to negative values.
20+
*
21+
* Example usage:
22+
* - byteToUnsignedInt((byte) 65) returns 65 (ASCII 'A')
23+
* - byteToUnsignedInt((byte) -1) returns 255
24+
* - byteToUnsignedInt((byte) -128) returns 128
25+
*
26+
* @param byteValue The signed byte value to convert
27+
* @return The unsigned integer representation (0-255)
28+
*/
29+
public static int byteToUnsignedInt(Byte byteValue) {
30+
int result = byteValue;
31+
if (result < 0) {
32+
result += 256;
33+
}
34+
return result;
35+
}
36+
37+
/**
38+
* Converts a binary string to its decimal integer representation.
39+
*
40+
* This method parses a string containing only '0' and '1' characters and
41+
* converts it to its corresponding decimal integer value. It's commonly used
42+
* in the decompression process to convert binary encoded data back to integers.
43+
*
44+
* The conversion is performed from left to right (most significant bit first).
45+
*
46+
* Example usage:
47+
* - binaryStringToInt("0") returns 0
48+
* - binaryStringToInt("1010") returns 10
49+
* - binaryStringToInt("11111111") returns 255
50+
*
51+
* @param binaryString The binary string to convert (must contain only '0' and '1')
52+
* @return The decimal integer representation of the binary string
53+
* @throws IllegalArgumentException if the string contains non-binary characters
54+
*/
55+
public static int binaryStringToInt(String binaryString) {
56+
int result = 0;
57+
for (int i = 0; i < binaryString.length(); i++) {
58+
result *= 2;
59+
char bit = binaryString.charAt(i);
60+
if (bit == '1') {
61+
result++;
62+
} else if (bit != '0') {
63+
throw new IllegalArgumentException(
64+
"Invalid binary string: contains character '" + bit + "' at position " + i);
65+
}
66+
}
67+
return result;
68+
}
69+
}

src/test/java/prog/lzw/LunzippingTest.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,4 @@ void testBinaryToStringConversion() {
102102
assertEquals("11111111", Lunzipping.bttost[255], "Conversion for 255");
103103
}
104104

105-
@Test
106-
void testByteToIntConversion() {
107-
// Test positive values
108-
assertEquals(0, Lunzipping.btoi((byte) 0));
109-
assertEquals(127, Lunzipping.btoi((byte) 127));
110-
111-
// Test negative values (which should be converted to positive)
112-
assertEquals(128, Lunzipping.btoi((byte) -128));
113-
assertEquals(255, Lunzipping.btoi((byte) -1));
114-
}
115-
116-
@Test
117-
void testStringToIntConversion() {
118-
assertEquals(0, Lunzipping.stoi("0"));
119-
assertEquals(1, Lunzipping.stoi("1"));
120-
assertEquals(2, Lunzipping.stoi("10"));
121-
assertEquals(255, Lunzipping.stoi("11111111"));
122-
}
123105
}

src/test/java/prog/lzw/LzippingTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ void setUp() throws IOException {
2626
compressedFile = new File(inputFile.getAbsolutePath() + ".LmZWp");
2727
}
2828

29-
@Test
30-
void testByteToInteger() {
31-
// Test positive byte values
32-
assertEquals(65, Lzipping.btoi((byte) 65)); // 'A'
33-
assertEquals(90, Lzipping.btoi((byte) 90)); // 'Z'
34-
assertEquals(48, Lzipping.btoi((byte) 48)); // '0'
35-
36-
// Test negative byte values (should convert to positive values 128-255)
37-
assertEquals(128, Lzipping.btoi((byte) -128));
38-
assertEquals(255, Lzipping.btoi((byte) -1));
39-
}
4029

4130
@Test
4231
void testFillBinaryString() {

src/test/java/prog/lzw/LzwUtilsTest.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package prog.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
/**
7+
* Comprehensive unit tests for the CommonUtil class.
8+
* Tests both the new method names and deprecated methods for backward compatibility.
9+
*/
10+
public class CommonUtilTest {
11+
12+
@Test
13+
void testByteToUnsignedIntConversion() {
14+
// Test edge cases
15+
assertEquals(0, CommonUtil.byteToUnsignedInt((byte) 0), "Zero byte should convert to 0");
16+
assertEquals(127, CommonUtil.byteToUnsignedInt((byte) 127), "Max positive byte should convert to 127");
17+
18+
// Test common ASCII values
19+
assertEquals(65, CommonUtil.byteToUnsignedInt((byte) 65), "ASCII 'A' should convert to 65");
20+
assertEquals(90, CommonUtil.byteToUnsignedInt((byte) 90), "ASCII 'Z' should convert to 90");
21+
assertEquals(48, CommonUtil.byteToUnsignedInt((byte) 48), "ASCII '0' should convert to 48");
22+
23+
// Test negative byte values (should convert to positive values 128-255)
24+
assertEquals(128, CommonUtil.byteToUnsignedInt((byte) -128), "Min byte should convert to 128");
25+
assertEquals(200, CommonUtil.byteToUnsignedInt((byte) -56), "Negative byte -56 should convert to 200");
26+
assertEquals(255, CommonUtil.byteToUnsignedInt((byte) -1), "-1 byte should convert to 255");
27+
}
28+
29+
@Test
30+
void testBinaryStringToIntConversion() {
31+
// Test single bit values
32+
assertEquals(0, CommonUtil.binaryStringToInt("0"), "Binary '0' should convert to 0");
33+
assertEquals(1, CommonUtil.binaryStringToInt("1"), "Binary '1' should convert to 1");
34+
35+
// Test multi-bit values
36+
assertEquals(2, CommonUtil.binaryStringToInt("10"), "Binary '10' should convert to 2");
37+
assertEquals(3, CommonUtil.binaryStringToInt("11"), "Binary '11' should convert to 3");
38+
assertEquals(10, CommonUtil.binaryStringToInt("1010"), "Binary '1010' should convert to 10");
39+
assertEquals(15, CommonUtil.binaryStringToInt("1111"), "Binary '1111' should convert to 15");
40+
41+
// Test byte boundary values
42+
assertEquals(255, CommonUtil.binaryStringToInt("11111111"), "8 bits all ones should convert to 255");
43+
assertEquals(1023, CommonUtil.binaryStringToInt("1111111111"), "10 bits all ones should convert to 1023");
44+
45+
// Test with leading zeros
46+
assertEquals(10, CommonUtil.binaryStringToInt("00001010"), "Binary with leading zeros should work correctly");
47+
}
48+
49+
@Test
50+
void testBinaryStringToIntWithInvalidInput() {
51+
// Test invalid characters
52+
assertThrows(IllegalArgumentException.class,
53+
() -> CommonUtil.binaryStringToInt("10201"),
54+
"Should throw exception for non-binary character '2'");
55+
56+
assertThrows(IllegalArgumentException.class,
57+
() -> CommonUtil.binaryStringToInt("abc"),
58+
"Should throw exception for non-binary characters");
59+
}
60+
61+
}

0 commit comments

Comments
 (0)