Projekt

Obecné

Profil

Stáhnout (1.94 KB) Statistiky
| Větev: | Tag: | Revize:
1 03a2d08f Michal Horký
import java.io.File;
2
import java.io.IOException;
3
import java.util.List;
4 49fd8648 Michal Horký
import java.util.concurrent.Semaphore;
5
import java.util.concurrent.atomic.AtomicBoolean;
6
import java.util.concurrent.atomic.AtomicReference;
7 03a2d08f Michal Horký
8
import jdeserialize.content;
9
import jdeserialize.jdeserialize;
10
11 49fd8648 Michal Horký
import io.FileWorker;
12
13 03a2d08f Michal Horký
public class Converter extends Thread {
14
	
15 49fd8648 Michal Horký
	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 03a2d08f Michal Horký
	
28 49fd8648 Michal Horký
	public void end() {
29
		active.set(false);
30
		setInput(null);
31
	}
32 03a2d08f Michal Horký
	
33 49fd8648 Michal Horký
	public void setInput(File input) {
34
		this.input.set(input);
35
		inputAvailable.release();
36 03a2d08f Michal Horký
	}
37
	
38
	@Override
39
	public void run() {
40
		super.run();
41 49fd8648 Michal Horký
		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 03a2d08f Michal Horký
		
63 49fd8648 Michal Horký
		String json;
64 03a2d08f Michal Horký
		try {
65 49fd8648 Michal Horký
			json = convert(array);
66 03a2d08f Michal Horký
		} catch (IOException e) {
67 49fd8648 Michal Horký
			json = null;
68 03a2d08f Michal Horký
		}
69 49fd8648 Michal Horký
		ui.completed(json);
70 03a2d08f Michal Horký
	}
71
	
72 49fd8648 Michal Horký
	private String convert(byte[] buffer) throws IOException {
73 e3d5fc53 @havlijan17
		String json = "";
74 49fd8648 Michal Horký
		
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 03a2d08f Michal Horký
81 49fd8648 Michal Horký
        // 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 e3d5fc53 @havlijan17
			{
88 49fd8648 Michal Horký
				json += cnt.toString();
89 e3d5fc53 @havlijan17
			}
90 03a2d08f Michal Horký
		}
91 49fd8648 Michal Horký
		
92
		return json;
93 03a2d08f Michal Horký
	}
94
95
}