1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.io.BufferedInputStream ; 19 import java.io.IOException ; 20 21 import org.apache.avalon.framework.thread.ThreadSafe; 22 import org.apache.excalibur.source.Source; 23 import org.apache.excalibur.source.SourceException; 24 25 32 public class GIFSourceInspector extends AbstractImageSourceInspector implements ThreadSafe { 33 34 35 public GIFSourceInspector() { 36 } 37 38 41 protected final boolean isImageMimeType(Source source) { 42 final String uri = source.getURI(); 43 final int index = uri.lastIndexOf('.'); 44 if (index != -1) { 45 String extension = uri.substring(index); 46 return extension.equalsIgnoreCase(".gif"); 47 } 48 return false; 49 } 50 51 54 protected final boolean isImageFileType(Source source) throws SourceException { 55 BufferedInputStream in = null; 56 try { 57 in = new BufferedInputStream (source.getInputStream()); 58 byte[] buf = new byte[3]; 59 int count = in.read(buf, 0, 3); 60 if(count < 3) return false; 61 if ((buf[0] == (byte)'G') && 62 (buf[1] == (byte)'I') && 63 (buf[2] == (byte)'F')) 64 return true; 65 } catch (IOException ioe) { 66 throw new SourceException("Could not read source", ioe); 67 } finally { 68 if (in != null) 69 try { 70 in.close(); 71 } catch(Exception e) {} 72 } 73 return false; 74 } 75 76 79 protected final int[] getImageSize(Source source) throws SourceException { 80 BufferedInputStream in = null; 81 try { 82 in = new BufferedInputStream (source.getInputStream()); 83 byte[] buf = new byte[10]; 84 int count = in.read(buf, 0, 10); 85 if(count < 10) throw new SourceException("Not a valid GIF file!"); 86 if((buf[0]) != (byte)'G' 87 || (buf[1]) != (byte)'I' 88 || (buf[2]) != (byte)'F' ) 89 throw new SourceException("Not a valid GIF file!"); 90 91 int w1 = (buf[6] & 0xff) | (buf[6] & 0x80); 92 int w2 = (buf[7] & 0xff) | (buf[7] & 0x80); 93 int h1 = (buf[8] & 0xff) | (buf[8] & 0x80); 94 int h2 = (buf[9] & 0xff) | (buf[9] & 0x80); 95 96 int width = w1 + (w2 << 8); 97 int height = h1 + (h2 << 8); 98 99 int[] dim = { width, height }; 100 return dim; 101 } catch (IOException ioe) { 102 throw new SourceException("Could not read source", ioe); 103 } finally { 104 if (in != null) 105 try { 106 in.close(); 107 } catch (Exception e) {} 108 } 109 } 110 111 } 112 113 | Popular Tags |