1.自己定义方法
优点:不同预先设置缓存大小
?缺点:不方便阅读,以及编写
?
package com.chalmers.change; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Arrays; /** * @author Chalmers 2016年2月22日 下午2:55:02 */ public class Change { public static void main(String[] args) throws IOException { Change c = new Change(); int id = 101; int age = 20; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //将属性写入 byteArrayOutputStream.write(c.int2Byte(id)); byteArrayOutputStream.write(c.int2Byte(age)); //转换成字节数组 byte[] buf = byteArrayOutputStream.toByteArray(); System.out.println(Arrays.toString(buf)); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( buf); //转换 byte[] id_byte = new byte[4]; byteArrayInputStream.read(id_byte); System.out.println("id: " + c.byte2Int(id_byte)); byte[] age_byte = new byte[4]; byteArrayInputStream.read(age_byte); System.out.println("age: " + c.byte2Int(age_byte)); } // 将int转换成byte类型 public byte[] int2Byte(int i) { byte[] bytes = new byte[4]; bytes[0] = (byte) (i >> 3 * 8); bytes[1] = (byte) (i >> 2 * 8); bytes[2] = (byte) (i >> 1 * 8); bytes[3] = (byte) (i >> 0 * 8); return bytes; } // 将byte转换成int类型 public int byte2Int(byte[] bytes) { return bytes[0] << 3 * 8 | bytes[1] << 2 * 8 | bytes[2] << 1 * 8 | bytes[3] << 0 * 8; } }
?
?
2.使用NIO中的ByteBuffer方法
优点:使用方便,不用引入额外的jar包
缺点:需要预先设置缓冲大小,不然会报错
package com.chalmers.change; import java.nio.ByteBuffer; import java.util.Arrays; /** * @author Chalmers * 2016年2月22日 下午9:33:59 */ /** * 将数据序列化 * 优点:使用方便,不用引入额外的jar包 * 缺点:需要预先设置缓冲大小,不然会报错 */ public class Change2 { public static void main(String[] args) { int id = 101; int age = 20; //设置空间大小为8 ByteBuffer byteBuffer = ByteBuffer.allocate(8); //将变量存入 byteBuffer.putInt(id); byteBuffer.putInt(age); //转成字节数组 byte[] buf = byteBuffer.array(); System.out.println(Arrays.toString(buf)); //将字节数组放入byteBuffer中 ByteBuffer byteBuffer2 = ByteBuffer.wrap(buf); //一个个的读出来 System.out.println("id: "+byteBuffer2.getInt()); System.out.println("age: "+byteBuffer2.getInt()); } }
?
?
3.1使用netty3的jar包中的ChannelBuffer类
这段代码只测试了简单的int类型,double等类型,而String类型需要额外的一些步骤,所以单独编写了。
优点:操作方便,不用预先设置大小
缺点:需要引入netty3的jar包
package com.chalmers.change; import java.util.Arrays; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; /** * @author Chalmers 2016年2月22日 下午9:42:37 */ /** * 序列化 * 优点:操作方便,不用预先设置大小 * 缺点:需要引入netty3的jar包 */ public class Change3 { public static void main(String[] args) { int id = 101; int age = 20; ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(); channelBuffer.writeInt(id); channelBuffer.writeInt(age); byte[] buf = new byte[channelBuffer.writerIndex()]; channelBuffer.readBytes(buf); System.out.println(Arrays.toString(buf)); ChannelBuffer wrappedBuffer = ChannelBuffers.wrappedBuffer(buf); System.out.println("id: "+wrappedBuffer.readInt()); System.out.println("age: "+wrappedBuffer.readInt()); } }
?
?
3.2使用netty3的jar包中的ChannelBuffer类
这段代码测试的是将String类型序列化
package com.chalmers.change; import java.util.Arrays; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; /** * @author Chalmers 2016年2月23日 下午3:44:16 */ public class Change4 { public static void main(String[] args) { String username = "Chalmers"; String password = "20134714"; ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(); //将字符串序列化跟其他不同,需要写写一个字符串的长度,然后再把字符串转成字节数组输入 //写入长度 channelBuffer.writeShort((short) username.length()); //写入字节数组 channelBuffer.writeBytes(username.getBytes()); channelBuffer.writeShort((short) password.length()); channelBuffer.writeBytes(password.getBytes()); //将Buffer转成字节数组 byte[] bytes = channelBuffer.array(); //输出 System.out.println(Arrays.toString(bytes)); //新建一个Buffer对象,并且把字节数组加载进来 ChannelBuffer channelBuffer2 = ChannelBuffers.wrappedBuffer(bytes); //感觉不太好,用int更方便,虽然short比int少占用两个字节 // 在反序列化字符串前,先读取字符串的长度 short len = channelBuffer2.readShort(); //构造字节数组 byte[] bytes2 = new byte[(int) len]; //将数据读取到字节数组汇中 channelBuffer2.readBytes(bytes2); String username2 = new String(bytes2); len = channelBuffer2.readShort(); byte[] bytes3 = new byte[(int) len]; channelBuffer2.readBytes(bytes3); String password2 = new String(bytes3); System.out.println(username2 + "\t" + password2); } }
?
原文:http://moonmonster.iteye.com/blog/2278664