View Javadoc

1   package org.apache.directmemory.serialization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.ObjectOutputStream;
25  
26  import org.apache.directmemory.measures.Ram;
27  import org.apache.directmemory.misc.DummyPojo;
28  
29  public final class DummyPojoSerializer
30      implements Serializer
31  {
32  
33      final DummyPojo pojo = new DummyPojo( "test", Ram.Kb( 2 ) );
34  
35      final byte[] data;
36  
37      public DummyPojoSerializer()
38      {
39          ByteArrayOutputStream baos = new ByteArrayOutputStream();
40          try
41          {
42              ObjectOutputStream oos = new ObjectOutputStream( baos );
43              oos.writeObject( pojo );
44              oos.flush();
45              oos.close();
46          }
47          catch ( Exception e )
48          {
49              // should not happen
50          }
51          data = baos.toByteArray();
52      }
53  
54      @SuppressWarnings( "unchecked" ) // it is just a dummy class for tests
55      @Override
56      public <T> T deserialize( byte[] source, Class<T> clazz )
57          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
58      {
59          // testing puts only
60          return (T) pojo;
61      }
62  
63      @Override
64      public <T> byte[] serialize( T obj )
65          throws IOException
66      {
67          return data;
68      }
69  
70  }