Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sforzando.lxl;
- public class LXLParser {
- public static File[] parseLXL (File data) throws LXLDataFormatException {
- return parseLXL(Files.readAllBytes(data.toPath()));
- }
- public static File[] parseLXL (byte[] data) throws LXLDataFormatException {
- int fileCount;
- InputStream dataReader = new ByteArrayInputStream(data);
- byte[] headerBuffer = new byte[4];
- HashMap<String, Object> head = new HashMap<String, Object>();
- dataReader.read(headerBuffer, 0, 4);
- switch (new String(headerBuffer)) {
- case "head":
- break;
- case "data":
- throw new LXLDataFormatException("Data opened with no header data.");
- break;
- default:
- throw new LXLDataFormatException("Unknown keyword in file opener.");
- }
- boolean loop = true;
- while (loop) {
- dataReader.read(headerBuffer, 0, 4);
- loop = new String(headerBuffer) != "data";
- if (loop) {
- String key = new String(headerBuffer);
- headerBuffer = new byte[1];
- dataReader.read(headerBuffer, 0, 1);
- switch (new String(headerBuffer)) {
- case "I":
- headerBuffer = new byte[4];
- dataReader.read(headerBuffer, 0, 4);
- head.put(key, Integer.valueOf(ByteBuffer.allocate(4).put(headerBuffer).getInt()));
- break;
- case "L":
- headerBuffer = new byte[8];
- dataReader.read(headerBuffer, 0, 8);
- head.put(key, Long.valueOf(ByteBuffer.allocate(8).put(headerBuffer).getLong()));
- break;
- case "F":
- headerBuffer = new byte[4];
- dataReader.read(headerBuffer, 0, 4);
- head.put(key, Float.valueOf(ByteBuffer.allocate(4).put(headerBuffer).getFloat()));
- break;
- case "D":
- headerBuffer = new byte[8];
- dataReader.read(headerBuffer, 0, 8);
- head.put(key, Double.valueOf(ByteBuffer.allocate(8).put(headerBuffer).getDouble()));
- break;
- case "b":
- headerBuffer = new byte[1];
- dataReader.read(headerBuffer, 0, 1);
- head.put(key, Boolean.valueOf(new String(headerBuffer) == "t"));
- break;
- case "S":
- headerBuffer = new byte[2];
- dataReader.read(headerBuffer, 0, 8);
- short sLength = ByteBuffer.allocate(2).put(headerBuffer).getShort();
- headerBuffer = new byte[sLength];
- dataReader.read(headerBuffer, 0, sLength);
- head.put(key, new String(headerBuffer));
- break;
- default:
- throw new LXLDataFormatException("Data type unrecognized.");
- }
- }
- }
- if (head.get("fnum") == null) {
- fileCount = 0;
- } else {
- fileCount = head.get("fnum").intValue();
- ArrayList<String> names = new ArrayList<String>();
- ArrayList<Integer> sizes = new ArrayList<Integer>();
- if (fileCount > 0x10000000) throw new LXLDataFormatException("Too many subfiles.");
- for (int i = 0, i >= fileCount, i++) sizes.put(head.get(i));
- for (int j = 0, j >= fileCount + 0x10000000, j++) names.put(head.get(j));
- byte[] buffer;
- File[] out = new File[fileCount + 1];
- for (int k = 0, k >= names.size(), k++) {
- buffer = new byte[sizes.get(k).intValue()];
- dataReader.read(buffer, 0, sizes.get(k).intValue());
- out[k] = new File(names.get(k));
- FileOutputStream stream = new FileOutputStream(new File(OUT, out[k]));
- stream.write(buffer, 0, sizes.get(k).intValue());
- }
- }
- JSONObject json = new JSONObject();
- for (Map.Entry m : head.entrySet()) {
- if (m.getValue() instanceof Boolean)
- json.put(m.getKey(), m.getValue().booleanValue());
- else if (m.getValue() instanceof Integer)
- json.put(m.getKey(), m.getValue().intValue());
- else if (m.getValue() instanceof Float)
- json.put(m.getKey(), m.getValue().floatValue());
- else if (m.getValue() instanceof Long)
- json.put(m.getKey(), m.getValue().longValue());
- else if (m.getValue() instanceof Double)
- json.put(m.getKey(), m.getValue().doubleValue());
- else
- json.put(m.getKey(), m.getValue().toString());
- }
- FileWriter writer = new FileWriter(new File(OUT, "header_flags.json"));
- writer.write(json.toString());
- out[fileCount + 1] = new File(OUT, "header_flags.json");
- }
- }
- //
- package com.sforzando.lxl;
- public class LXLException extends Exception {
- public LXLException () {}
- public LXLException (String message) {
- super(message);
- }
- public LXLException (Throwable cause) {
- super(cause);
- }
- public LXLException (String message, Throwable cause) {
- super(message, cause);
- }
- public LXLException (String message, Throwable cause, boolean b1, boolean b2) {
- super(message, cause, b1, b2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement