001/******************************************************************************* 002 * This software is provided as a supplement to the authors' textbooks on digital 003 * image processing published by Springer-Verlag in various languages and editions. 004 * Permission to use and distribute this software is granted under the BSD 2-Clause 005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit https://imagingbook.com for additional details. 008 ******************************************************************************/ 009package imagingbook.common.util; 010 011import java.io.BufferedInputStream; 012import java.io.BufferedOutputStream; 013import java.io.File; 014import java.io.FileInputStream; 015import java.io.FileOutputStream; 016import java.io.IOException; 017import java.io.InputStream; 018import java.io.ObjectInput; 019import java.io.ObjectInputStream; 020import java.io.ObjectOutput; 021import java.io.ObjectOutputStream; 022import java.io.OutputStream; 023 024/** 025 * Helper class providing static methods for writing/reading serialized data to/from files. It is recommended to 026 * serialize only data structures composed of standard Java types. Otherwise, if self-defined classes are reloaded, 027 * classes of previously serialized objects may not match any more, causing a {@link ClassNotFoundException} to be 028 * thrown. 029 * 030 * @author WB 031 * @version 2022/07/28 032 */ 033public abstract class SerializationHelper { 034 035 // This class is not supposed to be instantiated. 036 private SerializationHelper() {} 037 038 /** 039 * Writes a serialized representation of an arbitrary Java object to a file. 040 * 041 * @param obj the object to be serialized. 042 * @param file the file to write to. 043 * @return the full path of the written file. 044 */ 045 public static String writeObject(Object obj, File file) { 046 String path = file.getAbsolutePath(); 047 try (FileOutputStream strm = new FileOutputStream(file); 048 OutputStream buffer = new BufferedOutputStream(strm); 049 ObjectOutput output = new ObjectOutputStream(buffer);) 050 { 051 output.writeObject(obj); 052 } catch (IOException e) { 053 System.err.println(e.toString()); 054 return null; 055 } 056 return path; 057 } 058 059 /** 060 * Reads an object from a serialization file. The return value must be cast to the appropriate type, which must be 061 * known. 062 * 063 * @param file the file to read. 064 * @return the object reconstructed from the file representation or null if unsuccessful. 065 */ 066 public static Object readObject(File file) { 067 Object obj = null; 068 try (InputStream strm = new FileInputStream(file); 069 InputStream buffer = new BufferedInputStream(strm); 070 ObjectInput input = new ObjectInputStream(buffer);) 071 { 072 obj = input.readObject(); 073 } catch (ClassNotFoundException | IOException e) { 074 System.err.println(e.toString()); 075 return null; 076 } 077 return obj; 078 } 079 080}