public static < T extends Serializable > T deserializeFromStream ( InputStream in , Class < T > type ) throws ClassNotFoundException , IOException { ObjectInputStream objIn ; T obj ; if ( in instanceof ObjectInputStream ) objIn = ( ObjectInputStream ) in ; else objIn = new ObjectInputStream ( in ) ; obj = type . cast ( objIn . readObject ( ) ) ; // to close or not to close... That is the question... // we close... It doesn't make too much sense to have a stream with more afterwards...There would be two // stream headers... objIn . close ( ) ; return obj ; }
Read one serialized object from a input stream .