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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.io.serializer;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.ObjectInputStream;
024import java.io.ObjectOutputStream;
025import java.io.OutputStream;
026import java.io.Serializable;
027import java.util.Map;
028
029import org.apache.hadoop.classification.InterfaceAudience;
030import org.apache.hadoop.classification.InterfaceStability;
031import org.apache.hadoop.io.RawComparator;
032
033/**
034 * <p>
035 * An experimental {@link Serialization} for Java {@link Serializable} classes.
036 * </p>
037 * @see JavaSerializationComparator
038 */
039@InterfaceAudience.Public
040@InterfaceStability.Unstable
041public class JavaSerialization implements Serialization<Serializable> {
042
043  static class JavaSerializationDeserializer<T extends Serializable>
044    implements Deserializer<T> {
045
046    private ObjectInputStream ois;
047
048    public void open(InputStream in) throws IOException {
049      ois = new ObjectInputStream(in) {
050        @Override protected void readStreamHeader() {
051          // no header
052        }
053      };
054    }
055    
056    @SuppressWarnings("unchecked")
057    public T deserialize(T object) throws IOException {
058      try {
059        // ignore passed-in object
060        return (T) ois.readObject();
061      } catch (ClassNotFoundException e) {
062        throw new IOException(e.toString());
063      }
064    }
065
066    public void close() throws IOException {
067      ois.close();
068    }
069
070  }
071
072  static class JavaSerializationSerializer
073    implements Serializer<Serializable> {
074
075    private ObjectOutputStream oos;
076
077    public void open(OutputStream out) throws IOException {
078      oos = new ObjectOutputStream(out) {
079        @Override protected void writeStreamHeader() {
080          // no header
081        }
082      };
083    }
084
085    public void serialize(Serializable object) throws IOException {
086      oos.reset(); // clear (class) back-references
087      oos.writeObject(object);
088    }
089
090    public void close() throws IOException {
091      oos.close();
092    }
093
094  }
095
096  @InterfaceAudience.Private
097  public boolean accept(Class<?> c) {
098    return Serializable.class.isAssignableFrom(c);
099  }
100
101  @InterfaceAudience.Private
102  public Deserializer<Serializable> getDeserializer(Class<Serializable> c) {
103    return new JavaSerializationDeserializer<Serializable>();
104  }
105
106  @InterfaceAudience.Private
107  public Serializer<Serializable> getSerializer(Class<Serializable> c) {
108    return new JavaSerializationSerializer();
109  }
110
111}