From 08def9aa287e5309c12c6a4110f6b8486afd3a6c Mon Sep 17 00:00:00 2001 From: fred-boy Date: Wed, 25 Apr 2018 00:45:25 +0700 Subject: [PATCH] Save with BufferedOutputStream --- .../cavecraft/game/GameSaver.java | 46 ++++++++++--------- .../cavecraft/game/GameWorld.java | 2 +- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java b/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java index fccc7bc..1bf6684 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java @@ -3,8 +3,9 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; import ru.deadsoftware.cavecraft.CaveGame; -import ru.deadsoftware.cavecraft.Items; +import java.io.BufferedOutputStream; +import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.nio.ByteBuffer; @@ -16,39 +17,42 @@ public class GameSaver { private static int[][] fMap, bMap; private static int readIndex; - private static int bytesInt(byte[] bytes) { + private static int bytesToInt(byte[] bytes) { ByteBuffer wrapped = ByteBuffer.wrap(bytes); int res = wrapped.getInt(readIndex); readIndex+=4; return res; } - private static void writeInt(FileHandle file, int i, boolean append) { - byte[] bytes = ByteBuffer.allocate(4).putInt(i).array(); - file.writeBytes(bytes, append); + private static byte[] intToBytes(int i) { + return ByteBuffer.allocate(4).putInt(i).array(); } - private static void saveMap(FileHandle file, int[][] map) { + private static void saveMap(FileHandle file, int[][] map) throws IOException{ int rl,bl; int width = map.length; int height = map[0].length; - writeInt(file, VERSION, false); - writeInt(file, width, true); - writeInt(file, height, true); + BufferedOutputStream out = new BufferedOutputStream(file.write(false)); + out.write(intToBytes(VERSION)); + out.write(intToBytes(width)); + out.write(intToBytes(height)); for (int y=0; y