From b638d1b127413ea273170db6eeea4ddb2cfca279 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Mon, 25 May 2015 10:48:39 +0200
Subject: [PATCH] refactored out decodeBytes and decodeIntentions

---
 .../control/labcomm2014/DecoderChannel.java   | 54 +++++++++----------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/lib/java/se/lth/control/labcomm2014/DecoderChannel.java b/lib/java/se/lth/control/labcomm2014/DecoderChannel.java
index ca5d6ae..6e3ea2a 100644
--- a/lib/java/se/lth/control/labcomm2014/DecoderChannel.java
+++ b/lib/java/se/lth/control/labcomm2014/DecoderChannel.java
@@ -16,17 +16,33 @@ public class DecoderChannel implements Decoder {
     this.in = new DataInputStream(in);
   }
 
-  private void processSampleDef() throws IOException {
-    int index = decodePacked32();
-    int numIntentions = decodePacked32();
-    if(numIntentions != 1) {
-        System.out.println("WARNING: #intentions == "+numIntentions);
+  private byte[] decodeBytes() throws IOException {
+    int len = decodePacked32();
+    byte result[] = new byte[len];
+
+    for(int i=0; i<len; i++) {
+      result[i] = decodeByte();
     }
-    int keylen = decodePacked32();
-    if(keylen != 0) {
-        System.out.println("WARNING: keylen == "+keylen);
+    return result;
+  }
+
+  private String decodeIntentions() throws IOException {
+    int numIntentions = decodePacked32();
+    String name = "";
+    for(int i = 0; i<numIntentions; i++) {
+      byte key[] = decodeBytes();
+      byte val[] = decodeBytes();
+
+      if(key.length == 0) {
+        name = new String(val);
+      }
     }
-    String name = decodeString();
+    return name;
+  }
+
+  private void processSampleDef() throws IOException {
+    int index = decodePacked32();
+    String name = decodeIntentions();
     int signature_length = decodePacked32();
     byte[] signature = new byte[signature_length];
     ReadBytes(signature, signature_length);
@@ -35,15 +51,7 @@ public class DecoderChannel implements Decoder {
 
   private void processSampleRef() throws IOException {
     int index = decodePacked32();
-    int numIntentions = decodePacked32();
-    if(numIntentions != 1) {
-        System.out.println("WARNING: #intentions == "+numIntentions);
-    }
-    int keylen = decodePacked32();
-    if(keylen != 0) {
-        System.out.println("WARNING: keylen == "+keylen);
-    }
-    String name = decodeString();
+    String name = decodeIntentions();
     int signature_length = decodePacked32();
     byte[] signature = new byte[signature_length];
     ReadBytes(signature, signature_length);
@@ -55,15 +63,7 @@ public class DecoderChannel implements Decoder {
            processSample(Constant.TYPE_DEF);
       } catch(Exception ex) {
        int idx = decodePacked32();
-    int numIntentions = decodePacked32();
-    if(numIntentions != 1) {
-        System.out.println("WARNING: #intentions == "+numIntentions);
-    }
-    int keylen = decodePacked32();
-    if(keylen != 0) {
-        System.out.println("WARNING: keylen == "+keylen);
-    }
-       String name = decodeString();
+       String name = decodeIntentions();
        int siglen = decodePacked32();
        for(int i=0; i<siglen; i++) {
            byte b = decodeByte();
-- 
GitLab