View Javadoc
1   package org.apache.commons.jcs.utils.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 org.apache.commons.jcs.engine.behavior.IElementSerializer;
23  import org.apache.commons.jcs.io.ObjectInputStreamClassLoaderAware;
24  import org.apache.commons.jcs.utils.zip.CompressionUtil;
25  
26  import java.io.BufferedInputStream;
27  import java.io.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.IOException;
30  import java.io.ObjectInputStream;
31  import java.io.ObjectOutputStream;
32  
33  /**
34   * Performs default serialization and de-serialization. It gzips the value.
35   */
36  public class CompressingSerializer
37      implements IElementSerializer
38  {
39      /**
40       * Serializes an object using default serialization. Compresses the byte array.
41       * <p>
42       * @param obj object
43       * @return byte[]
44       * @throws IOException on i/o problem
45       */
46      @Override
47      public <T> byte[] serialize( T obj )
48          throws IOException
49      {
50          byte[] uncompressed = serializeObject( obj );
51          byte[] compressed = CompressionUtil.compressByteArray( uncompressed );
52          return compressed;
53      }
54  
55      /**
56       * Does the basic serialization.
57       * <p>
58       * @param obj object
59       * @return byte[]
60       * @throws IOException on i/o problem
61       */
62      protected <T> byte[] serializeObject( T obj )
63          throws IOException
64      {
65          ByteArrayOutputStream baos = new ByteArrayOutputStream();
66          ObjectOutputStream oos = new ObjectOutputStream( baos );
67          try
68          {
69              oos.writeObject( obj );
70          }
71          finally
72          {
73              oos.close();
74          }
75          byte[] uncompressed = baos.toByteArray();
76          return uncompressed;
77      }
78  
79      /**
80       * Uses default de-serialization to turn a byte array into an object. Decompresses the value
81       * first. All exceptions are converted into IOExceptions.
82       * <p>
83       * @param data bytes of data
84       * @return Object
85       * @throws IOException on i/o problem
86       * @throws ClassNotFoundException if class is not found during deserialization
87       */
88      @Override
89      public <T> T deSerialize( byte[] data, ClassLoader loader )
90          throws IOException, ClassNotFoundException
91      {
92          if ( data == null )
93          {
94              return null;
95          }
96          byte[] decompressedByteArray = CompressionUtil.decompressByteArray( data );
97          return deserializeObject( decompressedByteArray );
98      }
99  
100     /**
101      * Does the standard deserialization.
102      * <p>
103      * @param decompressedByteArray array of decompressed bytes
104      * @return Object
105      * @throws IOException on i/o error
106      * @throws ClassNotFoundException if class is not found during deserialization
107      */
108     protected <T> T deserializeObject( byte[] decompressedByteArray )
109         throws IOException, ClassNotFoundException
110     {
111         ByteArrayInputStream bais = new ByteArrayInputStream( decompressedByteArray );
112         BufferedInputStream bis = new BufferedInputStream( bais );
113         ObjectInputStream ois = new ObjectInputStreamClassLoaderAware( bis, null );
114 
115         try
116         {
117             @SuppressWarnings("unchecked") // Need to cast from Object
118             T readObject = (T) ois.readObject();
119             return readObject;
120         }
121         finally
122         {
123             ois.close();
124         }
125     }
126 }