Projekt

Obecné

Profil

Stáhnout (4.72 KB) Statistiky
| Větev: | Tag: | Revize:
1
package io;
2

    
3
import java.io.BufferedInputStream;
4
import java.io.BufferedWriter;
5
import java.io.ByteArrayInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileOutputStream;
10
import java.io.FileWriter;
11
import java.io.IOException;
12
import java.io.PrintStream;
13
import java.net.URISyntaxException;
14
import java.net.URL;
15
import java.util.ArrayList;
16
import java.util.Enumeration;
17
import java.util.List;
18
import java.util.NoSuchElementException;
19
import java.util.Properties;
20
import java.util.jar.JarEntry;
21
import java.util.jar.JarFile;
22

    
23
import javafx.collections.ObservableList;
24

    
25
public class FileWorker {
26
	
27
	private static final String CONFIG = "app.config";
28
	public static final String INPUT_DIR = "input_dir";
29
	public static final String ACE_MODE = "ace_mode";
30
	public static final String ACE_THEME = "ace_theme";
31
	public static final String DB_TYPE = "db_type";
32
	public static final String DB_URL = "db_url";
33
	public static final String DB_USER = "db_user";
34
	
35
	public static String getResource(String path) {
36
		return FileWorker.class.getResource(path).toString();
37
	}
38
	
39
	public static Properties getConfig() {
40
		Properties properties = new Properties();
41
		
42
		try {
43
			File config = new File(CONFIG);
44
			config.createNewFile();
45
			properties.load(new FileInputStream(config));
46
		} catch (IOException e) { }
47
		
48
		return properties;
49
	}
50
	
51
	public static void saveConfig(Properties properties) {
52
		try {
53
			properties.store(new FileOutputStream(CONFIG), null);
54
		} catch (IOException e) {
55
			e.printStackTrace();
56
		}
57
	}
58
	
59
	public static void getAceModesAndThemes(ObservableList<String> modes, ObservableList<String> themes) {
60
		final String aceFolder = "ace";
61
		List<String> names = new ArrayList<String>();
62
		
63
		
64
		String path = FileWorker.class.getResource(FileWorker.class.getSimpleName() + ".class").getFile();
65
		if (!path.startsWith("/")) {
66
			path = ClassLoader.getSystemClassLoader().getResource(path).getFile();
67
			File jarFile = new File(path.substring(6, path.lastIndexOf('!')));
68
			
69
			try {
70
				JarFile jar = new JarFile(jarFile);
71
				Enumeration<JarEntry> entries = jar.entries();
72
				while (entries.hasMoreElements()) {
73
					String name = entries.nextElement().getName();
74
					if (name.startsWith(aceFolder + "/")) {
75
						name = name.substring(name.lastIndexOf("/") + 1);
76
						if (name.length() > 0) {
77
							names.add(name);
78
						}
79
					}
80
				}
81
				jar.close();
82
			} catch (IOException e) {
83
				e.printStackTrace();
84
			}
85
		} else {
86
			URL url = FileWorker.class.getResource("/" + aceFolder);
87
			if (url != null) {
88
				try {
89
					File dir = new File(url.toURI());
90
					for (File file : dir.listFiles()) {
91
						names.add(file.getName());
92
					}
93
				} catch (URISyntaxException e) {
94
					e.printStackTrace();
95
				}
96
			}
97
		}
98
		
99
		
100
		for (int i = 0; i < names.size(); i++) {
101
			if (names.get(i).startsWith("mode-")) {
102
				modes.add(names.get(i).substring(5, names.get(i).lastIndexOf('.')));
103
			} else if (names.get(i).startsWith("theme-")) {
104
				themes.add(names.get(i).substring(6, names.get(i).lastIndexOf('.')));
105
			}
106
		}
107
	}
108
	
109
	public static PrintStream createRedirectStream(boolean error) throws FileNotFoundException {
110
		return new PrintStream(new File(error ? "jdeserialize_err.txt" : "jdeserialize_out.txt"));
111
	}
112
	
113
	public static byte[] loadFileContent(File file) throws IOException {
114
		byte buffer[] = new byte[(int) file.length()];
115
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
116
		bis.read(buffer);
117
		bis.close();
118
		return buffer;
119
	}
120
	
121
	public static byte[] extractFileContent(byte rawContent[]) throws IOException {
122
		byte buffer[];
123
		
124
		IZipReader input;
125
		try {
126
			input = new ZipInStream(new ByteArrayInputStream(rawContent));
127
		} catch (NoSuchElementException e) {
128
			System.out.println("Note: the file may be a special type of ZIP archive.");
129
			input = new InflaterInStream(new ByteArrayInputStream(rawContent));
130
		}
131
		
132
		try {
133
			List<Byte> _buffer = new ArrayList<Byte>();
134
			while (true) {
135
				int value = input.read();
136
				if (value < 0) {
137
					break;
138
				} else {
139
					_buffer.add((byte) value);
140
				}
141
			}
142

    
143
			buffer = new byte[_buffer.size()];
144
			for (int i = 0; i < buffer.length; i++) {
145
				buffer[i] = _buffer.get(i);
146
			}
147
			
148
			System.out.println(String.format("The file is a%s ZIP archive...", input.getClass() == InflaterInStream.class ? " special" : ""));
149
		} catch (Exception e) {
150
			System.out.println("The file is not a ZIP archive...");
151
			buffer = rawContent;
152
		} finally {
153
			input.close();
154
		}
155

    
156
		System.out.println("Bytes from the file were loaded correctly.");
157
		return buffer;
158
	}
159

    
160
	public static void saveJson(File file, String json) throws IOException {
161
		FileWriter fw = new FileWriter(file);
162
		BufferedWriter bw = new BufferedWriter(fw);
163
		bw.write(json);
164
		bw.flush();
165
		bw.close();
166
	}
167
	
168
}
(2-2/5)