Skip to content

Commit

Permalink
Merge pull request #635 from weibocom/featurn/simpleMultiSerialize
Browse files Browse the repository at this point in the history
support multi serialize in simpleSerialization
  • Loading branch information
rayzhang0603 authored Jan 17, 2018
2 parents c603561 + bc33c27 commit b938578
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public class SimpleSerialization implements Serialization {
@Override
public byte[] serialize(Object obj) throws IOException {
GrowableByteBuffer buffer = new GrowableByteBuffer(4096);
serialize(obj, buffer);
buffer.flip();
byte[] result = new byte[buffer.remaining()];
buffer.get(result);
return result;
}

private void serialize(Object obj, GrowableByteBuffer buffer) throws IOException {
if (obj != null) {
if (obj instanceof String) {
buffer.put((byte) 1);
Expand All @@ -48,19 +56,20 @@ public byte[] serialize(Object obj) throws IOException {
buffer.put(b);
} else if (obj instanceof Map) {
buffer.put((byte) 2);
int pos = buffer.position();
int size = 0;
buffer.position(5);
buffer.position(pos + 4);
for (Entry<Object, Object> entry : ((Map<Object, Object>) obj).entrySet()) {
if (entry.getKey() != null && entry.getValue() != null
&& (entry.getKey() instanceof String) && (entry.getValue() instanceof String)) {
size += putString(buffer, (String) entry.getKey());
size += putString(buffer, (String) entry.getValue());
}
}
buffer.position(1);
buffer.position(pos);
buffer.putInt(size);
buffer.position(5 + size);
} else if(obj instanceof byte[]){
buffer.position(pos + size + 4);
} else if (obj instanceof byte[]) {
buffer.put((byte) 3);
byte[] b = (byte[]) obj;
buffer.putInt(b.length);
Expand All @@ -71,15 +80,15 @@ public byte[] serialize(Object obj) throws IOException {
} else {
buffer.put((byte) 0);
}
buffer.flip();
byte[] result = new byte[buffer.remaining()];
buffer.get(result);
return result;
}

@Override
public <T> T deserialize(byte[] bytes, Class<T> clz) throws IOException {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
return deserialize(buffer, clz);
}

private <T> T deserialize(ByteBuffer buffer, Class<T> clz) throws IOException {
byte type = buffer.get();
switch (type) {
case 0:
Expand Down Expand Up @@ -111,6 +120,7 @@ public <T> T deserialize(byte[] bytes, Class<T> clz) throws IOException {
key = getString(buffer);
}
}
buffer.limit(buffer.capacity());
return (T) map;
} else {
throw new MotanServiceException("SimpleSerialization not support type:" + clz);
Expand All @@ -125,30 +135,24 @@ public <T> T deserialize(byte[] bytes, Class<T> clz) throws IOException {

@Override
public byte[] serializeMulti(Object[] data) throws IOException {
if (data.length == 1) {
return serialize(data[0]);
GrowableByteBuffer buffer = new GrowableByteBuffer(4096);
for (Object o : data) {
serialize(o, buffer);
}
//TODO mulit param support
throw new MotanServiceException("SimpleSerialization not support serialize multi Object");
buffer.flip();
byte[] result = new byte[buffer.remaining()];
buffer.get(result);
return result;
}

@Override
public Object[] deserializeMulti(byte[] data, Class<?>[] classes) throws IOException {
if (classes.length == 1) {
return new Object[]{deserialize(data, classes[0])};
} else {
StringBuilder sb = new StringBuilder(128);
sb.append("[");
for (Class c : classes) {
sb.append(c.getName()).append(",");
}
if (sb.length() > 1) {
sb.deleteCharAt(sb.length() - 1);
}
sb.append("]");
throw new MotanServiceException("SimpleSerialization not support deserialize multi Object of " + classes);
ByteBuffer buffer = ByteBuffer.wrap(data);
Object[] result = new Object[classes.length];
for (int i = 0; i < classes.length; i++) {
result[i] = deserialize(buffer, classes[i]);
}

return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void serialize() throws Exception {
assertEquals(entry.getValue(), m2.get(entry.getKey()));
}

byte[] bytes = new byte[]{2,34,12,24};
byte[] bytes = new byte[]{2, 34, 12, 24};
b = serialization.serialize(bytes);
assertNotNull(b);
assertTrue(b.length > 0);
Expand All @@ -67,5 +67,37 @@ public void serialize() throws Exception {
}
}

@Test
public void testSerializeMulti() throws Exception {
SimpleSerialization serialization = new SimpleSerialization();
Object[] objects = new Object[3];
objects[0] = "teststring";
Map<String, String> map = new HashMap<String, String>();
map.put("name", "ray");
map.put("code", "xxx");
objects[1] = map;
byte[] bytes = new byte[]{2, 34, 12, 24};
objects[2] = bytes;

byte[] b = serialization.serializeMulti(objects);
assertNotNull(b);
assertTrue(b.length > 0);

Object[] result = serialization.deserializeMulti(b, new Class[]{String.class, Map.class, byte[].class});
assertEquals(3, result.length);
assertTrue(result[0] instanceof String);
assertEquals(result[0], objects[0]);
assertTrue(result[1] instanceof Map);
Map<String, String> map2 = (Map<String, String>) result[1];
for (Map.Entry entry : map.entrySet()) {
assertEquals(entry.getValue(), map2.get(entry.getKey()));
}
assertTrue(result[2] instanceof byte[]);

byte[] nbytes = (byte[]) result[2];
for (int i = 0; i < nbytes.length; i++) {
assertEquals(nbytes[i], bytes[i]);
}
}

}

0 comments on commit b938578

Please # to comment.