Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ bin/
.project
.class
.jar
*.swp
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.zvidia</groupId>
<artifactId>pomelo</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0-websocket</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -55,4 +55,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
18 changes: 16 additions & 2 deletions src/main/java/com/zvidia/pomelo/protobuf/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,29 @@ public static byte[] encodeUInt32(int num) {
}

public static int decodeUInt32(byte[] bytes) {
return ByteUtils.bytesToInt(bytes);
int result = 0;
for(int i=0; i<bytes.length; i++)
{
byte tmp = bytes[i];
result += (tmp & 0x7f) << 7 * i;
if(tmp > 0)
{
return result;
}
}

return result;
}

public static byte[] encodeSInt32(int num) {
return ByteUtils.intToBytes(num);
}

public static int decodeSInt32(byte[] bytes) {
return ByteUtils.bytesToInt(bytes);
int uResult = Codec.decodeUInt32(bytes);
int flag = ((uResult % 2) == 1 ) ? -1 : 1;

return ((uResult % 2 + uResult)/2) * flag;
}

public static byte[] encodeUInt64(long num) {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/zvidia/pomelo/protobuf/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ private Object decodeProp(String type, JSONObject proto) throws JSONException {
}
case _int32:
case _sInt32: {
return getByte() & 0xff;
return Codec.decodeSInt32(getBytes(false));// & 0xff;
}
case _float: {
float aFloat = buffer.getFloat(offset);
float aFloat = buffer.getFloat();
offset += 4;
return aFloat;
}
case _double: {
double aDouble = buffer.getDouble(offset);
double aDouble = buffer.getDouble();
offset += 8;
return aDouble;
}
Expand Down Expand Up @@ -184,12 +184,16 @@ private byte getByte() {
}

private byte[] getBytes(boolean flag) {
ByteBuffer buf = ByteBuffer.allocate(4);
ByteBuffer buf = ByteBuffer.allocate(5);
int pos = offset;
flag = flag || false;
int b = buffer.getInt(pos);
buf.putInt(b);
pos += 4;
byte b;
do{
b = buffer.get();
buf.put(b);
pos += 1;
}while(b<0);

if (!flag) {
offset = pos;
buffer.position(pos);
Expand Down