Modifié système de sauvegarde : compression en GZIP (optimisation)

This commit is contained in:
galaxyoyo 2015-01-11 12:35:38 +01:00
parent 35a6f3e85d
commit fb10d14f8c
1 changed files with 31 additions and 19 deletions

View File

@ -5,18 +5,22 @@ import galaxyoyo.unknown.editor.Map;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedReader; import java.io.BufferedInputStream;
import java.io.BufferedWriter; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.logging.log4j.LogManager;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -46,6 +50,7 @@ public class EditorAPI
builder.enableComplexMapKeySerialization(); builder.enableComplexMapKeySerialization();
builder.serializeNulls(); builder.serializeNulls();
builder.setPrettyPrinting();
return builder.create(); return builder.create();
} }
@ -99,18 +104,18 @@ public class EditorAPI
try try
{ {
file.createNewFile(); file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file)); // BufferedWriter bw = new BufferedWriter(new FileWriter(file));
BufferedOutputStream bos = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
for (char c : json.toCharArray()) /* for (char c : json.toCharArray())
{ {
char ch = c; char ch = c;
ch = (char) ((((int) c * 2) - 4) * 3); ch = (char) ((((int) c * 2) - 4) * 3);
}*/
bw.append(ch); bos.write(json.getBytes("UTF-8"));
}
bw.close(); bos.close();
} }
catch (IOException ex) catch (IOException ex)
{ {
@ -139,18 +144,23 @@ public class EditorAPI
String json = null; String json = null;
try try
{ {
BufferedReader br = new BufferedReader(new FileReader(f)); long startTime = System.currentTimeMillis();
// BufferedReader br = new BufferedReader(new FileReader(f));
GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(new FileInputStream(f)));
byte[] bytes = new byte[512*1024];
int count = 0;
String text = ""; String text = "";
String ln; while ((count = gis.read(bytes)) != -1)
while ((ln = br.readLine()) != null)
{ {
text += ln + "\n"; text += new String(bytes, 0, count, "UTF-8");
} }
br.close(); gis.close();
System.out.println("Took : " + (System.currentTimeMillis() - startTime) + " ms");
bytes = null;
json = ""; json = text;
for (char c : text.toCharArray()) /* for (char c : text.toCharArray())
{ {
char ch = c; char ch = c;
@ -159,13 +169,15 @@ public class EditorAPI
json += ch; json += ch;
} }
json = json.substring(0, json.length() - 1); json = json.substring(0, json.length() - 1);*/
} }
catch (IOException e) catch (IOException e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
LogManager.getLogger(EditorAPI.class).warn(json);
RawMap rm = createGson().fromJson(json, RawMap.class); RawMap rm = createGson().fromJson(json, RawMap.class);
return open(rm); return open(rm);