-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLZW.java
More file actions
131 lines (106 loc) · 3.82 KB
/
LZW.java
File metadata and controls
131 lines (106 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.*;
import java.util.*;
public class LZW {
private static final int BIT_0 = 1;
/** Mask for bit 1 of a byte. */
private static final int BIT_1 = 0x02;
/** Mask for bit 2 of a byte. */
private static final int BIT_2 = 0x04;
/** Mask for bit 3 of a byte. */
private static final int BIT_3 = 0x08;
/** Mask for bit 4 of a byte. */
private static final int BIT_4 = 0x10;
/** Mask for bit 5 of a byte. */
private static final int BIT_5 = 0x20;
/** Mask for bit 6 of a byte. */
private static final int BIT_6 = 0x40;
/** Mask for bit 7 of a byte. */
private static final int BIT_7 = 0x80;
private static final int[] BITS = { BIT_0, BIT_1, BIT_2, BIT_3, BIT_4, BIT_5, BIT_6, BIT_7 };
static int binaryLength;
static String newFilename;
HashMap<String, String> map;
public LZW(int BinaryLength, String newFileName)
{
newFilename = newFileName;
map = new HashMap<String, String>((int)Math.pow(2, binaryLength));
binaryLength = BinaryLength;
for(int i = 0; i < 256; i++)
{
String binString = intToBinary(i);
String actualLetters = Character.toString((char)i);
map.put(actualLetters, binString);
//<"char", binary>
}
}
public void encode(String filepath) throws IOException
{
int counter = 256;
File file = new File(filepath);
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
int currInt = br.read();
String currString = String.valueOf((char)currInt); //gets "char" from 9 digit binary
int nextInt = br.read();
String nextString = String.valueOf((char)nextInt); //gets "char" from 9 digit binary
while(currInt != -1)
{
if(!map.containsKey(currString+nextString) && nextInt != -1)
{
sb.append(map.get(currString));
map.put(currString+nextString, intToBinary(counter));
currInt = nextInt;
currString = nextString;
nextInt = br.read();
nextString = String.valueOf((char)nextInt);
counter++;
}
else if(nextInt == -1)
{
sb.append(map.get(currString));
currInt = nextInt;
}
else
{
currString += nextString;
nextInt = br.read();
nextString = String.valueOf((char)nextInt);
}
}
writeToBinary(sb.toString());
}
public static void writeToBinary(String s) throws IOException
{
char[] ascii = s.toCharArray();
byte[] l_raw = new byte[ascii.length >> 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using
* multiplication every time inside the loop.
*/
for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
for (int bits = 0; bits < BITS.length; ++bits) {
if (ascii[jj - bits] == '1') {
l_raw[ii] |= BITS[bits];
}
}
}
FileOutputStream fos = new FileOutputStream(newFilename);
fos.write(l_raw);
}
public static String intToBinary(int num)
{
String binString = Integer.toBinaryString(num);
int l = binaryLength-binString.length();
for(int j = 0; j < l; j++)
{
binString = "0"+binString;
}
return binString;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
LZW lzw = new LZW(9, "output.bin");
lzw.encode("lzw-file3.txt");
}
}