KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > fjank > jcache > StreamCacheObject


1 /* Open Source Java Caching Service
2 * Copyright (C) 2002 Frank Karlstrøm
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * The author can be contacted by email: fjankk@sourceforge.net
18 */

19 package org.fjank.jcache;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.lang.ref.ReferenceQueue JavaDoc;
25 import javax.util.jcache.CacheException;
26 import javax.util.jcache.InvalidArgumentException;
27 import javax.util.jcache.ObjectNotFoundException;
28
29 /**
30  * A cacheobject which reads/writes the content with the aid of Input/Outputstreams
31  * Contains an internal reference to the wrapped object,
32  * so the override of SoftReference is probably broken on this object...
33  *
34  * @author Frank Karlstrøm
35  *
36  */

37 public class StreamCacheObject extends CacheObject {
38     private byte[] referent;
39     /**
40      * Creates a new StreamCacheObject object.
41      *
42      * @param key the name of this object
43      * @param referent the actuall object
44      * @param group the group this object belongs to
45      * @param q the ReferenceQueue this object goes to wneh the GC determines
46      * that this object is garbage.
47      */

48     public StreamCacheObject(
49         Object JavaDoc key,
50         Object JavaDoc referent,
51         CacheGroup group,
52         CacheRegion region,
53         ReferenceQueue JavaDoc q) throws CacheException {
54         super(key, referent, group, region, q);
55         //this.referent=serialize(referent);
56
}
57
58     
59
60     /**Overrides the normal get method by getting the Object,
61      * then creating an InputStream to this object.
62      *
63      *@todo ObjectNotFoundException is never thrown
64      */

65     public InputStream JavaDoc getInputStream() throws ObjectNotFoundException {
66         //just to trigger reference count
67
super.get();
68         return new ByteArrayInputStream JavaDoc(referent);
69         
70     }
71
72     /**gets an OutputStream to write to this object.
73      *@todo Exception is never thrown
74      * @return
75      */

76     public OutputStream JavaDoc getOutputStream() throws InvalidArgumentException {
77         return new CacheOutputStream(this);
78     }
79
80     /**
81      * @param bs
82      */

83      void setReferent(byte[] bs) {
84         referent = bs;
85     }
86
87 }
88
Popular Tags