Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
184 lines (160 loc) · 7.8 KB
/
Copy pathMain.java
File metadata and controls
184 lines (160 loc) · 7.8 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
184
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import codificacoes.CodingType;
import codificacoes.Decoder;
import codificacoes.Encoder;
import codificacoes.delta.DeltaCodification;
import codificacoes.eliasGamma.EliasGammaCodification;
import codificacoes.fibonacci.FibonacciCodification;
import codificacoes.golomb.GolombCodification;
import codificacoes.unaria.UnaryCodification;
import static codificacoes.CodingType.*;
public class Main {
public static void main(String[] args) {
boolean isOn = true;
while (isOn) {
// escolher função (0-codificar 1-decodificar)
Object[] functions = {"Codificar", "Decodificar"};
int op = JOptionPane.showOptionDialog(null, "Escolha a função desejada: (Para encerrar feche a janela!)", "Função",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, functions, functions[0]);
if (op == -1) {
Object[] options = {"Sim, finalizar programa", "Não, desejo recomeçar"};
int end = JOptionPane.showOptionDialog(null, "Deseja encerrar o programa?", "Finalizar",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (end == 0) {
isOn = false;
break;
} else {
continue;
}
}
// seleção de arquivo
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setCurrentDirectory(new java.io.File("./arquivos"));
if (op == 1) {
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.cod", "cod");
fileChooser.setFileFilter(filter);
fileChooser.addChoosableFileFilter(filter);
}
File selectedFile = null;
int retVal = fileChooser.showOpenDialog(null);
if (retVal == JFileChooser.APPROVE_OPTION) {
if (op == 1) {
while (retVal == JFileChooser.APPROVE_OPTION && !fileChooser.getSelectedFile().getName().endsWith(".cod")) {
JOptionPane.showMessageDialog(null, "O arquivo "
+ fileChooser.getSelectedFile().getName() + " não é um arquivo codificado!",
"Erro de compatibilidade", JOptionPane.ERROR_MESSAGE);
retVal = fileChooser.showOpenDialog(null);
}
}
selectedFile = fileChooser.getSelectedFile();
JOptionPane.showMessageDialog(null, selectedFile.getName());
}
if (retVal == 1) {
continue;
}
if (op == 1) {
try {
Decoder decoder = null;
byte[] data = Files.readAllBytes(selectedFile.toPath());
switch (data[0]) {
case 0:
decoder = new GolombCodification(data[1]);
break;
case 1:
decoder = new EliasGammaCodification();
break;
case 2:
decoder = new FibonacciCodification();
break;
case 3:
decoder = new UnaryCodification();
break;
case 4:
decoder = new DeltaCodification();
break;
default:
break;
}
if (decoder != null) {
byte[] result = decoder.decode(data);
final String ext = ".dec";
String filePath = selectedFile.getPath();
int extIndex = filePath.lastIndexOf(".");
String newPath = (extIndex > -1 ? filePath.substring(0, extIndex) : filePath) + ext;
Files.write(Paths.get(newPath), result);
JOptionPane.showMessageDialog(null, "Decodificação concluída com sucesso");
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
// escolher codificador (0: Golomb, 1:Elias-Gamma, 2:Fibonacci, 3:Unária e 4:Delta)
Object[] items = {Golomb.getName(), EliasGamma.getName(), Fibonacci.getName(), Unary.getName(), Delta.getName()};
Object selectedValue = JOptionPane.showInputDialog(null, "Escolha um codificador:", "Opção",
JOptionPane.INFORMATION_MESSAGE, null, items, items[0]);
if (selectedValue == null) {
continue;
} else {
final CodingType selectedCodingType = getValueByName((String) selectedValue);
Encoder encoder = null;
switch (selectedCodingType) {
case Golomb:
boolean invalidDivisor = true;
String inputValue = null;
while (invalidDivisor) {
inputValue = JOptionPane.showInputDialog("Insira o valor do divisor: (Entre 1 e 255)");
if (inputValue == null) {
break;
}
try {
int divisor = Integer.parseInt(inputValue);
encoder = new GolombCodification(divisor);
if(divisor > 0 && divisor < 256) {
invalidDivisor = false;
}
} catch (Exception e) {
// e.printStackTrace();
}
}
if (inputValue == null) {
continue;
}
break;
case EliasGamma:
encoder = new EliasGammaCodification();
break;
case Fibonacci:
encoder = new FibonacciCodification();
break;
case Unary:
encoder = new UnaryCodification();
break;
case Delta:
encoder = new DeltaCodification();
break;
}
try {
byte[] data = Files.readAllBytes(selectedFile.toPath());
byte[] result = encoder.encode(data);
final String ext = ".cod";
String filePath = selectedFile.getPath();
int extIndex = filePath.lastIndexOf(".");
String newPath = (extIndex > -1 ? filePath.substring(0, extIndex) : filePath) + ext;
Files.write(Paths.get(newPath), result);
JOptionPane.showMessageDialog(null, "Codificação concluída com sucesso");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
System.exit(0);
}
}