From 45b8e760c9119ade48b97e093eacc9fb55506555 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Fri, 7 Feb 2014 10:46:29 +0100
Subject: [PATCH] java version2006 written, not tested

---
 lib/java/Makefile                             | 14 +++++++++---
 .../se/lth/control/labcomm2006/LabComm.java   |  2 +-
 .../labcomm2006/LabCommDecoderChannel.java    | 17 +++++---------
 .../labcomm2006/LabCommEncoderChannel.java    | 22 +++++--------------
 4 files changed, 23 insertions(+), 32 deletions(-)

diff --git a/lib/java/Makefile b/lib/java/Makefile
index 5b2b14d..7eefab1 100644
--- a/lib/java/Makefile
+++ b/lib/java/Makefile
@@ -13,18 +13,26 @@ MODULES=LabCommDispatcher \
 	LabCommReader \
 	WriterWrapper
 
-all: labcomm.jar
+all: labcomm.jar labcomm2006.jar
 
 labcomm.jar: gen/JAVAC
 	echo $@
 	cd gen ; jar cf ../$@ se/lth/control/labcomm/*.class
 
+labcomm2006.jar: gen/JAVAC
+	echo $@
+	cd gen ; jar cf ../$@ se/lth/control/labcomm2006/*.class
+
 gen:
 	mkdir gen
 
-gen/JAVAC: $(MODULES:%=se/lth/control/labcomm/%.java) Makefile | gen
+gen/JAVAC: $(MODULES:%=se/lth/control/labcomm/%.java) $(MODULES:%=se/lth/control/labcomm2006/%.java) Makefile | gen
 	javac -d gen $(filter %.java, $^)
 	touch $@
 
+
+.PHONY: clean
+
 clean:
-	rm -rf labcomm.jar gen
+	rm -rf labcomm.jar gen labcomm2006.jar
+
diff --git a/lib/java/se/lth/control/labcomm2006/LabComm.java b/lib/java/se/lth/control/labcomm2006/LabComm.java
index 16b5abd..b83c391 100644
--- a/lib/java/se/lth/control/labcomm2006/LabComm.java
+++ b/lib/java/se/lth/control/labcomm2006/LabComm.java
@@ -27,6 +27,6 @@ public class LabComm {
   /*
    * Start of user declared types
    */
-  public static final int FIRST_USER_INDEX = 0x40;
+  public static final int FIRST_USER_INDEX = 0x80;
 
 }
diff --git a/lib/java/se/lth/control/labcomm2006/LabCommDecoderChannel.java b/lib/java/se/lth/control/labcomm2006/LabCommDecoderChannel.java
index f12fffc..d0767b0 100644
--- a/lib/java/se/lth/control/labcomm2006/LabCommDecoderChannel.java
+++ b/lib/java/se/lth/control/labcomm2006/LabCommDecoderChannel.java
@@ -134,19 +134,12 @@ public class LabCommDecoderChannel implements LabCommDecoder {
     return new String(chars);
   }
 
+  /**
+     method for API harmonization with labcomm2013.
+     Labcomm2006 encodes lengths etc as 32 bit ints.
+  */
   public int decodePacked32() throws IOException {
-    long res=0;
-    byte i=0;
-    boolean cont=true;
-
-    do {
-      byte c = in.readByte();
-      res = (res << 7) | (c & 0x7f);
-      cont = (c & 0x80) != 0;
-      i++;
-    } while(cont);
-
-    return (int) (res & 0xffffffff);
+    return in.readInt();
   }
 }
 
diff --git a/lib/java/se/lth/control/labcomm2006/LabCommEncoderChannel.java b/lib/java/se/lth/control/labcomm2006/LabCommEncoderChannel.java
index c38c852..e0de18d 100644
--- a/lib/java/se/lth/control/labcomm2006/LabCommEncoderChannel.java
+++ b/lib/java/se/lth/control/labcomm2006/LabCommEncoderChannel.java
@@ -19,7 +19,7 @@ public class LabCommEncoderChannel implements LabCommEncoder {
     data = new DataOutputStream(bytes);
     registry = new LabCommEncoderRegistry();
     if (emitVersion) {
-        throw new RuntimeError("Labcomm 2006 does not support emitVersion");
+        throw new IllegalArgumentException("Labcomm 2006 does not support emitVersion");
     }
   }
 
@@ -91,27 +91,17 @@ public class LabCommEncoderChannel implements LabCommEncoder {
   public void encodeString(String value) throws IOException {
     data.writeShort(0); // HACK...
     data.writeUTF(value);
-
-    //kludge, to replace above hack with packed length
-    ByteArrayOutputStream tmpb = new ByteArrayOutputStream();
-    DataOutputStream tmps = new DataOutputStream(tmpb);
-
-    tmps.writeUTF(value);
-    tmps.flush();
-    byte[] tmp = tmpb.toByteArray();
-  
-    encodePacked32(tmp.length-2);
-    for (int i = 2 ; i < tmp.length ; i++) {
-      encodeByte(tmp[i]);
-    }
   }
 
   /**
      method for API harmonization with labcomm2013.
      Labcomm2006 encodes lengths etc as 32 bit ints.
   */
-  public inline void encodePacked32(long value) throws IOException {
-    encodeInt(value);
+  public void encodePacked32(long value) throws IOException {
+    if(value > Integer.MAX_VALUE) {
+      throw new IllegalArgumentException("Value too large, must fit in 32 bits");
+    }
+    encodeInt((int) value);
   }
 }
 
-- 
GitLab