View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.avltree;
21  
22  
23  import java.io.*;
24  
25  
26  /**
27   * A marshaller which uses default Java Serialization.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   * @version $Rev$
31   */
32  public class DefaultMarshaller implements Marshaller
33  {
34      public static final DefaultMarshaller INSTANCE = new DefaultMarshaller();
35  
36  
37      public byte[] serialize( Object object ) throws IOException
38      {
39          ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
40          ObjectOutputStream out = new ObjectOutputStream( byteStream );
41          byte[] data;
42  
43          out.writeObject( object );
44          out.flush();
45          data = byteStream.toByteArray();
46          out.close();
47  
48          return data;
49      }
50  
51  
52      public Object deserialize( byte[] bytes ) throws IOException
53      {
54          Object object;
55          ByteArrayInputStream byteStream = new ByteArrayInputStream( bytes );
56          ObjectInputStream in = new ObjectInputStream( byteStream );
57  
58          try
59          {
60              object = in.readObject();
61          }
62          catch ( ClassNotFoundException e )
63          {
64              IOException ioe = new IOException( "Could not find class" );
65              ioe.initCause( e );
66              throw ioe;
67          }
68  
69          return object;
70      }
71  }