change readInstruction/writeInstruction to encodeInstruction/decodeInstruction
authoradam <adam@megacz.com>
Mon, 3 Nov 2008 09:51:00 +0000 (10:51 +0100)
committeradam <adam@megacz.com>
Mon, 3 Nov 2008 09:51:00 +0000 (10:51 +0100)
src/edu/berkeley/fleet/api/Fleet.java
src/edu/berkeley/fleet/two/FleetTwoFleet.java

index ca06e08..ce0cfb2 100644 (file)
@@ -41,14 +41,14 @@ public abstract class Fleet implements Iterable<Ship> {
      *   @instruction  The instruction to encode
      *   @dispatchFrom The dock from which the instruction being written is to be dispatched (required for encoding)
      */
-    public abstract Instruction readInstruction(DataInputStream is, Dock dispatchFrom) throws IOException;
+    public abstract BitVector   encodeInstruction(Instruction instruction, Dock dispatchFrom);
 
     /**
      *  Decodes an instruction from a BitVector
      *   @instruction  The instruction to decode
      *   @dispatchFrom The dock from which the instructions being read are to be dispatched (required for decoding)
      */
-    public abstract void        writeInstruction(DataOutputStream os, Dock dispatchFrom, Instruction instr) throws IOException;
+    public abstract Instruction decodeInstruction(BitVector instruction, Dock dispatchFrom);
 
     /** If supported, run the given code and create a FleetProcess to interact with it. */
     public FleetProcess run(Instruction[] program) {
index 921c0b6..da3e2f1 100644 (file)
@@ -106,20 +106,12 @@ public abstract class FleetTwoFleet extends Fleet {
         return null;
     }
 
-    /** read a machine-formatted instruction from a file (into a Java object) */
-    public Instruction readInstruction(DataInputStream is, Dock dispatchFrom) throws IOException {
-        long inst = 0;
-        try {
-            inst = (inst << 8) | (is.readByte() & 0xff);
-            inst = (inst << 8) | (is.readByte() & 0xff);
-            inst = (inst << 8) | (is.readByte() & 0xff);
-            inst = (inst << 8) | (is.readByte() & 0xff);
-            inst = (inst << 8) | (is.readByte() & 0xff);
-            inst = (inst << 8) | (is.readByte() & 0xff);
-            return readInstruction(inst, dispatchFrom);
-        } catch (EOFException eof) {
-            return null;
-        }
+    public Instruction decodeInstruction(BitVector instruction, Dock dispatchFrom) {
+        return readInstruction(instruction.toLong(), dispatchFrom);
+    }
+
+    public BitVector   encodeInstruction(Instruction instr, Dock dispatchFrom) {
+        return new BitVector(getWordWidth()).set(writeInstruction(instr, dispatchFrom));
     }
 
     public int getShiftWidth() { return SHIFT.valmaskwidth; }
@@ -306,13 +298,7 @@ public abstract class FleetTwoFleet extends Fleet {
         return instr;
     }
 
-    public void writeInstruction(DataOutputStream os, Dock dispatchFrom, Instruction d) throws IOException {
-        long instr = writeInstruction(d, dispatchFrom);
-        for(int i=5; i>=0; i--)
-            os.write(BitManipulations.getIntField(i*8+7, i*8, instr));
-    }
-
-    private static long flagFunctionToLong(FlagFunction ff) {
+    private long flagFunctionToLong(FlagFunction ff) {
         long ret = 0;
         for(Predicate p : ff)
             switch(p) {