Coverage Report - org.kuali.rice.kns.util.cache.CopiedObject
 
Classes in this File Line Coverage Branch Coverage Complexity
CopiedObject
0%
0/46
0%
0/10
3.167
 
 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  0
     public CopiedObject() {
 34  0
         oldSize = -1;
 35  0
     }
 36  
 
 37  0
     public CopiedObject( T cacheableObject ) {
 38  0
         oldSize = -1;
 39  0
         setContent( cacheableObject );
 40  0
     }
 41  
 
 42  
     /**
 43  
      * @return current value of bytes.
 44  
      */
 45  
     public int getSize() {
 46  0
         return size;
 47  
     }
 48  
 
 49  
     /**
 50  
      * @return current value of cacheableObject.
 51  
      */
 52  
     public T getContent() {
 53  0
         T copy = null;
 54  0
         if (content != null) {
 55  0
             ObjectInputStream ois = null;
 56  
             try {
 57  0
                 FastByteArrayInputStream deserializer = new FastByteArrayInputStream(content,size);
 58  0
                 ois = new ObjectInputStream(deserializer);
 59  0
                 copy = (T) ois.readObject();
 60  0
             }
 61  0
             catch (IOException e) {
 62  0
                 throw new CacheException("unable to complete getContent()", e);
 63  
             }
 64  0
             catch (ClassNotFoundException e) {
 65  0
                 throw new CacheException("unable to complete getContent()", e);
 66  
             }
 67  
             finally {
 68  0
                 try {
 69  0
                     if (ois != null) {
 70  0
                         ois.close();
 71  
                     }
 72  
                 }
 73  0
                 catch (IOException e) {
 74  
                     // ignoring this IOException, since the streams are going to be abandoned now anyway
 75  0
                 }
 76  0
             }
 77  
         }
 78  0
         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  0
         int copySize = 0;
 88  0
         if (cacheableObject != null) {
 89  0
             ObjectOutputStream oos = null;
 90  
             try {
 91  0
                 FastByteArrayOutputStream serializer = new FastByteArrayOutputStream();
 92  0
                 oos = new ObjectOutputStream(serializer);
 93  0
                 oos.writeObject(cacheableObject);
 94  
 
 95  0
                 if ( content != null ) {
 96  0
                     oldSize = size;
 97  
                 }
 98  0
                 size = serializer.getSize();
 99  0
                 content = serializer.getByteArray();
 100  0
             } catch (IOException e) {
 101  0
                 throw new CacheException("unable to complete deepCopy from src '" + cacheableObject.toString() + "'", e);
 102  
             }
 103  
             finally {
 104  0
                 try {
 105  0
                     if (oos != null) {
 106  0
                         oos.close();
 107  
                     }
 108  0
                 } catch (IOException e) {
 109  
                     // ignoring this IOException, since the streams are going to be abandoned now anyway
 110  0
                 }
 111  0
             }
 112  
         }
 113  0
     }
 114  
 
 115  
 
 116  
     /**
 117  
      * @return current value of oldSize.
 118  
      */
 119  
     public int getOldSize() {
 120  0
         return oldSize;
 121  
     }
 122  
 }