View Javadoc

1   /**********************************************************************
2    * ObjectStreamAdapter.java
3    * created on 13.08.2004 by netseeker
4    * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/adapter/ObjectStreamAdapter.java,v $
5    * $Date: 2006/12/03 13:54:41 $
6    * $Revision: 1.18 $
7    *
8    * ====================================================================
9    *
10   *  Copyright 2005-2006 netseeker aka Michael Manske
11   *
12   *  Licensed under the Apache License, Version 2.0 (the "License");
13   *  you may not use this file except in compliance with the License.
14   *  You may obtain a copy of the License at
15   *
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the License for the specific language governing permissions and
22   *  limitations under the License.
23   * ====================================================================
24   *
25   * This file is part of the ejoe framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  
31  package de.netseeker.ejoe.adapter;
32  
33  import java.io.InputStream;
34  import java.io.ObjectInputStream;
35  import java.io.ObjectOutputStream;
36  import java.io.OutputStream;
37  import java.io.Serializable;
38  
39  /***
40   * Simple (de)serialize adapter using the standard ObjectInputStream and ObjectOutputStream classes provided by SUN.
41   * 
42   * @author netseeker aka Michael Manske
43   * @since 0.3.0
44   */
45  public class ObjectStreamAdapter extends BaseAdapter
46  {
47      private static final long serialVersionUID = 1L;
48  
49      /*
50       * (non-Javadoc)
51       * 
52       * @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
53       */
54      public Object read( InputStream in ) throws Exception
55      {
56          Object obj = null;
57          ObjectInputStream oin = new ObjectInputStream( in );
58  
59          try
60          {
61              obj = oin.readObject();
62          }
63          finally
64          {
65              if ( oin != null )
66              {
67                  oin.close();
68              }
69          }
70  
71          return obj;
72      }
73  
74      /*
75       * (non-Javadoc)
76       * 
77       * @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
78       */
79      public void write( Object obj, OutputStream out ) throws Exception
80      {
81          Serializable target = (Serializable)obj;
82          ObjectOutputStream oout = new ObjectOutputStream( out );
83          oout.writeObject( target );
84          oout.close();
85      }
86  }