Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #732 from Zha-Zha/feature/zkSerialize
Optimize zookeeper serialization method
- Loading branch information
Showing
3 changed files
with
34 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...stry-zookeeper/src/main/java/com/weibo/api/motan/registry/zookeeper/StringSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.weibo.api.motan.registry.zookeeper; | ||
|
||
import com.weibo.api.motan.util.ByteUtil; | ||
import org.I0Itec.zkclient.exception.ZkMarshallingError; | ||
import org.I0Itec.zkclient.serialize.SerializableSerializer; | ||
|
||
import java.io.ObjectStreamConstants; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
public class StringSerializer extends SerializableSerializer { | ||
@Override | ||
public Object deserialize(byte[] bytes) throws ZkMarshallingError { | ||
try { | ||
if (ByteUtil.bytes2short(bytes, 0) == ObjectStreamConstants.STREAM_MAGIC) { | ||
return super.deserialize(bytes); | ||
} | ||
return new String(bytes, "UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new ZkMarshallingError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] serialize(Object obj) throws ZkMarshallingError { | ||
try { | ||
return obj.toString().getBytes("UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new ZkMarshallingError(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters