Commits

Tobias Westerblom committed 8a749bb

fixed unint64 decoding bug

  • Participants
  • Parent commits e3c6ab6

Comments (0)

Files changed (2)

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

 
     public long loadUnsignedInt() {
         long uint = 0;
-        uint = uint | inBuffer.get() << 24;
-        uint = uint | inBuffer.get() << 16;
-        uint = uint | inBuffer.get() << 8;
-        uint = uint | inBuffer.get();
+        uint = uint | (inBuffer.get() & 0xff) << 24;
+        uint = uint | (inBuffer.get() & 0xff) << 16;
+        uint = uint | (inBuffer.get() & 0xff) << 8;
+        uint = uint | (inBuffer.get() & 0xff);
         return uint;
     }
     

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

 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.IOException;
+import java.io.InputStream;
 import java.nio.ByteBuffer;
 
 import junit.framework.TestCase;
     	assertEquals(99, unpacked.game);
     }
     
+    public void testReadWriteUnsignedInt64() throws Exception {
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        DataOutputStream dos = new DataOutputStream(os);
+        PacketOutputStream pos = new PacketOutputStream(dos);
+        long unsignedIntValue = 1234L;
+        pos.saveUnsignedInt(unsignedIntValue);
+
+        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 unsignedIntResult = pis.loadUnsignedInt();
+        assertThat(unsignedIntResult, is(unsignedIntValue));
+    }    
+    
     private String randomString(int length) {
         char[] chars = new char[] {'a', 'b', 'c', 'd'};
         StringBuffer stringBuffer = new StringBuffer();