KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > CachedResource


1 // ========================================================================
2
// $Id: CachedResource.java,v 1.6 2004/05/09 20:32:49 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15
package org.mortbay.util;
16
17 import java.io.ByteArrayInputStream JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 /* ------------------------------------------------------------ */
26 /** Cached resource class.
27  *
28  * This resource caches in memory the contents of another resource.
29  * The update() method must be called to check if the real resource has
30  * been modified.
31  * @version $Id: CachedResource.java,v 1.6 2004/05/09 20:32:49 gregwilkins Exp $
32  * @author Greg Wilkins (gregw)
33  */

34
35 public class CachedResource extends Resource
36 {
37     Resource _resource;
38     long _lastModified;
39     byte[] _buf ;
40     String JavaDoc[] _list;
41     
42     /* ------------------------------------------------------------ */
43     CachedResource(Resource resource)
44         throws IOException JavaDoc
45     {
46         _resource=resource;
47         update();
48     }
49
50     /* ------------------------------------------------------------ */
51     public synchronized boolean isUptoDate()
52         throws IOException JavaDoc
53     {
54         return _resource!=null && _resource.exists() &&
55             _resource.lastModified()==_lastModified;
56     }
57     
58     /* ------------------------------------------------------------ */
59     public synchronized boolean update()
60         throws IOException JavaDoc
61     {
62         if (_resource!=null && !_resource.exists())
63         {
64             clear();
65             return true;
66         }
67         
68         long lm=_resource.lastModified();
69         
70         if (lm==_lastModified && (_buf!=null || _list!=null))
71             return false;
72         _lastModified=lm;
73         
74         if (_resource.isDirectory())
75             _list=_resource.list();
76
77         if (_list==null)
78         {
79             int l=(int)_resource.length();
80             if (l<0)
81                 l=1024;
82             ByteArrayOutputStream2 bout = new ByteArrayOutputStream2(l);
83             InputStream JavaDoc in = _resource.getInputStream();
84             try{IO.copy(in,bout);} finally {in.close();}
85             _buf=bout.getBuf();
86             if (_buf.length!=l)
87                 _buf=bout.toByteArray();
88         }
89         return true;
90     }
91     
92     /* ------------------------------------------------------------ */
93     public synchronized void clear()
94     {
95         _buf=null;
96         _list=null;
97     }
98
99     /* ------------------------------------------------------------ */
100     /** Release any resources held by the resource.
101      */

102     public void release()
103     {
104         clear();
105         _resource.release();
106     }
107
108     /* ------------------------------------------------------------ */
109     /**
110      * Returns true if the respresened resource exists.
111      */

112     public synchronized boolean exists()
113     {
114         return _buf!=null || _list!=null;
115     }
116
117     /* ------------------------------------------------------------ */
118     public boolean isDirectory()
119     {
120         return _list!=null;
121     }
122
123
124     /* ------------------------------------------------------------ */
125     public long lastModified()
126     {
127         return _lastModified;
128     }
129
130     /* ------------------------------------------------------------ */
131     public long length()
132     {
133         if (_buf!=null)
134             return _buf.length;
135         return -1;
136     }
137
138     /* ------------------------------------------------------------ */
139     public URL JavaDoc getURL()
140     {
141         return _resource.getURL();
142     }
143
144     /* ------------------------------------------------------------ */
145     public File JavaDoc getFile()
146         throws IOException JavaDoc
147     {
148         return _resource.getFile();
149     }
150
151     /* ------------------------------------------------------------ */
152     /**
153      * Returns the name of the resource
154      */

155     public String JavaDoc getName()
156     {
157         return _resource.getName();
158     }
159
160     /* ------------------------------------------------------------ */
161     /**
162      * Returns an input stream to the resource
163      */

164     public InputStream JavaDoc getInputStream()
165         throws IOException JavaDoc
166     {
167         if (_buf!=null)
168             return new ByteArrayInputStream JavaDoc(_buf);
169         return _resource.getInputStream();
170     }
171
172
173     /* ------------------------------------------------------------ */
174     /**
175      * Returns an output stream to the resource
176      */

177     public OutputStream JavaDoc getOutputStream()
178         throws java.io.IOException JavaDoc, SecurityException JavaDoc
179     {
180         return _resource.getOutputStream();
181     }
182
183     /* ------------------------------------------------------------ */
184     /**
185      * Deletes the given resource
186      */

187     public synchronized boolean delete()
188         throws SecurityException JavaDoc
189     {
190         if (_resource.delete())
191         {
192             clear();
193             return true;
194         }
195         return false;
196     }
197
198     /* ------------------------------------------------------------ */
199     /**
200      * Rename the given resource
201      */

202     public synchronized boolean renameTo( Resource dest)
203         throws SecurityException JavaDoc
204     {
205         if (_resource.renameTo(dest))
206         {
207             clear();
208             return true;
209         }
210         return false;
211     }
212
213     /* ------------------------------------------------------------ */
214     /**
215      * Returns a list of resource names contained in the given resource
216      */

217     public String JavaDoc[] list()
218     {
219         return _list;
220     }
221
222     /* ------------------------------------------------------------ */
223     /**
224      * Returns the resource contained inside the current resource with the
225      * given name
226      */

227     public Resource addPath(String JavaDoc path)
228         throws IOException JavaDoc,MalformedURLException JavaDoc
229     {
230         return _resource.addPath(path);
231     }
232
233     /* ------------------------------------------------------------ */
234     public String JavaDoc toString()
235     {
236         return _resource.toString();
237     }
238
239     /* ------------------------------------------------------------ */
240     public int hashCode()
241     {
242         return _resource.hashCode();
243     }
244     
245     /* ------------------------------------------------------------ */
246     public boolean equals( Object JavaDoc o)
247     {
248         return _resource.equals(o);
249     }
250
251     /* ------------------------------------------------------------ */
252     public void writeTo(OutputStream JavaDoc os, long startByte, long count)
253         throws IOException JavaDoc
254     {
255         if (count<0)
256             count=_buf.length-startByte;
257         if (_buf!=null)
258             os.write(_buf, (int) startByte, (int) count);
259     }
260     
261     /* ------------------------------------------------------------ */
262     public byte[] getCachedData()
263     {
264         return _buf;
265     }
266     
267     /* ------------------------------------------------------------ */
268     public void setCachedData(byte[] buf)
269     {
270         _buf = buf;
271     }
272
273     
274 }
275
Popular Tags