Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ltd.genuinesounds.game.SaveFormat;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.channels.Channels;
- import java.nio.channels.WritableByteChannel;
- import java.util.Map.Entry;
- import java.util.zip.GZIPOutputStream;
- @SuppressWarnings("unchecked")
- public class Format {
- // Error keys
- public static final byte ERROR_DEFINITION = 0;
- // Setting Flags
- public static final byte FLAG_READ = 1;
- public static final byte FLAG_WRITE = 2;
- public static final byte FLAG_DEFUALT = (byte) (FLAG_READ | FLAG_WRITE);
- // Format System Bits
- public static final byte SYS_SIZE_IN_BYTES = 2;
- public static final byte SYS_START_NAME = 4;
- public static final byte SYS_END_NAME = 8;
- public static final byte SYS_START_CONTENT = 16;
- public static final byte SYS_END_CONTENT = 32;
- // Type Tags
- public static final byte TAG_BOOLEAN = 0;
- public static final byte TAG_BYTE = 1;
- public static final byte TAG_SHORT = 2;
- public static final byte TAG_CHAR = 3;
- public static final byte TAG_INT = 4;
- public static final byte TAG_FLOAT = 5;
- public static final byte TAG_DOUBLE = 6;
- public static final byte TAG_LONG = 7;
- public static final byte TAG_STRING = 8;
- public static void saveAsReadble(byte caser, ISetting<?> setting) throws SaveException {
- saveAs(true, caser, setting);
- }
- public static void saveAsCompact(byte caser, ISetting<?> setting) throws SaveException {
- saveAs(false, caser, setting);
- }
- private static void saveAs(boolean readable, byte tagType, ISetting<?> setting) throws SaveException {
- // init ByteBuffer
- ByteBuffer bb = ByteBuffer.allocate(0xFFFFFF).order(ByteOrder.nativeOrder());
- byte[] name = setting.name().getBytes();
- // state which kind of information is carried in file.
- bb.put(tagType);
- // mark next byte as size in bytes, byte after that is settings name.
- bb.put((byte) (SYS_SIZE_IN_BYTES | SYS_START_NAME));
- bb.put((byte) name.length);
- bb.put(name);
- // type of settings list.
- switch (tagType) {
- case TAG_BOOLEAN:
- createByteArrayForBoolean(bb, (ISetting<Boolean>) setting);
- break;
- // TODO: add other cases
- default: throw new SaveException(setting);
- }
- // end all settings
- bb.put(SYS_END_CONTENT);
- // zero byte (not used)
- bb.put((byte) 0);
- // reduce buffer size to end bit (0)
- bb.limit(bb.position());
- // rewind for correct order
- bb.rewind();
- // write buffer
- try {
- File file = new File("D:/" + setting.name() + ".dat");
- file.createNewFile();
- writeBuffer(readable, bb, file);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void createByteArrayForBoolean(ByteBuffer bb, ISetting<Boolean> setting) {
- boolean first = true;
- for (Entry<String, Boolean> entry : setting.set()) {
- byte[] name = entry.getKey().getBytes();
- // on first run to save space end name from header else end privious content here for space saving
- // state size and begin name
- bb.put((byte) ((first ? SYS_END_NAME : SYS_END_CONTENT) | SYS_SIZE_IN_BYTES | SYS_START_NAME));
- bb.put((byte) name.length);
- bb.put(name);
- // end name and begin content, no size flag needed, booleans are assumed to be one bit
- bb.put((byte) (SYS_END_NAME | SYS_START_CONTENT));
- bb.put((byte) (entry.getValue() ? 1 : 0));
- // set not first run
- first = false;
- }
- }
- private static void writeBuffer(boolean readable, ByteBuffer buffer, File file) throws IOException {
- WritableByteChannel channel;
- if (!readable)
- channel = Channels.newChannel(new GZIPOutputStream(new FileOutputStream(file)));
- else
- channel = Channels.newChannel(new FileOutputStream(file));
- channel.write(buffer);
- channel.close();
- }
- }
Advertisement