PerformTestUserInfo

  1. public class PerformTestUserInfo {
  2. public static void main(String[] args) throws IOException {
  3. UserInfo info = new UserInfo();
  4. info.buildUserID(100).buildUserName("Welcome to Netty");
  5. int loop = 1000000;
  6. ByteArrayOutputStream bos = null;
  7. ObjectOutputStream os = null;
  8. long startTime = System.currentTimeMillis();
  9. for (int i = 0; i < loop; i++) {
  10. bos = new ByteArrayOutputStream();
  11. os = new ObjectOutputStream(bos);
  12. os.writeObject(info);
  13. os.flush();
  14. os.close();
  15. byte[] b = bos.toByteArray();
  16. bos.close();
  17. }
  18. long endTime = System.currentTimeMillis();
  19. System.out.println("The jdk serializable cost time is : "
  20. + (endTime - startTime) + " ms");
  21. System.out.println("-------------------------------------");
  22. ByteBuffer buffer = ByteBuffer.allocate(1024);
  23. startTime = System.currentTimeMillis();
  24. for (int i = 0; i < loop; i++) {
  25. byte[] b = info.codeC(buffer);
  26. }
  27. endTime = System.currentTimeMillis();
  28. System.out.println("The byte array serializable cost time is : "
  29. + (endTime - startTime) + " ms");
  30. }
  31. }

TestUserInfo

  1. public class TestUserInfo {
  2. /**
  3. * @param args
  4. * @throws IOException
  5. */
  6. public static void main(String[] args) throws IOException {
  7. UserInfo info = new UserInfo();
  8. info.buildUserID(100).buildUserName("Welcome to Netty");
  9. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  10. ObjectOutputStream os = new ObjectOutputStream(bos);
  11. os.writeObject(info);
  12. os.flush();
  13. os.close();
  14. byte[] b = bos.toByteArray();
  15. System.out.println("The jdk serializable length is : " + b.length);
  16. bos.close();
  17. System.out.println("-------------------------------------");
  18. System.out.println("The byte array serializable length is : "
  19. + info.codeC().length);
  20. }
  21. }

UserInfo

  1. public class UserInfo implements Serializable {
  2. /**
  3. * 默认的序列号
  4. */
  5. private static final long serialVersionUID = 1L;
  6. private String userName;
  7. private int userID;
  8. public UserInfo buildUserName(String userName) {
  9. this.userName = userName;
  10. return this;
  11. }
  12. public UserInfo buildUserID(int userID) {
  13. this.userID = userID;
  14. return this;
  15. }
  16. public final String getUserName() {
  17. return userName;
  18. }
  19. public final void setUserName(String userName) {
  20. this.userName = userName;
  21. }
  22. public final int getUserID() {
  23. return userID;
  24. }
  25. public final void setUserID(int userID) {
  26. this.userID = userID;
  27. }
  28. //自行序列化
  29. public byte[] codeC() {
  30. ByteBuffer buffer = ByteBuffer.allocate(1024);
  31. byte[] value = this.userName.getBytes();//userName转换为字节数组value
  32. buffer.putInt(value.length);//写入字节数组value的长度
  33. buffer.put(value);//写入字节数组value的值
  34. buffer.putInt(this.userID);//写入userID的值
  35. buffer.flip();//准备读取buffer中的数据
  36. value = null;
  37. byte[] result = new byte[buffer.remaining()];
  38. buffer.get(result);//buffer中的数据写入字节数组并作为结果返回
  39. return result;
  40. }
  41. //自行序列化方法2
  42. public byte[] codeC(ByteBuffer buffer) {
  43. buffer.clear();
  44. byte[] value = this.userName.getBytes();
  45. buffer.putInt(value.length);
  46. buffer.put(value);
  47. buffer.putInt(this.userID);
  48. buffer.flip();
  49. value = null;
  50. byte[] result = new byte[buffer.remaining()];
  51. buffer.get(result);
  52. return result;
  53. }
  54. }