1 16 17 package org.springframework.core.io; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.URL ; 24 25 36 public abstract class AbstractResource implements Resource { 37 38 43 public boolean exists() { 44 try { 46 return getFile().exists(); 47 } 48 catch (IOException ex) { 49 try { 51 InputStream is = getInputStream(); 52 is.close(); 53 return true; 54 } 55 catch (Throwable isEx) { 56 return false; 57 } 58 } 59 } 60 61 64 public boolean isOpen() { 65 return false; 66 } 67 68 72 public URL getURL() throws IOException { 73 throw new FileNotFoundException (getDescription() + " cannot be resolved to URL"); 74 } 75 76 80 public File getFile() throws IOException { 81 throw new FileNotFoundException (getDescription() + " cannot be resolved to absolute file path"); 82 } 83 84 88 public Resource createRelative(String relativePath) throws IOException { 89 throw new FileNotFoundException ("Cannot create a relative resource for " + getDescription()); 90 } 91 92 96 public String getFilename() throws IllegalStateException { 97 throw new IllegalStateException (getDescription() + " does not carry a filename"); 98 } 99 100 111 public abstract String getDescription(); 112 113 114 118 public String toString() { 119 return getDescription(); 120 } 121 122 126 public boolean equals(Object obj) { 127 return (obj == this || 128 (obj instanceof Resource && ((Resource) obj).getDescription().equals(getDescription()))); 129 } 130 131 135 public int hashCode() { 136 return getDescription().hashCode(); 137 } 138 139 } 140 | Popular Tags |