Java tutorial
/* * Copyright 2014 sohu.com All right reserved. This software is the * confidential and proprietary information of sohu.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with sohu.com. */ package serialization.hessian; import java.io.ByteArrayInputStream; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.caucho.hessian.io.HessianInput; import com.caucho.hessian.io.HessianOutput; /** * HessianUtils.java??hessian? * * @author apple 2014-4-23 ?2:02:56 */ public class HessianUtils { public static void serialize(Object o, String fileName) throws IOException { if (o == null) { throw new NullPointerException(); } HessianOutput output = new HessianOutput(FileUtils.openOutputStream(FileUtils.getFile(fileName))); output.writeObject(o); output.flush(); output.close(); System.out.println("file size : " + FileUtils.sizeOf(FileUtils.getFile(fileName))); } public static Object deserialize(String fileName) throws IOException { if (fileName == null) { throw new NullPointerException(); } HessianInput hessianInput = new HessianInput(FileUtils.openInputStream(FileUtils.getFile(fileName))); Object readObject = hessianInput.readObject(); hessianInput.close(); return readObject; } public static Object deserialize(byte[] by) throws IOException { if (by == null) { throw new NullPointerException(); } HessianInput hessianInput = new HessianInput(new ByteArrayInputStream(by)); Object readObject = hessianInput.readObject(); hessianInput.close(); return readObject; } }