package com.cn.core;

import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
/**
* 自定义序列化接口
*/
public abstract class Serializer { public static final Charset CHARSET = Charset.forName("UTF-8"); protected ChannelBuffer writeBuffer; protected ChannelBuffer readBuffer; /**
* 反序列化具体实现
*/
protected abstract void read(); /**
* 序列化具体实现
*/
protected abstract void write(); /**
* 从byte数组获取数据
*/
public Serializer readFromBytes(byte[] bytes) {
readBuffer = BufferFactory.getBuffer(bytes);
read();
readBuffer.clear();
return this;
} /**
* 从buff获取数据
*/
public void readFromBuffer(ChannelBuffer readBuffer) {
this.readBuffer = readBuffer;
read();
} /**
* 写入本地buff
*/
public ChannelBuffer writeToLocalBuff(){
writeBuffer = BufferFactory.getBuffer();
write();
return writeBuffer;
} /**
* 写入目标buff
*/
public ChannelBuffer writeToTargetBuff(ChannelBuffer buffer){
writeBuffer = buffer;
write();
return writeBuffer;
} /**
* 返回buffer数组
*/
public byte[] getBytes() {
writeToLocalBuff();
byte[] bytes = null;
if (writeBuffer.writerIndex() == 0) {
bytes = new byte[0];
} else {
bytes = new byte[writeBuffer.writerIndex()];
writeBuffer.readBytes(bytes);
}
writeBuffer.clear();
return bytes;
} public byte readByte() {
return readBuffer.readByte();
} public short readShort() {
return readBuffer.readShort();
} public int readInt() {
return readBuffer.readInt();
} public long readLong() {
return readBuffer.readLong();
} public float readFloat() {
return readBuffer.readFloat();
} public double readDouble() {
return readBuffer.readDouble();
} public String readString() {
int size = readBuffer.readShort();
if (size <= 0) {
return "";
} byte[] bytes = new byte[size];
readBuffer.readBytes(bytes); return new String(bytes, CHARSET);
} public <T> List<T> readList(Class<T> clz) {
List<T> list = new ArrayList<>();
int size = readBuffer.readShort();
for (int i = 0; i < size; i++) {
list.add(read(clz));
}
return list;
} public <K,V> Map<K,V> readMap(Class<K> keyClz, Class<V> valueClz) {
Map<K,V> map = new HashMap<>();
int size = readBuffer.readShort();
for (int i = 0; i < size; i++) {
K key = read(keyClz);
V value = read(valueClz);
map.put(key, value);
}
return map;
} @SuppressWarnings("unchecked")
public <I> I read(Class<I> clz) {
Object t = null;
if ( clz == int.class || clz == Integer.class) {
t = this.readInt();
} else if (clz == byte.class || clz == Byte.class){
t = this.readByte();
} else if (clz == short.class || clz == Short.class){
t = this.readShort();
} else if (clz == long.class || clz == Long.class){
t = this.readLong();
} else if (clz == float.class || clz == Float.class){
t = readFloat();
} else if (clz == double.class || clz == Double.class){
t = readDouble();
} else if (clz == String.class ){
t = readString();
} else if (Serializer.class.isAssignableFrom(clz)){
try {
byte hasObject = this.readBuffer.readByte();
if(hasObject == 1){
Serializer temp = (Serializer)clz.newInstance();
temp.readFromBuffer(this.readBuffer);
t = temp;
}else{
t = null;
}
} catch (Exception e) {
e.printStackTrace();
} } else {
throw new RuntimeException(String.format("不支持类型:[%s]", clz));
}
return (I) t;
} public Serializer writeByte(Byte value) {
writeBuffer.writeByte(value);
return this;
} public Serializer writeShort(Short value) {
writeBuffer.writeShort(value);
return this;
} public Serializer writeInt(Integer value) {
writeBuffer.writeInt(value);
return this;
} public Serializer writeLong(Long value) {
writeBuffer.writeLong(value);
return this;
} public Serializer writeFloat(Float value) {
writeBuffer.writeFloat(value);
return this;
} public Serializer writeDouble(Double value) {
writeBuffer.writeDouble(value);
return this;
} public <T> Serializer writeList(List<T> list) {
if (isEmpty(list)) {
writeBuffer.writeShort((short) 0);
return this;
}
writeBuffer.writeShort((short) list.size());
for (T item : list) {
writeObject(item);
}
return this;
} public <K,V> Serializer writeMap(Map<K, V> map) {
if (isEmpty(map)) {
writeBuffer.writeShort((short) 0);
return this;
}
writeBuffer.writeShort((short) map.size());
for (Entry<K, V> entry : map.entrySet()) {
writeObject(entry.getKey());
writeObject(entry.getValue());
}
return this;
} public Serializer writeString(String value) {
if (value == null || value.isEmpty()) {
writeShort((short) 0);
return this;
} byte data[] = value.getBytes(CHARSET);
short len = (short) data.length;
writeBuffer.writeShort(len);
writeBuffer.writeBytes(data);
return this;
} public Serializer writeObject(Object object) { if(object == null){
writeByte((byte)0);
}else{
if (object instanceof Integer) {
writeInt((int) object);
return this;
} if (object instanceof Long) {
writeLong((long) object);
return this;
} if (object instanceof Short) {
writeShort((short) object);
return this;
} if (object instanceof Byte) {
writeByte((byte) object);
return this;
} if (object instanceof String) {
String value = (String) object;
writeString(value);
return this;
}
if (object instanceof Serializer) {
writeByte((byte)1);
Serializer value = (Serializer) object;
value.writeToTargetBuff(writeBuffer);
return this;
} throw new RuntimeException("不可序列化的类型:" + object.getClass());
} return this;
} private <T> boolean isEmpty(Collection<T> c) {
return c == null || c.size() == 0;
}
public <K,V> boolean isEmpty(Map<K,V> c) {
return c == null || c.size() == 0;
}
} /**
* buff工厂
*/
class BufferFactory {
public static ByteOrder BYTE_ORDER = ByteOrder.BIG_ENDIAN;
/**
* 获取一个buffer
*/
public static ChannelBuffer getBuffer() {
ChannelBuffer dynamicBuffer = ChannelBuffers.dynamicBuffer();
return dynamicBuffer;
}
/**
* 将数据写入buffer
*/
public static ChannelBuffer getBuffer(byte[] bytes) {
ChannelBuffer copiedBuffer = ChannelBuffers.copiedBuffer(bytes);
return copiedBuffer;
}
}

要序列化的对象:

package com.cn;

import java.util.ArrayList;
import java.util.List; import com.cn.core.Serializer; public class Player extends Serializer{//继承序列化接口,实现read和write接口 private long playerId; private int age; private List<Integer> skills = new ArrayList<>(); private Resource resource = new Resource(); public Resource getResource() {
return resource;
} public void setResource(Resource resource) {
this.resource = resource;
} public long getPlayerId() {
return playerId;
} public void setPlayerId(long playerId) {
this.playerId = playerId;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public List<Integer> getSkills() {
return skills;
} public void setSkills(List<Integer> skills) {
this.skills = skills;
} @Override
protected void read() {
this.playerId = readLong();
this.age = readInt();
this.skills = readList(Integer.class);
this.resource = read(Resource.class);
} @Override
protected void write() {
writeLong(playerId);
writeInt(age);
writeList(skills);
writeObject(resource);
} }
package com.cn;

import com.cn.core.Serializer;

public class Resource extends Serializer {

    private int gold;

    public int getGold() {
return gold;
} public void setGold(int gold) {
this.gold = gold;
} @Override
protected void read() {
this.gold = readInt();
} @Override
protected void write() {
writeInt(gold);
} }
package com.cn;

import java.util.Arrays;

public class Test4 {

    public static void main(String[] args) {

        Player player = new Player();
player.setPlayerId(10001);
player.setAge(22);
player.getSkills().add(101);
player.getResource().setGold(99999); byte[] bytes = player.getBytes(); System.out.println(Arrays.toString(bytes)); //============================================== Player player2 = new Player();
player2.readFromBytes(bytes);
System.out.println(player2.getPlayerId() + " "+player2.getAge() + " "+ Arrays.toString(player2.getSkills().toArray())+" " +player2.getResource().getGold()); } }

最新文章

  1. protobuf学习(1)-ubuntu14.04下protobuf2.6安装
  2. iOS UIButton 设置图片不变型setImage
  3. C++学习笔记22:设备
  4. Cloning EBS from Linux 5 to Linux 6 Fails: &quot;Error While Loading Shared Libraries: libclntsh.so.10.1
  5. oracle 得到新插入数据的ID并使用
  6. jquery学习笔记1
  7. C#分部方法
  8. Linux通配符摘要
  9. elasticsearch最佳实践
  10. Leetcode 136 137 260 SingleNumber I II III
  11. 实体处理模块IEntityModule
  12. Zookeeper运维
  13. Realm 处理List&lt;String&gt; 问题 Type parameter &#39;java.lang.String&#39; is not within its bound; should implement &#39;io.realm.RealmModel
  14. Scrapy at a glance预览
  15. 【二分】Producing Snow @Codeforces Round #470 Div.2 C
  16. ArcGIS AddIN 与ArcMap自带工具进行交互
  17. EasyUI自动消失的弹框
  18. day81
  19. 小程序报错数据传输长度为 xxx 已经超过最大长度 xxx
  20. 阿里云ECS 固定带宽变为按量付费的方式

热门文章

  1. Spring_day03--Spring的事务管理
  2. 在静态工具类中需要注入mapper
  3. hdu1071(定积分求面积)
  4. DNS rebinging攻击方式
  5. mysql如何用sql添加字段如何设置字符集和排序规则
  6. PHP 开发环境的搭建和使用03-- 安装mySql
  7. 02.ZooKeeper的Java客户端使用
  8. 160426、JavaScript 秘密花园
  9. QA规范
  10. JAR包中的MANIFEST.MF文件详解以及编写规范