1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27 public class CopiedObject<T extends Serializable> {
28
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
44
45 public int getSize() {
46 return size;
47 }
48
49
50
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
75 }
76 }
77 }
78 return copy;
79 }
80
81
82
83
84
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
110 }
111 }
112 }
113 }
114
115
116
117
118
119 public int getOldSize() {
120 return oldSize;
121 }
122 }