Projekt

Obecné

Profil

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

    
135
			buffer = new byte[_buffer.size()];
136
			for (int i = 0; i < buffer.length; i++) {
137
				buffer[i] = _buffer.get(i);
138
			}
139
			
140
			System.out.println(String.format("The file is a%s ZIP archive...", input.getClass() == InflaterInStream.class ? " special" : ""));
141
		} catch (Exception e) {
142
			System.out.println("The file is not a ZIP archive...");
143
			buffer = fileContent;
144
		} finally {
145
			input.close();
146
		}
147

    
148
		System.out.println("Bytes from the file were loaded correctly.");
149
		return buffer;
150
	}
151

    
152
	private static byte[] loadFileContent(File file) throws IOException {
153
		byte buffer[] = new byte[(int) file.length()];
154
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
155
		bis.read(buffer);
156
		bis.close();
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)