1 package org.mortbay.util; 16 17 import java.io.ByteArrayInputStream ; 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 25 26 34 35 public class CachedResource extends Resource 36 { 37 Resource _resource; 38 long _lastModified; 39 byte[] _buf ; 40 String [] _list; 41 42 43 CachedResource(Resource resource) 44 throws IOException 45 { 46 _resource=resource; 47 update(); 48 } 49 50 51 public synchronized boolean isUptoDate() 52 throws IOException 53 { 54 return _resource!=null && _resource.exists() && 55 _resource.lastModified()==_lastModified; 56 } 57 58 59 public synchronized boolean update() 60 throws IOException 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 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 102 public void release() 103 { 104 clear(); 105 _resource.release(); 106 } 107 108 109 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 getURL() 140 { 141 return _resource.getURL(); 142 } 143 144 145 public File getFile() 146 throws IOException 147 { 148 return _resource.getFile(); 149 } 150 151 152 155 public String getName() 156 { 157 return _resource.getName(); 158 } 159 160 161 164 public InputStream getInputStream() 165 throws IOException 166 { 167 if (_buf!=null) 168 return new ByteArrayInputStream (_buf); 169 return _resource.getInputStream(); 170 } 171 172 173 174 177 public OutputStream getOutputStream() 178 throws java.io.IOException , SecurityException 179 { 180 return _resource.getOutputStream(); 181 } 182 183 184 187 public synchronized boolean delete() 188 throws SecurityException 189 { 190 if (_resource.delete()) 191 { 192 clear(); 193 return true; 194 } 195 return false; 196 } 197 198 199 202 public synchronized boolean renameTo( Resource dest) 203 throws SecurityException 204 { 205 if (_resource.renameTo(dest)) 206 { 207 clear(); 208 return true; 209 } 210 return false; 211 } 212 213 214 217 public String [] list() 218 { 219 return _list; 220 } 221 222 223 227 public Resource addPath(String path) 228 throws IOException ,MalformedURLException 229 { 230 return _resource.addPath(path); 231 } 232 233 234 public String 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 o) 247 { 248 return _resource.equals(o); 249 } 250 251 252 public void writeTo(OutputStream os, long startByte, long count) 253 throws IOException 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 |