gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r2899 - in freeway/src: . org/gnu/freeway org/gnu/freeway/c


From: mdonoughe
Subject: [GNUnet-SVN] r2899 - in freeway/src: . org/gnu/freeway org/gnu/freeway/cwrappers org/gnu/freeway/cwrappers/util
Date: Thu, 25 May 2006 19:03:05 -0700 (PDT)

Author: mdonoughe
Date: 2006-05-25 19:03:01 -0700 (Thu, 25 May 2006)
New Revision: 2899

Added:
   freeway/src/CIntTest.java
   freeway/src/org/gnu/freeway/cwrappers/
   freeway/src/org/gnu/freeway/cwrappers/CInt.java
   freeway/src/org/gnu/freeway/cwrappers/util/
   freeway/src/org/gnu/freeway/cwrappers/util/CWrapper.java
Log:
CInt sounded like a good one to start with. I'm looking for feedback.


Added: freeway/src/CIntTest.java
===================================================================
--- freeway/src/CIntTest.java   2006-05-25 22:27:35 UTC (rev 2898)
+++ freeway/src/CIntTest.java   2006-05-26 02:03:01 UTC (rev 2899)
@@ -0,0 +1,21 @@
+import org.gnu.freeway.cwrappers.CInt;
+
+public class CIntTest {
+       public static void main(String[] args) {
+               CInt test = new CInt(1234567890);
+               test.deserializeFromByteArray(test.serializeToByteArray());
+               System.out.println((Integer) (test.getObject()));
+               
+               test = new CInt(-1234567890);
+               test.deserializeFromByteArray(test.serializeToByteArray());
+               System.out.println((Integer) (test.getObject()));
+               
+               test = new CInt(987654321);
+               test.deserializeFromByteArray(test.serializeToByteArray());
+               System.out.println((Integer) (test.getObject()));
+               
+               test = new CInt(-987654321);
+               test.deserializeFromByteArray(test.serializeToByteArray());
+               System.out.println((Integer) (test.getObject()));
+       }
+}

Added: freeway/src/org/gnu/freeway/cwrappers/CInt.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/CInt.java     2006-05-25 22:27:35 UTC 
(rev 2898)
+++ freeway/src/org/gnu/freeway/cwrappers/CInt.java     2006-05-26 02:03:01 UTC 
(rev 2899)
@@ -0,0 +1,52 @@
+/**
+ * @PROJECT_INFO@
+ */
+package org.gnu.freeway.cwrappers;
+
+import org.gnu.freeway.cwrappers.util.CWrapper;
+
+/**
+ * 
+ */
+public class CInt implements CWrapper {
+
+       private Integer value;
+       
+       public CInt(Integer value) {
+               this.value = value;
+       }
+       
+       public CInt(int value) {
+               this.value = new Integer(value);
+       }
+       
+       /**
+        * Returns a byte array containing the integer in big endian format
+        */
+       public byte[] serializeToByteArray() {
+               byte[] retValue = {0, 0, 0, 0};
+               int intValue = value.intValue();
+               retValue[0] = (byte) (intValue >> 24);
+               retValue[1] = (byte) (intValue >> 16);
+               retValue[2] = (byte) (intValue >> 8);
+               retValue[3] = (byte) intValue;
+               return retValue;
+       }
+
+       public void deserializeFromByteArray(byte[] serializedData) {
+               // those bitmasks seem to keep the sign bit from moving around
+               value = new Integer(((serializedData[0] & 0xff) << 24) | 
((serializedData[1] & 0xff) << 16) | ((serializedData[2] & 0xff) << 8) | 
((serializedData[3] & 0xff)));
+       }
+
+       public Object getObject() {
+               return value;
+       }
+
+       public void setObject(Object data) {
+               value = (Integer) data;
+       }
+
+       public int getSerializedSize() {
+               return 4;
+       }
+}

Added: freeway/src/org/gnu/freeway/cwrappers/util/CWrapper.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/util/CWrapper.java    2006-05-25 
22:27:35 UTC (rev 2898)
+++ freeway/src/org/gnu/freeway/cwrappers/util/CWrapper.java    2006-05-26 
02:03:01 UTC (rev 2899)
@@ -0,0 +1,34 @@
+/**
+ * @PROJECT_INFO@
+ */
+
+package org.gnu.freeway.cwrappers.util;
+
+/**
+ * Implementations of this interface are used to pass arguments between
+ * Freeway and plugins that may be written in C.
+ */
+
+public interface CWrapper {
+       /**
+        * Returns a byte array representation of the contained object.
+        */
+       public byte[] serializeToByteArray();
+       /**
+        * Decodes the given byte array and updates the contained object.
+        * This function may be ignored by nonmutable types.
+        */
+       public void deserializeFromByteArray(byte[] serializedData);
+       /**
+        * Returns the contained object.
+        */
+       public Object getObject();
+       /**
+        * Update the contained object. May be ignored by nonmutable types.
+        */
+       public void setObject(Object data);
+       /**
+        * Returns the size of the byte array created when serialized.
+        */
+       public int getSerializedSize();
+}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]