Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 49fd8648

Přidáno uživatelem Michal Horký před asi 4 roky(ů)

#re #7879

Ještě není dokončeno - commit kvůli merge.

Zobrazit rozdíly:

project/Deserializer/src/Converter.java
1 1
import java.io.File;
2 2
import java.io.IOException;
3 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;
4 7

  
5 8
import jdeserialize.content;
6 9
import jdeserialize.jdeserialize;
7 10

  
11
import io.FileWorker;
12

  
8 13
public class Converter extends Thread {
9 14
	
10
	private Deserializer main;
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
	}
11 27
	
12
	private File input;
13
	private File output;
28
	public void end() {
29
		active.set(false);
30
		setInput(null);
31
	}
14 32
	
15
	public Converter(Deserializer main, File inputFilename, File outputFilename) {
16
		this.main = main;
17
		this.input = inputFilename;
18
		this.output = outputFilename;
33
	public void setInput(File input) {
34
		this.input.set(input);
35
		inputAvailable.release();
19 36
	}
20 37
	
21 38
	@Override
22 39
	public void run() {
23 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
		}
24 62
		
63
		String json;
25 64
		try {
26
			String json = convert(FileWorker.loadByteArray(input));
27
			
28
			String title = "Uložení JSON";
29
			if (FileWorker.saveJson(output, json)) {
30
				System.out.println("The JSON file was saved correctly.");
31
				Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku.");
32
			} else {
33
				System.out.println("There was an error saving the JSON file.");
34
				Report.error(title, null, "Při ukládání JSON souboru nastala chyba.");
35
			}
65
			json = convert(array);
36 66
		} catch (IOException e) {
37
			System.out.println("There was an error loading the file.");
38
			e.printStackTrace();
39
			Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě.");
67
			json = null;
40 68
		}
41

  
42
		main.convertIsComplete();
69
		ui.completed(json);
43 70
	}
44 71
	
45
	private String convert(byte[] buffer) {
72
	private String convert(byte[] buffer) throws IOException {
46 73
		String json = "";
47
		try {
48
			System.out.println("Deserialization begins.");
49
			jdeserialize deserializer = new jdeserialize(buffer);
50
			
51
			// gets the "contents" into an array - returnes the deserialization of all
52
            // 'writes' into serialized object via objectOutputStream
53
            List<content> cntnts = deserializer.getContent();
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();
54 80

  
55
            // testing with only first of the contents
56
			//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
57
			
58
			for(content cnt : cntnts)
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)
59 87
			{
60
				if(cnt != null)
61
				{
62
					json += cnt.toString();
63
				}
88
				json += cnt.toString();
64 89
			}
65

  
66
			System.out.println("Task completed.");
67
			
68
			//just for tests
69
			//System.out.println(json);
70
			
71
			return json;
72
		} catch (IOException e) {
73
			System.out.println("An error occurred while processing!");
74
			e.printStackTrace();
75
			return null;
76 90
		}
91
		
92
		return json;
77 93
	}
78 94

  
79 95
}

Také k dispozici: Unified diff