CompressRpcCodec extends AbstractCodec { @Override public byte[] encode(Channel channel, Object message) throws IOException { if (needEncodeV1(message)) { return v1Codec.encode(channel, message); } else { return encodeV2(channel, message); } }  @Override byte[] encode(Channel channel, Object message); @Override Object decode(Channel channel, String remoteIp, byte[] data); byte[] encodeV2(Channel channel, Object message); Object decodeV2(Channel channel, String remoteIp, byte[] data); static InputStream getInputStream(byte[] data); byte[] compress(byte[] org, boolean useGzip, int minGzSize); static void putMethodSign(Provider<?> provider, List<Method> methods); static void putMethodSign(String methodSign, MethodInfo methodInfo); static final String CODEC_VERSION_SWITCHER; static final String GROUP_CODEC_VERSION_SWITCHER; }
@Test public void testSwitcher() throws IOException { DefaultRequest request = getRequest("void", null); byte[] bytes = rpcCodec.encode(channel, request); assertTrue(isCompressVersion(bytes)); MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.CODEC_VERSION_SWITCHER, true); bytes = rpcCodec.encode(channel, request); assertTrue(isV1Version(bytes)); MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.CODEC_VERSION_SWITCHER, false); MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.GROUP_CODEC_VERSION_SWITCHER + URLParamType.group.getValue(), true); bytes = rpcCodec.encode(channel, request); assertTrue(isV1Version(bytes)); }
@Test public void testGzip() throws IOException { DefaultRequest request = getRequest("int[]", new Object[] {new int[] {1, 2}}); byte[] bytes = rpcCodec.encode(channel, request); assertFalse(isGzip(bytes)); int bodyLength = ByteUtil.bytes2int(bytes, 12); URL url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello"); Map<String, String> params = url.getParameters(); params.put(URLParamType.usegz.name(), "true"); params.put(URLParamType.mingzSize.name(), String.valueOf(bodyLength - 1)); Channel tempChannel = new MockChannel(url); bytes = rpcCodec.encode(tempChannel, request); assertTrue(isGzip(bytes)); url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello"); params = url.getParameters(); params.put(URLParamType.usegz.name(), "true"); params.put(URLParamType.mingzSize.name(), String.valueOf(bodyLength)); tempChannel = new MockChannel(url); bytes = rpcCodec.encode(tempChannel, request); assertFalse(isGzip(bytes)); }