/** * Copyright (c) 2014-2015, biezhi 王爵 (biezhi.me@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.blade.kit; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * 序列化类 * @author biezhi * @since 1.0 */ public class SerializeKit { /** * 序列化 * @param object 要被序列化的对象 * @return 序列化后的字节码 */ public static byte[] serialize(Object object) { byte[] bytes = null; ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); oos.flush(); } catch (Exception e) { throw new RuntimeException(e); } finally { if (oos != null) { try { oos.close(); bytes = baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } } return bytes; } /** * 反序列化 * @param bytes 反序列化的字节码 * @return 反序列化后的对象 */ public static Serializable unserialize(byte[] bytes) { Serializable obj = null; ObjectInputStream ois = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bais); obj = (Serializable) ois.readObject(); } catch (Exception e) { throw new RuntimeException(e); } finally { if (ois != null) { try { ois.close(); } catch (IOException e) { throw new RuntimeException(e); } } } return obj; } }