Java tutorial
/** * Copyright (C) 2007 Asterios Raptis * * 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 net.sourceforge.jaulp.io; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; import net.sourceforge.jaulp.file.FileConstants; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.beanutils.BeanUtils; /** * Helper-class for make exact copys from serialized objects. * * @version 1.0 * @author Asterios Raptis */ public final class SerializedObjectUtils { /** The LOGGER. */ protected static final Logger LOGGER = Logger.getLogger(SerializedObjectUtils.class.getName()); /** * Private Constructor. */ private SerializedObjectUtils() { } /** * The Method toByteArray() serialize an Object to byte array. * * @param <T> * the generic type of the given object * @param object * The Object to convert into a byte array. * @return The byte array from the Object. * @throws IOException * Signals that an I/O exception has occurred. */ public static <T> byte[] toByteArray(final T object) throws IOException { ByteArrayOutputStream byteArrayOutputStream = null; ObjectOutputStream objectOutputStream = null; try { byteArrayOutputStream = new ByteArrayOutputStream(FileConstants.KILOBYTE); byteArrayOutputStream.reset(); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); return byteArrayOutputStream.toByteArray(); } finally { StreamUtils.closeOutputStream(byteArrayOutputStream); StreamUtils.closeOutputStream(objectOutputStream); } } /** * The Method toObject() converts the given byte array into an Object. * * @param byteArray * The byte array to convert into an Object. * @return The Object the was converted from the byte array. * @throws IOException * Signals that an I/O exception has occurred. * @throws ClassNotFoundException * is thrown when a class is not found in the classloader or no definition for the * class with the specified name could be found. */ public static Object toObject(final byte[] byteArray) throws IOException, ClassNotFoundException { Object object = null; ByteArrayInputStream byteArrayInputStream = null; ObjectInputStream objectInputStream = null; try { byteArrayInputStream = new ByteArrayInputStream(byteArray); objectInputStream = new ObjectInputStream(byteArrayInputStream); object = objectInputStream.readObject(); objectInputStream.close(); } finally { StreamUtils.closeInputStream(byteArrayInputStream); StreamUtils.closeInputStream(objectInputStream); } return object; } /** * Copys the given Object and returns the copy from the object or null if the object can't be * serialized. * * @param <T> * the generic type of the given object * @param orig * The object to copy. * @return Returns a copy from the original object. * @throws IOException * Signals that an I/O exception has occurred. * @throws ClassNotFoundException * is thrown when a class is not found in the classloader or no definition for the * class with the specified name could be found. */ @SuppressWarnings("unchecked") public static <T extends Serializable> T copySerializedObject(final T orig) throws IOException, ClassNotFoundException { T object = null; ByteArrayOutputStream byteArrayOutputStream = null; ObjectOutputStream objectOutputStream = null; try { byteArrayOutputStream = new ByteArrayOutputStream(); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(orig); objectOutputStream.flush(); objectOutputStream.close(); final ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); final ObjectInputStream ois = new ObjectInputStream(bis); object = (T) ois.readObject(); } finally { StreamUtils.closeOutputStream(byteArrayOutputStream); StreamUtils.closeOutputStream(objectOutputStream); } return object; } /** * Reads the object from the given file. * * @param file * In that file is the object saved. * @return The object in the file or null. * @throws IOException * Signals that an I/O exception has occurred. * @throws ClassNotFoundException * is thrown when a class is not found in the classloader or no definition for the * class with the specified name could be found. */ public static Object readSerializedObjectFromFile(final File file) throws IOException, ClassNotFoundException { Object object = null; FileInputStream fis = null; ObjectInputStream in = null; try { fis = new FileInputStream(file); in = new ObjectInputStream(fis); object = in.readObject(); in.close(); } finally { StreamUtils.closeInputStream(in); StreamUtils.closeInputStream(fis); } return object; } /** * Writes the given object to the given File. * * @param obj * The object to write to the File. * @param file * In this file will be the object saved. * @return true if the object was written to the file otherwise false. * @throws IOException * Signals that an I/O exception has occurred. */ public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException { boolean written = true; FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(obj); oos.flush(); oos.close(); } finally { StreamUtils.closeOutputStream(oos); StreamUtils.closeOutputStream(fos); } return written; } /** * Gets the changed data. * * @param sourceOjbect * the source ojbect * @param objectToCompare * the object to compare * @return the changed data * @throws IllegalAccessException * the illegal access exception * @throws InvocationTargetException * the invocation target exception * @throws NoSuchMethodException * the no such method exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static List<SerializedChangedAttributeResult> getChangedData(Object sourceOjbect, Object objectToCompare) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Map beanDescription = BeanUtils.describe(sourceOjbect); beanDescription.remove("class"); Map clonedBeanDescription = BeanUtils.describe(objectToCompare); clonedBeanDescription.remove("class"); List<SerializedChangedAttributeResult> changedData = new ArrayList<>(); for (Object key : beanDescription.keySet()) { BeanComparator comparator = new BeanComparator(key.toString()); if (comparator.compare(sourceOjbect, objectToCompare) != 0) { Object sourceAttribute = beanDescription.get(key); Object changedAttribute = clonedBeanDescription.get(key); changedData.add(new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute)); } } return changedData; } /** * Compares the given two objects and gets the changed data. * * @param sourceOjbect * the source ojbect * @param objectToCompare * the object to compare * @return the changed data * @throws IllegalAccessException * the illegal access exception * @throws InvocationTargetException * the invocation target exception * @throws NoSuchMethodException * the no such method exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map<Object, SerializedChangedAttributeResult> getChangedDataMap(Object sourceOjbect, Object objectToCompare) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Map beanDescription = BeanUtils.describe(sourceOjbect); beanDescription.remove("class"); Map clonedBeanDescription = BeanUtils.describe(objectToCompare); clonedBeanDescription.remove("class"); Map<Object, SerializedChangedAttributeResult> changedData = new HashMap<>(); for (Object key : beanDescription.keySet()) { BeanComparator comparator = new BeanComparator(key.toString()); if (comparator.compare(sourceOjbect, objectToCompare) != 0) { Object sourceAttribute = beanDescription.get(key); Object changedAttribute = clonedBeanDescription.get(key); changedData.put(key, new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute)); } } return changedData; } }