对象转化为byte数组,byte数组转化为对象
/** * 对象转Byte数组 * * @param obj * @return * @throws Exception */ public static byte[] objectToBytes(Object obj) throws Exception { logger.debug("objectToString called "); ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream sOut = new ObjectOutputStream(out); sOut.writeObject(obj); sOut.flush(); byte[] bytes = out.toByteArray(); logger.debug(bytes.toString()); return bytes; } ** * 字节数组转对象 * * @param content * @return * @throws Exception */ public static Object bytesToObject(byte[] bytes) throws Exception { logger.debug("bytesToObject called "); //byte转object ByteArrayInputStream in = new ByteArrayInputStream(bytes); ObjectInputStream sIn = new ObjectInputStream(in); return sIn.readObject(); } |