@Test public void testSave() throws IOException { Instructions instructions = new Instructions(); instructions.init(); ScriptDefinition script = new Assembler(instructions).assemble(getClass().getResourceAsStream(SCRIPT_RESOURCE)); byte[] saved = new ScriptSaver().save(script); ScriptDefinition loadedScripot = new ScriptLoader().load(42, saved); assertEquals(script, loadedScripot); }
public byte[] save(ScriptDefinition script) { int[] instructions = script.getInstructions(); int[] intOperands = script.getIntOperands(); String[] stringOperands = script.getStringOperands(); Map<Integer, Integer>[] switches = script.getSwitches(); OutputStream out = new OutputStream(); out.writeByte(0); for (int i = 0; i < instructions.length; ++i) { int opcode = instructions[i]; out.writeShort(opcode); if (opcode == LOAD_STRING) { out.writeString(stringOperands[i]); } else if (opcode < 100 && opcode != RETURN && opcode != POP_INT && opcode != POP_STRING) { out.writeInt(intOperands[i]); } else { out.writeByte(intOperands[i]); } } out.writeInt(instructions.length); out.writeShort(script.getLocalIntCount()); out.writeShort(script.getLocalStringCount()); out.writeShort(script.getIntStackCount()); out.writeShort(script.getStringStackCount()); int switchStart = out.getOffset(); if (switches == null) { out.writeByte(0); } else { out.writeByte(switches.length); for (Map<Integer, Integer> s : switches) { out.writeShort(s.size()); for (Entry<Integer, Integer> e : s.entrySet()) { out.writeInt(e.getKey()); out.writeInt(e.getValue()); } } } int switchLength = out.getOffset() - switchStart; out.writeShort(switchLength); return out.flip(); }
ScriptSaver { public byte[] save(ScriptDefinition script) { int[] instructions = script.getInstructions(); int[] intOperands = script.getIntOperands(); String[] stringOperands = script.getStringOperands(); Map<Integer, Integer>[] switches = script.getSwitches(); OutputStream out = new OutputStream(); out.writeByte(0); for (int i = 0; i < instructions.length; ++i) { int opcode = instructions[i]; out.writeShort(opcode); if (opcode == LOAD_STRING) { out.writeString(stringOperands[i]); } else if (opcode < 100 && opcode != RETURN && opcode != POP_INT && opcode != POP_STRING) { out.writeInt(intOperands[i]); } else { out.writeByte(intOperands[i]); } } out.writeInt(instructions.length); out.writeShort(script.getLocalIntCount()); out.writeShort(script.getLocalStringCount()); out.writeShort(script.getIntStackCount()); out.writeShort(script.getStringStackCount()); int switchStart = out.getOffset(); if (switches == null) { out.writeByte(0); } else { out.writeByte(switches.length); for (Map<Integer, Integer> s : switches) { out.writeShort(s.size()); for (Entry<Integer, Integer> e : s.entrySet()) { out.writeInt(e.getKey()); out.writeInt(e.getValue()); } } } int switchLength = out.getOffset() - switchStart; out.writeShort(switchLength); return out.flip(); } }
ScriptSaver { public byte[] save(ScriptDefinition script) { int[] instructions = script.getInstructions(); int[] intOperands = script.getIntOperands(); String[] stringOperands = script.getStringOperands(); Map<Integer, Integer>[] switches = script.getSwitches(); OutputStream out = new OutputStream(); out.writeByte(0); for (int i = 0; i < instructions.length; ++i) { int opcode = instructions[i]; out.writeShort(opcode); if (opcode == LOAD_STRING) { out.writeString(stringOperands[i]); } else if (opcode < 100 && opcode != RETURN && opcode != POP_INT && opcode != POP_STRING) { out.writeInt(intOperands[i]); } else { out.writeByte(intOperands[i]); } } out.writeInt(instructions.length); out.writeShort(script.getLocalIntCount()); out.writeShort(script.getLocalStringCount()); out.writeShort(script.getIntStackCount()); out.writeShort(script.getStringStackCount()); int switchStart = out.getOffset(); if (switches == null) { out.writeByte(0); } else { out.writeByte(switches.length); for (Map<Integer, Integer> s : switches) { out.writeShort(s.size()); for (Entry<Integer, Integer> e : s.entrySet()) { out.writeInt(e.getKey()); out.writeInt(e.getValue()); } } } int switchLength = out.getOffset() - switchStart; out.writeShort(switchLength); return out.flip(); }  }
ScriptSaver { public byte[] save(ScriptDefinition script) { int[] instructions = script.getInstructions(); int[] intOperands = script.getIntOperands(); String[] stringOperands = script.getStringOperands(); Map<Integer, Integer>[] switches = script.getSwitches(); OutputStream out = new OutputStream(); out.writeByte(0); for (int i = 0; i < instructions.length; ++i) { int opcode = instructions[i]; out.writeShort(opcode); if (opcode == LOAD_STRING) { out.writeString(stringOperands[i]); } else if (opcode < 100 && opcode != RETURN && opcode != POP_INT && opcode != POP_STRING) { out.writeInt(intOperands[i]); } else { out.writeByte(intOperands[i]); } } out.writeInt(instructions.length); out.writeShort(script.getLocalIntCount()); out.writeShort(script.getLocalStringCount()); out.writeShort(script.getIntStackCount()); out.writeShort(script.getStringStackCount()); int switchStart = out.getOffset(); if (switches == null) { out.writeByte(0); } else { out.writeByte(switches.length); for (Map<Integer, Integer> s : switches) { out.writeShort(s.size()); for (Entry<Integer, Integer> e : s.entrySet()) { out.writeInt(e.getKey()); out.writeInt(e.getValue()); } } } int switchLength = out.getOffset() - switchStart; out.writeShort(switchLength); return out.flip(); }  byte[] save(ScriptDefinition script); }
ScriptSaver { public byte[] save(ScriptDefinition script) { int[] instructions = script.getInstructions(); int[] intOperands = script.getIntOperands(); String[] stringOperands = script.getStringOperands(); Map<Integer, Integer>[] switches = script.getSwitches(); OutputStream out = new OutputStream(); out.writeByte(0); for (int i = 0; i < instructions.length; ++i) { int opcode = instructions[i]; out.writeShort(opcode); if (opcode == LOAD_STRING) { out.writeString(stringOperands[i]); } else if (opcode < 100 && opcode != RETURN && opcode != POP_INT && opcode != POP_STRING) { out.writeInt(intOperands[i]); } else { out.writeByte(intOperands[i]); } } out.writeInt(instructions.length); out.writeShort(script.getLocalIntCount()); out.writeShort(script.getLocalStringCount()); out.writeShort(script.getIntStackCount()); out.writeShort(script.getStringStackCount()); int switchStart = out.getOffset(); if (switches == null) { out.writeByte(0); } else { out.writeByte(switches.length); for (Map<Integer, Integer> s : switches) { out.writeShort(s.size()); for (Entry<Integer, Integer> e : s.entrySet()) { out.writeInt(e.getKey()); out.writeInt(e.getValue()); } } } int switchLength = out.getOffset() - switchStart; out.writeShort(switchLength); return out.flip(); }  byte[] save(ScriptDefinition script);  }