该类为cache4j缓存框架中的工具类方法,该方法实现了两个接口

接口1:计算对象在内存中所占字节数

接口2:复制对象,实现深度克隆效果,实现原理为先序列化对象,然后在反序列化对象;返回一个新的对象,达到克隆效果

package net.sf.cache4j.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
/**
*
* @version $Revision: 1.0 $ $Date:$
* @author Yuriy Stepovoy. <ahref="mailto:stepovoy@gmail.com">stepovoy@gmail.com</a>
**/
public class Utils {
/**
* 计算一个对象所占字节数
* @param o对象,该对象必须继承Serializable接口即可序列化
* @return
* @throws IOException
*/
public static int size(final Object o) throws IOException {
if (o == null) {
return 0;
}
ByteArrayOutputStream buf = new ByteArrayOutputStream(4096);
ObjectOutputStream out = new ObjectOutputStream(buf);
out.writeObject(o);
out.flush();
buf.close();
return buf.size();
}
/**
* 赋值对象,返回对象的引用,如果参数o为符合对象,则内部每一个对象必须可序列化
* @param o对象,该对象必须继承Serializable接口即可序列化
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object copy(final Object o) throws IOException,
ClassNotFoundException {
if (o == null) {
return null;
}
ByteArrayOutputStream outbuf = new ByteArrayOutputStream(4096);
ObjectOutput out = new ObjectOutputStream(outbuf);
out.writeObject(o);
out.flush();
outbuf.close();
ByteArrayInputStream inbuf = new ByteArrayInputStream(outbuf.toByteArray());
ObjectInput in = new ObjectInputStream(inbuf);
return in.readObject();
}
}

例如:对象SMatrix

首先在定义类SMatrix时,必须为

public class SMatrix implements Serializable{
//***
}

一定要注意的一个问题是:所计算的对象必须要实现implements Serializable

最新文章

  1. Android的编码规范
  2. ecshop的弊病和需要修改的地方,持续更新
  3. select2 取值 赋值
  4. SqlServer循环读取配置
  5. 混血儿爹妈要混的远,数据库与WEB分离,得混的近
  6. FloatingActionButton 完全解析[Design Support Library(2)]
  7. 登录RMAN 报告ORA-12162:TNS:net service name is incorrectly specified错
  8. shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、功能、select
  9. Linux基础-最基础
  10. Mac进行 usr/bin 目录下修改权限问题,operation not permitted
  11. 一个简单的C语言程序(详解)
  12. Python学习笔记,day5
  13. 实验吧web题:
  14. Error creating bean with name &#39;tomcatEmbeddedServletContainerFactory&#39;
  15. rabbitMQ在linux上安装
  16. Content portal for Pocketables Tasker articles
  17. SpringMvc拦截器运行原理。
  18. Theos简介
  19. The &#39;microsoft.jet.oledb.4.0&#39; provider is not registered on the local machin
  20. 关于命名空间 namespace的总结

热门文章

  1. git在本地创建远程仓库
  2. 【linux】chmod命令详细用法
  3. [转]开源那些事儿(四)-如何使用CodePlex进行项目管理
  4. USB 2.0 Spec 微缩版
  5. IE11登陆交行网银崩溃
  6. android学习笔记32——资源
  7. 通过xib创建View
  8. IOS中UIWebView停止加载
  9. Hadoop学习9--动态增加datanode
  10. lower_bound实现函数