From 34aa47e5fdb386e9e811177e75c266161a7f058f Mon Sep 17 00:00:00 2001 From: linda Date: Thu, 30 Jul 2015 17:21:39 +0800 Subject: [PATCH] misc:add zip encode to serializer --- .../com/linda/framework/rpc/nio/RpcNioConnector.java | 8 ++++---- .../linda/framework/rpc/serializer/JdkSerializer.java | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/linda/framework/rpc/nio/RpcNioConnector.java b/src/main/java/com/linda/framework/rpc/nio/RpcNioConnector.java index f3375d4..28d5944 100644 --- a/src/main/java/com/linda/framework/rpc/nio/RpcNioConnector.java +++ b/src/main/java/com/linda/framework/rpc/nio/RpcNioConnector.java @@ -53,10 +53,10 @@ public RpcNioConnector(){ } private void initBuf(){ - channelWriteBuffer = ByteBuffer.allocate(RpcUtils.MEM_8KB); - channelReadBuffer = ByteBuffer.allocate(RpcUtils.MEM_8KB); - rpcNioReadBuffer = new RpcNioBuffer(RpcUtils.MEM_8KB); - rpcNioWriteBuffer = new RpcNioBuffer(RpcUtils.MEM_8KB); + channelWriteBuffer = ByteBuffer.allocate(RpcUtils.MEM_32KB); + channelReadBuffer = ByteBuffer.allocate(RpcUtils.MEM_32KB); + rpcNioReadBuffer = new RpcNioBuffer(RpcUtils.MEM_32KB); + rpcNioWriteBuffer = new RpcNioBuffer(RpcUtils.MEM_32KB); } @Override diff --git a/src/main/java/com/linda/framework/rpc/serializer/JdkSerializer.java b/src/main/java/com/linda/framework/rpc/serializer/JdkSerializer.java index 0c676a6..4c514bb 100644 --- a/src/main/java/com/linda/framework/rpc/serializer/JdkSerializer.java +++ b/src/main/java/com/linda/framework/rpc/serializer/JdkSerializer.java @@ -6,6 +6,7 @@ import java.io.ObjectOutputStream; import com.linda.framework.rpc.exception.RpcException; +import com.linda.framework.rpc.utils.NioUtils; public class JdkSerializer implements RpcSerializer { @@ -16,7 +17,9 @@ public byte[] serialize(Object obj) { ObjectOutputStream stream = new ObjectOutputStream(bis); stream.writeObject(obj); stream.close(); - return bis.toByteArray(); + byte[] bytes = bis.toByteArray(); + //使用zip压缩,缩小网络包 + return NioUtils.zip(bytes); } catch (Exception e) { throw new RpcException(e); } @@ -25,7 +28,9 @@ public byte[] serialize(Object obj) { @Override public Object deserialize(byte[] bytes) { try { - ByteArrayInputStream bis = new ByteArrayInputStream(bytes); + //使用zip解压缩 + byte[] unzip = NioUtils.unzip(bytes); + ByteArrayInputStream bis = new ByteArrayInputStream(unzip); ObjectInputStream stream = new ObjectInputStream(bis); return stream.readObject(); } catch (Exception e) {