[zb4osgi-changeset] [scm] ZigBee 4 OSGi repository change: r1151 - in /projects/zb4osgi/trunk/zigbee.common/src: main/java/it/cnr/isti/primitivetypes/util/Integers.java test/java/it/cnr/isti/primitivetypes/util/IntegersTest.java

scm-notify at zb4osgi.aaloa.org scm-notify at zb4osgi.aaloa.org
Wed Jan 14 18:39:16 CET 2015


Author: giancarlo.riolo
Date: Wed Jan 14 18:39:16 2015
New Revision: 1151

Log:
Zigbee.common  (refs #262) Read and Write 24 bit needs more investigation (negative numbers rapresentation) 32 bits works 

Modified:
    projects/zb4osgi/trunk/zigbee.common/src/main/java/it/cnr/isti/primitivetypes/util/Integers.java
    projects/zb4osgi/trunk/zigbee.common/src/test/java/it/cnr/isti/primitivetypes/util/IntegersTest.java

Modified: projects/zb4osgi/trunk/zigbee.common/src/main/java/it/cnr/isti/primitivetypes/util/Integers.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.common/src/main/java/it/cnr/isti/primitivetypes/util/Integers.java	(original)
+++ projects/zb4osgi/trunk/zigbee.common/src/main/java/it/cnr/isti/primitivetypes/util/Integers.java	Wed Jan 14 18:39:16 2015
@@ -358,12 +358,6 @@
 		ByteBuffer.wrap(dest, pos, 4).order(ByteOrder.LITTLE_ENDIAN)
 				.putInt(data);
 		return 4;
-
-		/*
-		 * dest[pos + 3] = (byte) (data >> 24); dest[pos + 2] = (byte) ((data <<
-		 * 8) >> 24); dest[pos + 1] = (byte) ((data << 16) >> 24); dest[pos + 0]
-		 * = (byte) ((data << 24) >> 24); return 4;
-		 */
 	}
 
 	final public static int writeLongObject(byte[] dest, int pos, Long data) {
@@ -458,7 +452,16 @@
 	 * @since 0.2.0
 	 */
 	public static int readInt24bit(byte[] src, int pos) {
-		return (((src[pos] & 0xFF) << 8) + ((src[pos + 1] & 0xFF) << 16) + ((src[pos + 2] & 0xFF) << 24)) >> 8;
+
+		int[] buffer = new int[3];
+		ByteBuffer b = ByteBuffer.wrap(src);
+		buffer[0] = b.get(pos+2);
+		buffer[1] = b.get(pos+1);
+		buffer[2] = b.get(pos);
+			
+		int result = ((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8)) >> 8;
+
+		return result;
 	}
 
 	/**
@@ -475,9 +478,17 @@
 	 * @since 0.2.0
 	 */
 	public static int writeInt24bit(byte[] dest, int pos, int data) {
-		dest[pos] = (byte) ((data & 0xFF0000) >> 16);
-		dest[pos + 1] = (byte) ((data & 0x00FF00) >> 8);
-		dest[pos + 2] = (byte) ((data & 0x0000FF));
+		
+		
+		ByteBuffer b = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
+		b.putInt(data);		
+		byte[] result = b.array();		
+		
+		
+		 dest[pos] = (byte) ((result[1] & 0xFF0000) >> 16);
+	        dest[pos + 1] = (byte) ((result[2] & 0x00FF00) >> 8);
+	        dest[pos + 2] = (byte) ((result[3] & 0x0000FF));
+	    
 		return 3;
 	}
 

Modified: projects/zb4osgi/trunk/zigbee.common/src/test/java/it/cnr/isti/primitivetypes/util/IntegersTest.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.common/src/test/java/it/cnr/isti/primitivetypes/util/IntegersTest.java	(original)
+++ projects/zb4osgi/trunk/zigbee.common/src/test/java/it/cnr/isti/primitivetypes/util/IntegersTest.java	Wed Jan 14 18:39:16 2015
@@ -30,9 +30,6 @@
 
 import java.math.BigInteger;
 import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
 
 import org.junit.Test;
 
@@ -318,9 +315,9 @@
 			idx += Integers.writeLong(buffer, idx, expected[i], 8);
 		}
 		for (int i = 0; i < expected.length; i++) {
-			
-		 long value = Integers.readLong(buffer, i * 8);
-		 assertEquals("failed to write with K=8", expected[i], value);
+
+			long value = Integers.readLong(buffer, i * 8);
+			assertEquals("failed to write with K=8", expected[i], value);
 		}
 
 		for (int k = MAX_SIZE; k < MIN_SIZE; k--) {
@@ -344,18 +341,16 @@
 
 		assertArrayEquals(original, single);
 
-		// broken test
-		// TODO:Debug
-		// int[] expected = new int[] { 0, 1, -1, 256, -256, 65793, -65793 };
-		// byte[] buffer = new byte[3 * expected.length];
-		// int idx = 0;
-		// for (int i = 0; i < expected.length; i++) {
-		// idx += Integers.writeInt24bit(buffer, idx, expected[i]);
-		// }
-		// for (int i = 0; i < expected.length; i++) {
-		// int value = Integers.readInt24bit(buffer, i * 3);
-		// assertEquals(expected[i], value);
-		// }
+		int[] expected = new int[] { 0, 1, -1, 256, -256, 65793, -65793 };
+		byte[] buffer = new byte[3 * expected.length];
+		int idx = 0;
+		for (int i = 0; i < expected.length; i++) {
+			idx += Integers.writeInt24bit(buffer, idx, expected[i]);
+		}
+		for (int i = 0; i < expected.length; i++) {
+	//		int value = Integers.readInt24bit(buffer, i * 3);
+	//		assertEquals(expected[i], value);
+		}
 
 	}
 
@@ -506,7 +501,7 @@
 		// checking full 8 bytes write
 		Integers.writeLong(test64, 0, l, 8);
 		assertArrayEquals(expected, test64);
-		
+
 		// checking partial write
 		for (int i = 1; i <= 8; i++) {
 			byte[] test = new byte[i];




More information about the Commit mailing list