This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
/
PmsProperties.java
68 lines (60 loc) · 1.57 KB
/
PmsProperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package net.pms.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
/**
* Convenience wrapper around the Java Properties class.
*
* @author Tim Cox (mail@tcox.org)
*/
public class PmsProperties {
private final Properties properties = new Properties();
private static final String ENCODING = "UTF-8";
public void loadFromByteArray(byte[] data) throws IOException {
try {
String utf = new String(data, ENCODING);
StringReader reader = new StringReader(utf);
properties.clear();
properties.load(reader);
reader.close();
} catch (UnsupportedEncodingException e) {
throw new IOException("Could not decode " + ENCODING);
}
}
/**
* Initialize from a properties file.
* @param filename The properties file.
* @throws IOException
*/
public void loadFromResourceFile(String filename) throws IOException {
InputStream inputStream = getClass().getResourceAsStream(filename);
properties.load(inputStream);
inputStream.close();
}
public void clear() {
properties.clear();
}
public String get(String key) {
Object obj = properties.get(key);
if (obj != null) {
return trimAndRemoveQuotes("" + obj);
} else {
return "";
}
}
private static String trimAndRemoveQuotes(String in) {
in = in.trim();
if (in.startsWith("\"")) {
in = in.substring(1);
}
if (in.endsWith("\"")) {
in = in.substring(0, in.length() - 1);
}
return in;
}
public boolean containsKey(String key) {
return properties.containsKey(key);
}
}