001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core.avltree;
021    
022    
023    import java.io.ByteArrayInputStream;
024    import java.io.ByteArrayOutputStream;
025    import java.io.IOException;
026    import java.io.ObjectInputStream;
027    import java.io.ObjectOutputStream;
028    
029    import org.apache.directory.server.i18n.I18n;
030    
031    
032    /**
033     * A Marshaller which uses default Java Serialization.
034     *
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev$
037     */
038    public class DefaultMarshaller implements Marshaller<Object>
039    {
040        public static final DefaultMarshaller INSTANCE = new DefaultMarshaller();
041    
042    
043        public byte[] serialize( Object object ) throws IOException
044        {
045            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
046            ObjectOutputStream out = new ObjectOutputStream( byteStream );
047            byte[] data;
048    
049            out.writeObject( object );
050            out.flush();
051            data = byteStream.toByteArray();
052            out.close();
053    
054            return data;
055        }
056    
057    
058        public Object deserialize( byte[] bytes ) throws IOException
059        {
060            Object object;
061            ByteArrayInputStream byteStream = new ByteArrayInputStream( bytes );
062            ObjectInputStream in = new ObjectInputStream( byteStream );
063    
064            try
065            {
066                object = in.readObject();
067            }
068            catch ( ClassNotFoundException e )
069            {
070                IOException ioe = new IOException( I18n.err( I18n.ERR_445 ) );
071                ioe.initCause( e );
072                throw ioe;
073            }
074    
075            return object;
076        }
077    }