-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.java
More file actions
183 lines (156 loc) · 5.45 KB
/
Copy pathEncoder.java
File metadata and controls
183 lines (156 loc) · 5.45 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* @author : Premkumar Sreedhar Vemula
* @StudentID: 801101045
* */
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Encoder {
Map<String, Integer> encTABLE;
//static final String inputFilePath="/Users/premkumar/eclipse-workspace/LZW Algorithm/Encoded Files/";
static final String inputFilePath=Paths.get("").toAbsolutePath().toString().concat("/");
/*
* @Authors: Premkumar Sreedhar Vemula
*
* @Description
* In the constructor the initial table is created, which here is the ASCII table
* */
public Encoder() {
encTABLE = new HashMap<String, Integer>();
for (int i = 0; i < 255 ; i++)
encTABLE.put("" + (char) i, i);
}
/*
* @Authors: Premkumar Sreedhar Vemula
*
* @Parameters:
* inputString - String of characters to be encoded
* bitLength - bitLength
* fileName - input file name
*
* @Description:
* This method follows the logic of Lempel–Ziv–Welch (LZW) algorithm
* and encodes the input string into integer codes which is stored in an ArrayList
* */
public void encoder(String inputString, double bitLength, String fileName) {
ArrayList<Integer> encodedCodes=new ArrayList<Integer>();
char[] ipchar=inputString.toCharArray();
String iniString="";
int tblsize=encTABLE.size();
double maxTblSize=Math.pow(2, bitLength);
for(int i=0;i<ipchar.length;i++) {
try {
if (encTABLE.containsKey(iniString.concat(String.valueOf(ipchar[i])))) {
iniString += ipchar[i];
} else {
encodedCodes.add(encTABLE.get(iniString));
if(tblsize<maxTblSize) {
tblsize+=1;
encTABLE.put(iniString.concat(String.valueOf(ipchar[i])), tblsize);
}
iniString = String.valueOf(ipchar[i]);
}
} catch (Exception e) {
System.out.println("Error in the for block while executing the encode algo. Error as follows ------->"+e);
}
}
encodedCodes.add(encTABLE.get(iniString));
generateEncodedOutputFile(encodedCodes,fileName);
}
/*
* @Authors: Premkumar Sreedhar Vemula
*
* @Parameters:
* encodedCode - arraylist which consists of the encoded codes for the input text
* inFileName - name of the input file.
*
* @Description:
* This method is used to generate a compressed output 16-bit file (.lzw)
* in the same path where the input file was with .lzw extension
* it reads the encoded codes in for each and writes it to the file.
* */
public void generateEncodedOutputFile(ArrayList<Integer> encodedCode,String inFileName)
{
OutputStream outputStream = null;
String outputFilename=inputFilePath.concat(inFileName.substring(0, inFileName.indexOf(".")).concat(".lzw"));
try { outputStream = new
FileOutputStream(outputFilename);
//Writer outputStreamWriter = new OutputStreamWriter(outputStream);
Writer outputStreamWriter = new OutputStreamWriter(outputStream,"UTF_16BE"); //The charset is used to generate a 16-bit file
encodedCode.forEach(code -> {
try {
if(code==null)
outputStreamWriter.write(0); // this block is for cases if the initial table has no values in it or if the first char wont find a code from the table which wouldnt happen because ASCII table has mostly all the char.
else
outputStreamWriter.write(code);
} catch (IOException e) {
System.out.println("Error while creating encoded file. "+e);
}
});
outputStreamWriter.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found error, kindly check if the path exists. "+e);
}
catch (IOException e) {
System.out.println("Error while closing the file after done writing. "+e);
e.printStackTrace();
}
finally {
System.out.println("Encoded file generated at the path: " +outputFilename);
}
}
/*
* @Authors: Premkumar Sreedhar Vemula
*
* @Description:
* The main methods takes the input arguments
* args[0] - name of the input file which is a text file that has to be encoded
* args[1] - bitLength
* Passes these arguments as input to the readInputFiletoString()
* */
public static void main(String[] args) throws IOException {
String inputFilename=null;
inputFilename=args[0];
if(!inputFilename.substring(inputFilename.lastIndexOf(".")).equals(".txt")) {
System.out.println("Please check the input file format, it should be in .txt format");
}
else {
double bitLength=Integer.parseInt(args[1]);
Encoder enc = new Encoder();
String fileData=readInputFiletoString(inputFilename);
if(fileData.isEmpty()) {
System.out.println("Please check the input file it is either not in the same directory or it is empty");
}
else {
enc.encoder(fileData,bitLength,inputFilename);
}
}
}
/* @Authors: Premkumar Sreedhar Vemula
*
* @Parameters:
* fileName - name of the input file which is to be encoded
*
* @Description:
* This method takes the filename as the input and reads the file from the path
* */
public static String readInputFiletoString(String fileName) {
String data = null;
try {
data = new String(Files.readAllBytes(Paths.get(inputFilePath.concat(fileName))));
}
catch(Exception e) {
System.out.println("Error while reading file to string. Error is as below: " +e);
}
return data;
}
}