com.janeluo.jfinalplus.kit.SerializableKit Maven / Gradle / Ivy
/**
* Copyright (c) 2011-2013, kidzhou 周磊 ([email protected])
*
* 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.janeluo.jfinalplus.kit;
import java.io.*;
public class SerializableKit {
/**
* 将对象序列化为byte[]
*
* @param obj
* @return
*/
public static byte[] toByteArray(Serializable obj) {
byte[] bytes = null;
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
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;
}
/**
* 将byte[]反序列化为Object
*
* @param bytes
* @return
*/
public static Serializable toObject(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;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy