net.pms.util.PmsProperties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pms Show documentation
Show all versions of pms Show documentation
PS3 Media Server is a cross-platform DLNA-compliant UPnP Media Server.
Originally written to support the PlayStation 3, PS3 Media Server has been
expanded to support a range of other media renderers, including smartphones,
televisions, music players and more.
The newest version!
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 ([email protected])
*/
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);
try {
properties.load(inputStream);
} finally {
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);
}
}