View Javadoc

1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   */
38  package com.gargoylesoftware.base.io;
39  
40  import java.io.ObjectOutputStream;
41  import java.io.OutputStream;
42  import java.io.IOException;
43  import java.util.List;
44  import java.util.ArrayList;
45  
46  /***
47   * A specialized subclass of ObjectOutputStream that is used to serialize
48   * objects.  This stream will remove duplicate objects from the stream in
49   * order to shrink the resulting byte stream.<p>
50   *
51   * Only objects of the following types will be condensed: Character, Double,
52   * Integer, Long, Short and String.  The biggest benefit will come from
53   * duplicate Strings.
54   *
55   * @version $Revision: 1.4 $
56   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
57   */
58  public class CondensedObjectOutputStream extends ObjectOutputStream {
59  
60      /***
61       * The list of classes that we can try condensing. Only classes that
62       * are immutable should be in this list.
63       */
64      private final String classNames_[] = {
65          "java.lang.Character",
66          "java.lang.Double",
67          "java.lang.Integer",
68          "java.lang.Long",
69          "java.lang.Short",
70          "java.lang.String",
71      };
72  
73      private final List classes_;
74      private final List objects_ = new ArrayList();
75  
76      /***
77       * Create the stream
78       * @param stream The output stream that we are wrapping
79       * @throws IOException If the superclass throws an IOException in it's
80       * constructor.
81       */
82      public CondensedObjectOutputStream( final OutputStream stream ) throws IOException {
83          super(stream);
84          enableReplaceObject(true);
85  
86          classes_ = new ArrayList( classNames_.length );
87          int i;
88          for( i=0; i<classNames_.length; i++ ) {
89              try {
90                  classes_.add( Class.forName(classNames_[i]) );
91              }
92              catch( final ClassNotFoundException e ) {
93                  // Theoretically impossible if the classNames list is set up correctly.
94                  throw new NoClassDefFoundError( classNames_[i] );
95              }
96          }
97  
98      }
99  
100     /***
101      * Overrides the superclass to perform substitutions of duplicate
102      * immutable objects.
103      *
104      * @param object The object to be serialized
105      * @return Either the object that was passed in or an identical object
106      * that had previously been passed in.
107      */
108     protected Object replaceObject( final Object object ) {
109 
110         Object rc = object;
111 
112         if( object != null && classes_.contains( object.getClass() ) ) {
113 
114             final int index = objects_.indexOf(object);
115             if( index == -1 ) {
116                 objects_.add( object );
117             }
118             else {
119                 rc = objects_.get(index);
120             }
121         }
122 
123         return rc;
124     }
125 }
126