Commits

Peter Lundh  committed 6b63abb

fixed unpacking of uint32 and uint16

  • Participants
  • Parent commits b039d23

Comments (0)

Files changed (3)

File core/src/main/java/com/cubeia/firebase/io/PacketInputStream.java

  * Handles the deserialization of the Styx wire format.
  */
 public final class PacketInputStream {
+	
+	public static final long MAX_UNSIGNED_INTEGER = (long) Math.pow(2,32);
+	public static final long MAX_UNSIGNED_SHORT = (long) Math.pow(2,16);
+	
 	private final ByteBuffer inBuffer;
 
     public PacketInputStream(ByteBuffer inBuffer) {
     }
 
     public int loadUnsignedShort() throws IOException {
-        return BinaryData.asUnsigned(inBuffer.getShort());
+        int uint = 0;
+        uint = uint | (inBuffer.get() & 0xff) << 8;
+        uint = uint | (inBuffer.get() & 0xff);
+        if ( uint < 0 ) {
+        	uint += MAX_UNSIGNED_SHORT;
+        }
+        return uint;
     }
 
     public short loadShort() throws IOException {
         uint = uint | (inBuffer.get() & 0xff) << 16;
         uint = uint | (inBuffer.get() & 0xff) << 8;
         uint = uint | (inBuffer.get() & 0xff);
+        if ( uint < 0 ) {
+        	uint += MAX_UNSIGNED_INTEGER;
+        }
         return uint;
     }
     

File core/src/main/java/com/cubeia/firebase/io/PacketOutputStream.java

 
 import java.io.DataOutputStream;
 import java.io.IOException;
-import java.nio.ByteBuffer;
 
 /**
  * Handles the serialization of the Styx wire format.
     }
     
     public void saveUnsignedInt(long val) throws IOException {
-    	int b1,b2,b3,b4;
-    	ByteBuffer bb = ByteBuffer.allocate(4);
-    	b1 = (int) ((val & 0xff000000) >>> 24);
-    	b2 = (int) ((val & 0x00ff0000) >>> 16);
-    	b3 = (int) ((val & 0x0000ff00) >>> 8);
-    	b4 = (int) ((val & 0x000000ff));
-    	
-    	bb.put((byte) (b1 & 0xff));
-    	bb.put((byte) (b2 & 0xff));
-    	bb.put((byte) (b3 & 0xff));
-    	bb.put((byte) (b4 & 0xff));
-        
-    	os.write(bb.array());
-        
+    	 os.write((byte) (0xff & val >>> 24));
+    	 os.write((byte) (0xff & val >>> 16));
+    	 os.write((byte) (0xff & val >>> 8));
+    	 os.write((byte) (0xff & val));
     }
     
     public void saveLong(long val) throws IOException {

File core/src/test/java/com/cubeia/firebase/protocol/PacketInputStreamTest.java

 package com.cubeia.firebase.protocol;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.InputStream;
 import java.nio.ByteBuffer;
 
+import org.junit.Ignore;
+import org.junit.Test;
+
 import junit.framework.TestCase;
 
 import com.cubeia.firebase.io.PacketInputStream;
+import com.cubeia.firebase.io.PacketOutputStream;
 
 public class PacketInputStreamTest extends TestCase {
 
 		assertEquals(4, in.loadByte());
 		assertEquals(-1, in.peek()); // end of stream	
 	}
+	
+	@Ignore
+	@Test
+	public void uint32Test() throws Exception {
+		for ( long value = 0; value < PacketInputStream.MAX_UNSIGNED_INTEGER; value ++ ) {
+		   ByteArrayOutputStream os = new ByteArrayOutputStream();
+	        DataOutputStream dos = new DataOutputStream(os);
+	        PacketOutputStream pos = new PacketOutputStream(dos);
+	        
+	        pos.saveUnsignedInt(value);
+
+	        byte[] byteBuffer = os.toByteArray();
+	        InputStream is = new ByteArrayInputStream(byteBuffer);
+	        DataInputStream dis = new DataInputStream(is);
+	        byte[] ba = new byte[200];
+	        dis.read(ba);
+	        PacketInputStream pis = new PacketInputStream(ByteBuffer.wrap(ba));
+	        
+	        long result = pis.loadUnsignedInt();
+	        assertThat(result, is(value));
+	        
+		}
+	}
+	
+	@Ignore
+	@Test
+	public void uint16Test() throws Exception {
+		for ( int value = 0; value < PacketInputStream.MAX_UNSIGNED_SHORT; value ++ ) {
+		   ByteArrayOutputStream os = new ByteArrayOutputStream();
+	        DataOutputStream dos = new DataOutputStream(os);
+	        PacketOutputStream pos = new PacketOutputStream(dos);
+	        
+	        pos.saveUnsignedShort(value);
+
+	        byte[] byteBuffer = os.toByteArray();
+	        InputStream is = new ByteArrayInputStream(byteBuffer);
+	        DataInputStream dis = new DataInputStream(is);
+	        byte[] ba = new byte[200];
+	        dis.read(ba);
+	        PacketInputStream pis = new PacketInputStream(ByteBuffer.wrap(ba));
+	        
+	        int result = pis.loadUnsignedShort();
+	        assertThat(result, is(value));
+	        
+		}
+	}
+
 }
+