added base 64 preference

This commit is contained in:
Harrison Deng 2017-05-09 15:27:25 -05:00
parent cbd51bca5b
commit f390f4f6dd

View File

@ -0,0 +1,48 @@
package zero1hd.polyjet.util;
import java.util.HashMap;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.Json;
public class Base64Preferences {
FileHandle base64File;
HashMap<String, String> prefKeys;
Json json;
@SuppressWarnings("unchecked")
public Base64Preferences(String prefName) {
base64File = Gdx.files.external(".prefs/" + prefName);
json = new Json();
flush();
}
public void putString(String key, String string) {
prefKeys.put(key, string);
}
public void putObj(String key, Object object) {
prefKeys.put(key, json.toJson(object));
}
public void writeBase64String(String text) {
base64File.writeString(Base64Coder.encodeString(text), true);
}
public void writeBase64Object(Object obj) {
base64File.writeString(Base64Coder.encodeString(json.toJson(obj)), true);
}
public void flush() {
prefKeys = json.fromJson(HashMap.class, new String(Base64Coder.decode(base64File.readString())));
if (prefKeys == null) {
prefKeys = new HashMap<>();
}
base64File.writeString(null, false);
writeBase64Object(json);
}
}