1 2 24 25 26 27 28 29 package com.lutris.classloader; 30 31 import java.io.ByteArrayOutputStream ; 34 import java.io.FileNotFoundException ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 38 import com.lutris.logging.LogChannel; 39 40 75 public abstract class Resource { 76 77 79 80 protected String name = null; 81 82 83 protected ClassPathEntry location = null; 84 85 86 protected long size = -1; 87 88 89 protected long lastModifiedTime = -1; 90 91 92 protected boolean loggingEnabled = false; 93 94 95 protected LogChannel logChannel; 97 99 100 protected int logLevel; 102 104 106 122 protected Resource(String name, ClassPathEntry location, 123 LogChannel loadLogChannel) 125 throws NullPointerException { 127 this.logChannel = loadLogChannel; 129 if (logChannel != null) { 131 this.logLevel = logChannel.getLevel(MultiClassLoader.LOG_LEVEL); 133 loggingEnabled = logChannel.isEnabled(logLevel); 135 } 140 if (name == null || location == null) { 141 throw new NullPointerException (); 142 } 143 this.name = name; 144 this.location = location; 145 } 146 147 149 155 public String getName() { 156 return name; 157 } 158 159 165 public ClassPathEntry getLocation() { 166 return location; 167 } 168 169 177 public String toString() { 178 return "[" + location + "][" + name + "]"; 179 } 180 181 189 public long getSize() { 190 return size; 191 } 192 193 202 public long getLastModifiedTime() { 203 return lastModifiedTime; 204 } 205 206 212 public long getTime() { 213 return lastModifiedTime; 214 } 215 216 223 public abstract long getCurrentLastModifiedTime() throws FileNotFoundException ; 224 225 231 public boolean hasBeenModified() throws FileNotFoundException { 232 long currentTime = getCurrentLastModifiedTime(); 233 if (loggingEnabled) { 234 logChannel.write(logLevel, "hasBeenModified: " + getName() 236 + ": current time=" + currentTime 238 + ", last time=" + lastModifiedTime); 239 } 240 return (currentTime > lastModifiedTime); 241 } 242 243 250 private byte[] getBytesKnowSize(InputStream inputStream) 251 throws IOException { 252 byte[] bytes = new byte[(int)size]; 253 int bytesRead; 254 255 int idx = 0; 258 while (idx < size) { 259 if (inputStream.available() == 0) { 260 break; 261 } 262 if ((bytesRead = inputStream.read(bytes, idx, ((int)size)-idx)) == -1) { 263 break; 264 } 265 idx += bytesRead; 266 } 267 268 if (idx != size) { 269 throw new IOException ("Read of resource expected " + size 270 + " bytes, got " + idx + " bytes"); 271 } 272 return bytes; 273 } 274 275 282 private byte[] getBytesUnknowSize(InputStream inputStream) 283 throws IOException { 284 int bytesRead; 285 byte[] byteBuffer = new byte[10240]; 286 ByteArrayOutputStream byteStream = new ByteArrayOutputStream (); 287 while (true) { 288 if (inputStream.available() == 0) { 289 break; 290 } 291 if ((bytesRead = inputStream.read(byteBuffer)) == -1) { 292 break; 293 } 294 byteStream.write(byteBuffer, 0, bytesRead); 295 } 296 return byteStream.toByteArray(); 297 } 298 299 308 public byte[] getBytes() throws IOException { 309 InputStream inputStream = getInputStream(); 310 if (inputStream == null) { 311 throw new IOException ("Null InputStream for resource, " + this); 312 } 313 try { 314 if (size < 0) { 315 return getBytesUnknowSize(inputStream); 316 } else { 317 return getBytesKnowSize(inputStream); 318 } 319 } finally { 320 inputStream.close(); 321 } 322 } 323 324 333 public abstract InputStream getInputStream() throws IOException ; 334 335 342 public boolean equals(Resource resource) { 343 if (name.equals(resource.getName()) 344 && location.equals(resource.getLocation()) 345 && (size == resource.getSize()) 346 && (lastModifiedTime == resource.getLastModifiedTime())) { 347 return true; 348 } 349 return false; 350 } 351 } 352 | Popular Tags |