mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 10:40:06 -08:00
"b" + "r" now changed to 'b' + 'w'. It's more straight-forward, and may cause perfomance improvements - character primitives allocation is faster and less expensive than string creation.
77 lines
2.2 KiB
Java
77 lines
2.2 KiB
Java
package mage.client.util.object;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectOutputStream;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
|
|
/**
|
|
* Utility class to save an object on disk.
|
|
*
|
|
* @author ayrat
|
|
*/
|
|
public class SaveObjectUtil {
|
|
|
|
/**
|
|
* Defines should data be saved or not.
|
|
* Read from system property:
|
|
*/
|
|
private static boolean saveIncomeData = false;
|
|
|
|
/**
|
|
* Defines the system property name to get {@link #saveIncomeData} value from.
|
|
*/
|
|
private static final String SAVE_DATA_PROPERTY = "saveObjects";
|
|
|
|
/**
|
|
* Date pattern used to form filename to save object to.
|
|
*/
|
|
private static final String DATE_PATTERN = "[yyyy_MM_dd][H-mm-ss]";
|
|
|
|
static {
|
|
saveIncomeData = System.getProperty(SAVE_DATA_PROPERTY) != null;
|
|
}
|
|
|
|
/**
|
|
* Save object on disk.
|
|
*
|
|
* @param object Object to save.
|
|
* @param name Part of name that will be used to form original filename to save object to.
|
|
*/
|
|
public static void saveObject(Object object, String name) {
|
|
if (saveIncomeData) {
|
|
ObjectOutputStream oos = null;
|
|
try {
|
|
File dir = new File("income");
|
|
if (!dir.exists() || dir.exists() && dir.isFile()) {
|
|
boolean bCreated = dir.mkdir();
|
|
if (!bCreated) {
|
|
return;
|
|
}
|
|
}
|
|
String time = now(DATE_PATTERN);
|
|
File f = new File("income" + File.separator + name + '_' + time + ".save");
|
|
if (!f.exists()) {
|
|
f.createNewFile();
|
|
}
|
|
oos = new ObjectOutputStream(new FileOutputStream(f));
|
|
oos.writeObject(object);
|
|
oos.close();
|
|
|
|
} catch (FileNotFoundException e) {
|
|
return;
|
|
} catch (IOException io) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static String now(String dateFormat) {
|
|
Calendar cal = Calendar.getInstance();
|
|
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
|
return sdf.format(cal.getTime());
|
|
}
|
|
}
|