1 22 23 24 package com.mchange.v2.ser; 25 26 import java.io.*; 27 import com.mchange.v1.io.*; 28 import com.mchange.v2.log.*; 29 30 public final class SerializableUtils 31 { 32 final static MLogger logger = MLog.getLogger( SerializableUtils.class ); 33 34 private SerializableUtils() 35 {} 36 37 38 public static byte[] toByteArray(Object obj) throws NotSerializableException 39 { return serializeToByteArray( obj ); } 40 41 public static byte[] toByteArray(Object obj, Indirector indirector, IndirectPolicy policy) throws NotSerializableException 42 { 43 try 44 { 45 if (policy == IndirectPolicy.DEFINITELY_INDIRECT) 46 { 47 if (indirector == null) 48 throw new IllegalArgumentException ("null indirector is not consistent with " + policy); 49 50 IndirectlySerialized indirect = indirector.indirectForm( obj ); 51 return toByteArray( indirect ); 52 } 53 else if ( policy == IndirectPolicy.INDIRECT_ON_EXCEPTION ) 54 { 55 if (indirector == null) 56 throw new IllegalArgumentException ("null indirector is not consistent with " + policy); 57 58 try { return toByteArray( obj ); } 59 catch ( NotSerializableException e ) 60 { return toByteArray( obj, indirector, IndirectPolicy.DEFINITELY_INDIRECT ); } 61 } 62 else if (policy == IndirectPolicy.DEFINITELY_DIRECT) 63 return toByteArray( obj ); 64 else 65 throw new InternalError ("unknown indirecting policy: " + policy); 66 } 67 catch ( NotSerializableException e ) 68 { throw e; } 69 catch ( Exception e ) 70 { 71 if ( logger.isLoggable( MLevel.WARNING ) ) 73 logger.log( MLevel.WARNING, "An Exception occurred while serializing an Object to a byte[] with an Indirector.", e ); 74 throw new NotSerializableException( e.toString() ); 75 } 76 } 77 78 81 public static byte[] serializeToByteArray(Object obj) throws NotSerializableException 82 { 83 try 84 { 85 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 86 ObjectOutputStream out = new ObjectOutputStream(baos); 87 out.writeObject(obj); 88 return baos.toByteArray(); 89 } 90 catch (NotSerializableException e) 91 { 92 e.fillInStackTrace(); 95 throw e; 96 } 97 catch (IOException e) 98 { 99 if ( logger.isLoggable( MLevel.SEVERE ) ) 101 logger.log( MLevel.SEVERE, "An IOException occurred while writing into a ByteArrayOutputStream?!?", e ); 102 throw new Error ("IOException writing to a byte array!"); 103 } 104 } 105 106 109 public static Object fromByteArray(byte[] bytes) throws IOException, ClassNotFoundException 110 { 111 Object out = deserializeFromByteArray( bytes ); 112 if (out instanceof IndirectlySerialized) 113 return ((IndirectlySerialized) out).getObject(); 114 else 115 return out; 116 } 117 118 public static Object fromByteArray(byte[] bytes, boolean ignore_indirects) throws IOException, ClassNotFoundException 119 { 120 if (ignore_indirects) 121 return deserializeFromByteArray( bytes ); 122 else 123 return fromByteArray( bytes ); 124 } 125 126 129 public static Object deserializeFromByteArray(byte[] bytes) throws IOException, ClassNotFoundException 130 { 131 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)); 132 return in.readObject(); 133 } 134 135 136 public static Object testSerializeDeserialize( Object o ) throws IOException, ClassNotFoundException 137 { return deepCopy( o ); } 138 139 public static Object deepCopy( Object o ) throws IOException, ClassNotFoundException 140 { 141 byte[] bytes = serializeToByteArray( o ); 142 return deserializeFromByteArray( bytes ); 143 } 144 145 public final static Object unmarshallObjectFromFile(File file) 146 throws IOException, ClassNotFoundException 147 { 148 ObjectInputStream in = null; 149 try 150 { 151 in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); 152 return in.readObject(); 153 } 154 finally 155 {InputStreamUtils.attemptClose(in);} 156 } 157 158 public final static void marshallObjectToFile(Object o, File file) 159 throws IOException 160 { 161 ObjectOutputStream out = null; 162 try 163 { 164 out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file))); 165 out.writeObject(o); 166 } 167 finally 168 {OutputStreamUtils.attemptClose(out);} 169 } 170 } 171 172 | Popular Tags |