1
|
import java.io.File;
|
2
|
import java.io.IOException;
|
3
|
import java.util.List;
|
4
|
import java.util.concurrent.Semaphore;
|
5
|
import java.util.concurrent.atomic.AtomicBoolean;
|
6
|
import java.util.concurrent.atomic.AtomicReference;
|
7
|
|
8
|
import jdeserialize.content;
|
9
|
import jdeserialize.jdeserialize;
|
10
|
|
11
|
import io.FileWorker;
|
12
|
|
13
|
public class Converter extends Thread {
|
14
|
|
15
|
private IConversionResults ui;
|
16
|
private AtomicBoolean active;
|
17
|
|
18
|
private AtomicReference<File> input;
|
19
|
private Semaphore inputAvailable;
|
20
|
|
21
|
public Converter(IConversionResults ui) {
|
22
|
this.ui = ui;
|
23
|
active = new AtomicBoolean(true);
|
24
|
input = new AtomicReference<File>(null);
|
25
|
inputAvailable = new Semaphore(0);
|
26
|
}
|
27
|
|
28
|
public void end() {
|
29
|
active.set(false);
|
30
|
setInput(null);
|
31
|
}
|
32
|
|
33
|
public void setInput(File input) {
|
34
|
this.input.set(input);
|
35
|
inputAvailable.release();
|
36
|
}
|
37
|
|
38
|
@Override
|
39
|
public void run() {
|
40
|
super.run();
|
41
|
while (active.get()) {
|
42
|
try {
|
43
|
inputAvailable.acquire();
|
44
|
File input = this.input.get();
|
45
|
if (input != null) {
|
46
|
process(input);
|
47
|
}
|
48
|
} catch (InterruptedException e) {
|
49
|
continue;
|
50
|
}
|
51
|
}
|
52
|
}
|
53
|
|
54
|
private void process(File input) {
|
55
|
byte array[];
|
56
|
try {
|
57
|
array = FileWorker.loadByteArray(input);
|
58
|
} catch (IOException e) {
|
59
|
ui.loadingInputFileError();
|
60
|
return;
|
61
|
}
|
62
|
|
63
|
String json;
|
64
|
try {
|
65
|
json = convert(array);
|
66
|
} catch (IOException e) {
|
67
|
json = null;
|
68
|
}
|
69
|
ui.completed(json);
|
70
|
}
|
71
|
|
72
|
private String convert(byte[] buffer) throws IOException {
|
73
|
String json = "";
|
74
|
|
75
|
jdeserialize deserializer = new jdeserialize(buffer);
|
76
|
|
77
|
// gets the "contents" into an array - returnes the deserialization of all
|
78
|
// 'writes' into serialized object via objectOutputStream
|
79
|
List<content> cntnts = deserializer.getContent();
|
80
|
|
81
|
// testing with only first of the contents
|
82
|
//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
83
|
|
84
|
for(content cnt : cntnts)
|
85
|
{
|
86
|
if(cnt != null)
|
87
|
{
|
88
|
json += cnt.toString();
|
89
|
}
|
90
|
}
|
91
|
|
92
|
return json;
|
93
|
}
|
94
|
|
95
|
}
|