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.mitosis.operation;
21  
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  import java.io.ObjectStreamConstants;
29  
30  import org.apache.directory.server.schema.registries.Registries;
31  
32  
33  /**
34   * Encodes {@link Operation}s to <tt>byte[]</tt> and vice versa so an
35   * {@link Operation} can be transferred via TCP/IP communication.
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class OperationCodec
40  {
41      public OperationCodec()
42      {
43      }
44  
45  
46      /**
47       * Transforms the specified {@link Operation} into a byte array.
48       */
49      public byte[] encode( Operation op )
50      {
51          ByteArrayOutputStream bout = new ByteArrayOutputStream();
52          try
53          {
54              ObjectOutputStream out = new ObjectOutputStream( bout );
55              out.useProtocolVersion( ObjectStreamConstants.PROTOCOL_VERSION_2 );
56              Operation.serialize( op, out );
57              out.flush();
58              out.close();
59          }
60          catch ( IOException e )
61          {
62              throw ( InternalError ) new InternalError().initCause( e );
63          }
64          catch ( ClassNotFoundException cnfe )
65          {
66              throw ( InternalError ) new InternalError().initCause( cnfe );
67          }
68          
69          return bout.toByteArray();
70      }
71  
72  
73      /**
74       * Transforms the specified byte array into an {@link Operation}.
75       */
76      public Operation decode( Registries registries, byte[] data )
77      {
78          ObjectInputStream in;
79          
80          try
81          {
82              in = new ObjectInputStream( new ByteArrayInputStream( data ) );
83              return Operation.deserialize( registries, in );
84          }
85          catch ( IOException e )
86          {
87              throw ( InternalError ) new InternalError().initCause( e );
88          }
89          catch ( ClassNotFoundException e )
90          {
91              throw ( InternalError ) new InternalError().initCause( e );
92          }
93      }
94  }