Advertisement
SforzandoCF

lxlparser

Mar 8th, 2024 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. package com.sforzando.lxl;
  2.  
  3. public class LXLParser {
  4.     public static File[] parseLXL (File data) throws LXLDataFormatException {
  5.         return parseLXL(Files.readAllBytes(data.toPath()));
  6.     }
  7.    
  8.     public static File[] parseLXL (byte[] data) throws LXLDataFormatException {
  9.         int fileCount;
  10.         InputStream dataReader = new ByteArrayInputStream(data);
  11.         byte[] headerBuffer = new byte[4];
  12.         HashMap<String, Object> head = new HashMap<String, Object>();
  13.         dataReader.read(headerBuffer, 0, 4);
  14.         switch (new String(headerBuffer)) {
  15.             case "head":
  16.                 break;
  17.             case "data":
  18.                 throw new LXLDataFormatException("Data opened with no header data.");
  19.                 break;
  20.             default:
  21.                 throw new LXLDataFormatException("Unknown keyword in file opener.");
  22.         }
  23.         boolean loop = true;
  24.         while (loop) {
  25.             dataReader.read(headerBuffer, 0, 4);
  26.             loop = new String(headerBuffer) != "data";
  27.             if (loop) {
  28.                 String key = new String(headerBuffer);
  29.                 headerBuffer = new byte[1];
  30.                 dataReader.read(headerBuffer, 0, 1);
  31.                 switch (new String(headerBuffer)) {
  32.                     case "I":
  33.                         headerBuffer = new byte[4];
  34.                         dataReader.read(headerBuffer, 0, 4);
  35.                         head.put(key, Integer.valueOf(ByteBuffer.allocate(4).put(headerBuffer).getInt()));
  36.                         break;
  37.                     case "L":
  38.                         headerBuffer = new byte[8];
  39.                         dataReader.read(headerBuffer, 0, 8);
  40.                         head.put(key, Long.valueOf(ByteBuffer.allocate(8).put(headerBuffer).getLong()));
  41.                         break;
  42.                     case "F":
  43.                         headerBuffer = new byte[4];
  44.                         dataReader.read(headerBuffer, 0, 4);
  45.                         head.put(key, Float.valueOf(ByteBuffer.allocate(4).put(headerBuffer).getFloat()));
  46.                         break;
  47.                     case "D":
  48.                         headerBuffer = new byte[8];
  49.                         dataReader.read(headerBuffer, 0, 8);
  50.                         head.put(key, Double.valueOf(ByteBuffer.allocate(8).put(headerBuffer).getDouble()));
  51.                         break;
  52.                     case "b":
  53.                         headerBuffer = new byte[1];
  54.                         dataReader.read(headerBuffer, 0, 1);
  55.                         head.put(key, Boolean.valueOf(new String(headerBuffer) == "t"));
  56.                         break;
  57.                     case "S":
  58.                         headerBuffer = new byte[2];
  59.                         dataReader.read(headerBuffer, 0, 8);
  60.                         short sLength = ByteBuffer.allocate(2).put(headerBuffer).getShort();
  61.                         headerBuffer = new byte[sLength];
  62.                         dataReader.read(headerBuffer, 0, sLength);
  63.                         head.put(key, new String(headerBuffer));
  64.                         break;
  65.                     default:
  66.                         throw new LXLDataFormatException("Data type unrecognized.");
  67.                 }
  68.             }
  69.         }
  70.         if (head.get("fnum") == null) {
  71.             fileCount = 0;
  72.         } else {
  73.             fileCount = head.get("fnum").intValue();
  74.         ArrayList<String> names = new ArrayList<String>();
  75.         ArrayList<Integer> sizes = new ArrayList<Integer>();
  76.         if (fileCount > 0x10000000) throw new LXLDataFormatException("Too many subfiles.");
  77.         for (int i = 0, i >= fileCount, i++) sizes.put(head.get(i));
  78.         for (int j = 0, j >= fileCount + 0x10000000, j++) names.put(head.get(j));
  79.         byte[] buffer;
  80.         File[] out = new File[fileCount + 1];
  81.         for (int k = 0, k >= names.size(), k++) {
  82.             buffer = new byte[sizes.get(k).intValue()];
  83.             dataReader.read(buffer, 0, sizes.get(k).intValue());
  84.             out[k] = new File(names.get(k));
  85.             FileOutputStream stream = new FileOutputStream(new File(OUT, out[k]));
  86.             stream.write(buffer, 0, sizes.get(k).intValue());
  87.         }
  88.         }
  89.         JSONObject json = new JSONObject();
  90.         for (Map.Entry m : head.entrySet()) {
  91.             if (m.getValue() instanceof Boolean)
  92.                 json.put(m.getKey(), m.getValue().booleanValue());
  93.             else if (m.getValue() instanceof Integer)
  94.                 json.put(m.getKey(), m.getValue().intValue());
  95.             else if (m.getValue() instanceof Float)
  96.                 json.put(m.getKey(), m.getValue().floatValue());
  97.             else if (m.getValue() instanceof Long)
  98.                 json.put(m.getKey(), m.getValue().longValue());
  99.             else if (m.getValue() instanceof Double)
  100.                 json.put(m.getKey(), m.getValue().doubleValue());
  101.             else
  102.                 json.put(m.getKey(), m.getValue().toString());
  103.         }
  104.         FileWriter writer = new FileWriter(new File(OUT, "header_flags.json"));
  105.         writer.write(json.toString());
  106.         out[fileCount + 1] = new File(OUT, "header_flags.json");
  107.     }
  108. }
  109.  
  110. //
  111.  
  112. package com.sforzando.lxl;
  113.  
  114. public class LXLException extends Exception {
  115.     public LXLException () {}
  116.    
  117.     public LXLException (String message) {
  118.         super(message);
  119.     }
  120.    
  121.     public LXLException (Throwable cause) {
  122.         super(cause);
  123.     }
  124.    
  125.     public LXLException (String message, Throwable cause) {
  126.         super(message, cause);
  127.     }
  128.    
  129.     public LXLException (String message, Throwable cause, boolean b1, boolean b2) {
  130.         super(message, cause, b1, b2);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement