GenuineSounds

Format

May 27th, 2012
205
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1.  
  2. package ltd.genuinesounds.game.SaveFormat;
  3.  
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.nio.ByteBuffer;
  8. import java.nio.ByteOrder;
  9. import java.nio.channels.Channels;
  10. import java.nio.channels.WritableByteChannel;
  11. import java.util.Map.Entry;
  12. import java.util.zip.GZIPOutputStream;
  13.  
  14. @SuppressWarnings("unchecked")
  15. public class Format {
  16.    
  17.     // Error keys
  18.     public static final byte ERROR_DEFINITION = 0;
  19.  
  20.     // Setting Flags
  21.     public static final byte FLAG_READ = 1;
  22.     public static final byte FLAG_WRITE = 2;
  23.     public static final byte FLAG_DEFUALT = (byte) (FLAG_READ | FLAG_WRITE);
  24.  
  25.     // Format System Bits
  26.     public static final byte SYS_SIZE_IN_BYTES = 2;
  27.     public static final byte SYS_START_NAME = 4;
  28.     public static final byte SYS_END_NAME = 8;
  29.     public static final byte SYS_START_CONTENT = 16;
  30.     public static final byte SYS_END_CONTENT = 32;
  31.  
  32.     // Type Tags
  33.     public static final byte TAG_BOOLEAN = 0;
  34.     public static final byte TAG_BYTE = 1;
  35.     public static final byte TAG_SHORT = 2;
  36.     public static final byte TAG_CHAR = 3;
  37.     public static final byte TAG_INT = 4;
  38.     public static final byte TAG_FLOAT = 5;
  39.     public static final byte TAG_DOUBLE = 6;
  40.     public static final byte TAG_LONG = 7;
  41.     public static final byte TAG_STRING = 8;
  42.  
  43.     public static void saveAsReadble(byte caser, ISetting<?> setting) throws SaveException {
  44.         saveAs(true, caser, setting);
  45.     }
  46.  
  47.     public static void saveAsCompact(byte caser, ISetting<?> setting) throws SaveException {
  48.         saveAs(false, caser, setting);
  49.     }
  50.  
  51.     private static void saveAs(boolean readable, byte tagType, ISetting<?> setting) throws SaveException {
  52.         // init ByteBuffer
  53.         ByteBuffer bb = ByteBuffer.allocate(0xFFFFFF).order(ByteOrder.nativeOrder());
  54.        
  55.         byte[] name = setting.name().getBytes();
  56.         // state which kind of information is carried in file.
  57.         bb.put(tagType);
  58.  
  59.         // mark next byte as size in bytes, byte after that is settings name.
  60.         bb.put((byte) (SYS_SIZE_IN_BYTES | SYS_START_NAME));
  61.         bb.put((byte) name.length);
  62.         bb.put(name);
  63.  
  64.         // type of settings list.
  65.         switch (tagType) {
  66.             case TAG_BOOLEAN:
  67.                 createByteArrayForBoolean(bb, (ISetting<Boolean>) setting);
  68.                 break;
  69.                
  70.             // TODO: add other cases
  71.             default: throw new SaveException(setting);
  72.         }
  73.  
  74.         // end all settings
  75.         bb.put(SYS_END_CONTENT);
  76.  
  77.         // zero byte (not used)
  78.         bb.put((byte) 0);
  79.        
  80.         // reduce buffer size to end bit (0)
  81.         bb.limit(bb.position());
  82.        
  83.         // rewind for correct order
  84.         bb.rewind();
  85.        
  86.         // write buffer
  87.         try {
  88.             File file = new File("D:/" + setting.name() + ".dat");
  89.             file.createNewFile();
  90.             writeBuffer(readable, bb, file);
  91.         }
  92.         catch (IOException e) {
  93.             e.printStackTrace();
  94.         }
  95.     }
  96.    
  97.     private static void createByteArrayForBoolean(ByteBuffer bb, ISetting<Boolean> setting) {
  98.         boolean first = true;
  99.         for (Entry<String, Boolean> entry : setting.set()) {
  100.             byte[] name = entry.getKey().getBytes();
  101.            
  102.             // on first run to save space end name from header else end privious content here for space saving
  103.             // state size and begin name
  104.             bb.put((byte) ((first ? SYS_END_NAME : SYS_END_CONTENT) | SYS_SIZE_IN_BYTES | SYS_START_NAME));
  105.             bb.put((byte) name.length);
  106.             bb.put(name);
  107.            
  108.             // end name and begin content, no size flag needed, booleans are assumed to be one bit
  109.             bb.put((byte) (SYS_END_NAME | SYS_START_CONTENT));
  110.             bb.put((byte) (entry.getValue() ? 1 : 0));
  111.            
  112.             // set not first run
  113.             first = false;
  114.         }
  115.     }
  116.  
  117.     private static void writeBuffer(boolean readable, ByteBuffer buffer, File file) throws IOException {
  118.         WritableByteChannel channel;
  119.         if (!readable)
  120.             channel = Channels.newChannel(new GZIPOutputStream(new FileOutputStream(file)));
  121.         else
  122.             channel = Channels.newChannel(new FileOutputStream(file));
  123.         channel.write(buffer);
  124.         channel.close();
  125.     }
  126.  
  127. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment