View Javadoc

1   /*
2    * Copyright 2006-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.util.cache;
17  
18  import java.io.IOException;
19  import java.io.ObjectInputStream;
20  import java.io.ObjectOutputStream;
21  import java.io.Serializable;
22  
23  
24  /**
25   * Container class to simplify getting both a deepCopied object and its size returned from a single call to deepCopy.
26   */
27  public class CopiedObject<T extends Serializable> {
28      //private Serializable content;
29      private byte[] content;
30      private int size;
31      private int oldSize;
32  
33      public CopiedObject() {
34          oldSize = -1;
35      }
36  
37      public CopiedObject( T cacheableObject ) {
38          oldSize = -1;
39          setContent( cacheableObject );
40      }
41  
42      /**
43       * @return current value of bytes.
44       */
45      public int getSize() {
46          return size;
47      }
48  
49      /**
50       * @return current value of cacheableObject.
51       */
52      public T getContent() {
53          T copy = null;
54          if (content != null) {
55              ObjectInputStream ois = null;
56              try {
57                  FastByteArrayInputStream deserializer = new FastByteArrayInputStream(content,size);
58                  ois = new ObjectInputStream(deserializer);
59                  copy = (T) ois.readObject();
60              }
61              catch (IOException e) {
62                  throw new CacheException("unable to complete getContent()", e);
63              }
64              catch (ClassNotFoundException e) {
65                  throw new CacheException("unable to complete getContent()", e);
66              }
67              finally {
68                  try {
69                      if (ois != null) {
70                          ois.close();
71                      }
72                  }
73                  catch (IOException e) {
74                      // ignoring this IOException, since the streams are going to be abandoned now anyway
75                  }
76              }
77          }
78          return copy;
79      }
80  
81      /**
82       * Sets the cacheableObject attribute value.
83       * 
84       * @param cacheableObject The cacheableObject to set.
85       */
86      public void setContent(T cacheableObject) {
87          int copySize = 0;
88          if (cacheableObject != null) {
89              ObjectOutputStream oos = null;
90              try {
91                  FastByteArrayOutputStream serializer = new FastByteArrayOutputStream();
92                  oos = new ObjectOutputStream(serializer);
93                  oos.writeObject(cacheableObject);
94  
95                  if ( content != null ) {
96                      oldSize = size;
97                  }
98                  size = serializer.getSize();
99                  content = serializer.getByteArray();
100             } catch (IOException e) {
101                 throw new CacheException("unable to complete deepCopy from src '" + cacheableObject.toString() + "'", e);
102             }
103             finally {
104                 try {
105                     if (oos != null) {
106                         oos.close();
107                     }
108                 } catch (IOException e) {
109                     // ignoring this IOException, since the streams are going to be abandoned now anyway
110                 }
111             }
112         }
113     }
114 
115 
116     /**
117      * @return current value of oldSize.
118      */
119     public int getOldSize() {
120         return oldSize;
121     }
122 }