This project has retired. For details please refer to its Attic page.
DirectWritableSerializer xref
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, 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  package org.apache.giraph.writable.kryo.serializers;
19  
20  import java.io.DataInputStream;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.giraph.utils.ReflectionUtils;
25  import org.apache.giraph.utils.WritableUtils;
26  import org.apache.hadoop.io.Writable;
27  
28  import com.esotericsoftware.kryo.Kryo;
29  import com.esotericsoftware.kryo.Serializer;
30  import com.esotericsoftware.kryo.io.Input;
31  import com.esotericsoftware.kryo.io.Output;
32  
33  /**
34   * A custom Serializer that will call the Writable methods defined by the
35   * object itself to serialize the object, instead of Kryo auto-magically
36   * serializing
37   *
38   * @param <T> Object type, should implement Writable
39   */
40  
41  public class DirectWritableSerializer<T extends Writable>
42      extends Serializer<T> {
43  
44    @Override
45    public void write(Kryo kryo, Output output, T object) {
46      try {
47        object.write(new DataOutputStream(output));
48      } catch (IOException e) {
49        throw new RuntimeException(
50            "DirectWritableSerializer.write calling Writable method of class: " +
51              object.getClass().getName() + " encountered issues", e);
52      }
53    }
54  
55    @Override
56    public T read(Kryo kryo, Input input, Class<T> type) {
57      try {
58        T object = create(kryo, input, type);
59        kryo.reference(object);
60        object.readFields(new DataInputStream(input));
61  
62        return object;
63      } catch (IOException e) {
64        throw new RuntimeException(
65            "DirectWritableSerializer.read calling Writable method of class: " +
66            type.getName() + " encountered issues", e);
67      }
68    }
69  
70    @Override
71    public T copy(Kryo kryo, T original) {
72      return WritableUtils.createCopy(original);
73    }
74  
75    /**
76     * Used by {@link #read(Kryo, Input, Class)} to create the new object.
77     * This can be overridden to customize object creation, eg to call a
78     * constructor with arguments. The default implementation
79     * uses {@link Kryo#newInstance(Class)}.
80     *
81     * @param kryo Kryo object instance
82     * @param input Input
83     * @param type Type of the class to create
84     * @return New instance of wanted type
85     */
86    protected T create(Kryo kryo, Input input, Class<T> type) {
87      return ReflectionUtils.newInstance(type);
88    }
89  }