1 29 30 package nextapp.echo2.app; 31 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.OutputStream ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.Map ; 38 39 43 public class ResourceImageReference 44 extends StreamImageReference { 45 46 50 private static final int BUFFER_SIZE = 4096; 51 52 55 private static final Map extensionToContentType; 56 static { 57 Map map = new HashMap (); 58 map.put("gif", "image/gif"); 59 map.put("png", "image/png"); 60 map.put("jpeg", "image/jpeg"); 61 map.put("jpg", "image/jpg"); 62 map.put("bmp", "image/bmp"); 63 extensionToContentType = Collections.unmodifiableMap(map); 64 } 65 66 74 private static final String getContentType(String resourceName) { 75 String contentType; 76 77 int extensionDelimiterPosition = resourceName.lastIndexOf("."); 79 if (extensionDelimiterPosition == -1) { 80 throw new IllegalArgumentException ("Invalid file extension (resource has no extension: " + resourceName + ")"); 81 } else { 82 String extension = resourceName.substring(extensionDelimiterPosition + 1).toLowerCase(); 83 contentType = (String ) extensionToContentType.get(extension); 84 if (contentType == null) { 85 throw new IllegalArgumentException ("Invalid file extension (no matching content type: " + resourceName + ")"); 86 } 87 } 88 89 return contentType; 90 } 91 92 private Extent width, height; 93 private String contentType; 94 private String resource; 95 private String id; 96 97 103 public ResourceImageReference(String resource) { 104 this(resource, null, null, null); 105 } 106 107 117 public ResourceImageReference(String resource, String contentType) { 118 this(resource, contentType, null, null); 119 } 120 121 131 public ResourceImageReference(String resource, Extent width, Extent height) { 132 this(resource, null, width, height); 133 } 134 135 147 public ResourceImageReference(String resource, String contentType, Extent width, Extent height) { 148 super(); 149 150 if (resource.charAt(0) == '/') { 152 this.resource = resource.substring(1); 153 } else { 154 this.resource = resource; 155 } 156 157 this.contentType = contentType == null ? getContentType(resource) : contentType; 158 this.width = width; 159 this.height = height; 160 id = ApplicationInstance.generateSystemId(); 161 } 162 163 166 public boolean equals(Object o) { 167 if (!(o instanceof ResourceImageReference)) { 168 return false; 169 } 170 ResourceImageReference that = (ResourceImageReference) o; 171 if (!(this.resource == that.resource || (this.resource != null && this.resource.equals(that.resource)))) { 172 return false; 173 } 174 if (!(this.contentType == that.contentType || (this.contentType != null && this.contentType.equals(that.contentType)))) { 175 return false; 176 } 177 if (!(this.width == that.width || (this.width != null && this.width.equals(that.width)))) { 178 return false; 179 } 180 if (!(this.height == that.height || (this.height != null && this.height.equals(that.height)))) { 181 return false; 182 } 183 return true; 184 } 185 186 189 public String getContentType() { 190 return contentType; 191 } 192 193 196 public Extent getHeight() { 197 return height; 198 } 199 200 203 public String getRenderId() { 204 return id; 205 } 206 207 212 public String getResource() { 213 return resource; 214 } 215 216 219 public Extent getWidth() { 220 return width; 221 } 222 223 226 public int hashCode() { 227 return resource == null ? 0 : resource.hashCode(); 228 } 229 230 233 public void render(OutputStream out) 234 throws IOException { 235 InputStream in = null; 236 byte[] buffer = new byte[BUFFER_SIZE]; 237 int bytesRead = 0; 238 239 try { 240 in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); 241 if (in == null) { 242 throw new IllegalArgumentException ("Specified resource does not exist: " + resource + "."); 243 } 244 do { 245 bytesRead = in.read(buffer); 246 if (bytesRead > 0) { 247 out.write(buffer, 0, bytesRead); 248 } 249 } while (bytesRead > 0); 250 } finally { 251 if (in != null) { try { in.close(); } catch (IOException ex) { } } 252 } 253 } 254 } 255 | Popular Tags |