| Home >> All >> com >> flexstor >> common >> [ util Javadoc ] |
Source code: com/flexstor/common/util/Serialization.java
1 /* 2 * Serialization.java 3 * 4 * Copyright $Date: 2003/08/11 02:22:31 $ FLEXSTOR.net Inc. 5 * 6 * This work is licensed for use and distribution under license terms found at 7 * http://www.flexstor.org/license.html 8 * 9 */ 10 11 package com.flexstor.common.util; 12 13 import java.io.ByteArrayInputStream; 14 import java.io.ByteArrayOutputStream; 15 import java.io.FileInputStream; 16 import java.io.FileNotFoundException; 17 import java.io.FileOutputStream; 18 import java.io.IOException; 19 import java.io.InvalidClassException; 20 import java.io.ObjectInputStream; 21 import java.io.ObjectOutputStream; 22 import java.io.StreamCorruptedException; 23 24 25 /** 26 * Serialization provides static methods for serializing and deserializing objects to and from 27 * a file or byte array. 28 */ 29 public abstract class Serialization 30 { 31 // To get the version number from MKS 32 public final static String IDENTIFIER="$Id: Serialization.java,v 1.2 2003/08/11 02:22:31 aleric Exp $"; 33 34 public static byte[] serializeToByteArray( Object obj ) 35 throws IOException 36 { 37 if (obj != null) 38 { 39 ByteArrayOutputStream baOutputStream = null; 40 ObjectOutputStream oOutputStream = null; 41 try 42 { 43 baOutputStream = new ByteArrayOutputStream(); 44 oOutputStream = new ObjectOutputStream(baOutputStream); 45 oOutputStream.writeObject(obj); 46 oOutputStream.flush(); 47 oOutputStream.reset(); 48 byte[] baItem = baOutputStream.toByteArray(); 49 return baItem; 50 } 51 finally 52 { 53 if ( oOutputStream != null ) 54 oOutputStream.close(); 55 if ( baOutputStream != null ) 56 baOutputStream.close(); 57 } 58 } 59 return new byte[0]; 60 } 61 62 public static Object deserializeFromByteArray( byte[] baItem ) 63 throws StreamCorruptedException, InvalidClassException, IOException, ClassNotFoundException 64 { 65 if (baItem != null && baItem.length > 0) 66 { 67 ObjectInputStream oInputStream = null; 68 try 69 { 70 oInputStream = new ObjectInputStream(new ByteArrayInputStream(baItem)); 71 Object obj = (Object) oInputStream.readObject(); 72 return obj; 73 } 74 finally 75 { 76 if ( oInputStream != null ) 77 oInputStream.close(); 78 } 79 } 80 return null; 81 } 82 83 public static void serializeToFile( Object obj, String sFileName ) 84 throws IOException 85 { 86 ObjectOutputStream oOutputStream = null; 87 try 88 { 89 oOutputStream = new ObjectOutputStream(new FileOutputStream(sFileName)); 90 oOutputStream.writeObject(obj); 91 oOutputStream.flush(); 92 oOutputStream.reset(); 93 } 94 finally 95 { 96 if ( oOutputStream != null ) 97 oOutputStream.close(); 98 } 99 } 100 101 public static Object deserializeFromFile( String sFileName ) 102 throws FileNotFoundException, StreamCorruptedException, InvalidClassException, IOException, ClassNotFoundException 103 { 104 Object obj = null; 105 ObjectInputStream oInputStream = null; 106 try 107 { 108 oInputStream = new ObjectInputStream(new FileInputStream(sFileName)); 109 obj = (Object) (oInputStream.readObject()); 110 return obj; 111 } 112 finally 113 { 114 if ( oInputStream != null ) 115 oInputStream.close(); 116 } 117 } 118 }