[120] | 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, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.io.serializer; |
---|
| 20 | |
---|
| 21 | import java.io.IOException; |
---|
| 22 | import java.io.InputStream; |
---|
| 23 | import java.io.ObjectInputStream; |
---|
| 24 | import java.io.ObjectOutputStream; |
---|
| 25 | import java.io.OutputStream; |
---|
| 26 | import java.io.Serializable; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * <p> |
---|
| 30 | * An experimental {@link Serialization} for Java {@link Serializable} classes. |
---|
| 31 | * </p> |
---|
| 32 | * @see JavaSerializationComparator |
---|
| 33 | */ |
---|
| 34 | public class JavaSerialization implements Serialization<Serializable> { |
---|
| 35 | |
---|
| 36 | static class JavaSerializationDeserializer<T extends Serializable> |
---|
| 37 | implements Deserializer<T> { |
---|
| 38 | |
---|
| 39 | private ObjectInputStream ois; |
---|
| 40 | |
---|
| 41 | public void open(InputStream in) throws IOException { |
---|
| 42 | ois = new ObjectInputStream(in) { |
---|
| 43 | @Override protected void readStreamHeader() { |
---|
| 44 | // no header |
---|
| 45 | } |
---|
| 46 | }; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | @SuppressWarnings("unchecked") |
---|
| 50 | public T deserialize(T object) throws IOException { |
---|
| 51 | try { |
---|
| 52 | // ignore passed-in object |
---|
| 53 | return (T) ois.readObject(); |
---|
| 54 | } catch (ClassNotFoundException e) { |
---|
| 55 | throw new IOException(e.toString()); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | public void close() throws IOException { |
---|
| 60 | ois.close(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | static class JavaSerializationSerializer |
---|
| 66 | implements Serializer<Serializable> { |
---|
| 67 | |
---|
| 68 | private ObjectOutputStream oos; |
---|
| 69 | |
---|
| 70 | public void open(OutputStream out) throws IOException { |
---|
| 71 | oos = new ObjectOutputStream(out) { |
---|
| 72 | @Override protected void writeStreamHeader() { |
---|
| 73 | // no header |
---|
| 74 | } |
---|
| 75 | }; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | public void serialize(Serializable object) throws IOException { |
---|
| 79 | oos.reset(); // clear (class) back-references |
---|
| 80 | oos.writeObject(object); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | public void close() throws IOException { |
---|
| 84 | oos.close(); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public boolean accept(Class<?> c) { |
---|
| 90 | return Serializable.class.isAssignableFrom(c); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | public Deserializer<Serializable> getDeserializer(Class<Serializable> c) { |
---|
| 94 | return new JavaSerializationDeserializer<Serializable>(); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | public Serializer<Serializable> getSerializer(Class<Serializable> c) { |
---|
| 98 | return new JavaSerializationSerializer(); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | } |
---|