KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > GIFSourceInspector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.source.impl;
17
18 import java.io.BufferedInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
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 /**
26  * This source inspector adds extra attributes for image files.
27  *
28  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
29  * @author <a HREF="mailto:balld@webslingerZ.com">Donald A. Ball Jr.</a>
30  * @version CVS $Id: GIFSourceInspector.java 30932 2004-07-29 17:35:38Z vgritsenko $
31  */

32 public class GIFSourceInspector extends AbstractImageSourceInspector implements ThreadSafe {
33     
34     
35     public GIFSourceInspector() {
36     }
37     
38     /**
39      * Checks the source uri for the .gif extension.
40      */

41     protected final boolean isImageMimeType(Source source) {
42         final String JavaDoc uri = source.getURI();
43         final int index = uri.lastIndexOf('.');
44         if (index != -1) {
45             String JavaDoc extension = uri.substring(index);
46             return extension.equalsIgnoreCase(".gif");
47         }
48         return false;
49     }
50     
51     /**
52      * Checks that this is in fact a gif file.
53      */

54     protected final boolean isImageFileType(Source source) throws SourceException {
55         BufferedInputStream JavaDoc in = null;
56         try {
57             in = new BufferedInputStream JavaDoc(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 JavaDoc ioe) {
66             throw new SourceException("Could not read source", ioe);
67         } finally {
68             if (in != null)
69                 try {
70                     in.close();
71                 } catch(Exception JavaDoc e) {}
72         }
73         return false;
74     }
75
76     /**
77      * Returns width as first element, height as second
78      */

79     protected final int[] getImageSize(Source source) throws SourceException {
80         BufferedInputStream JavaDoc in = null;
81         try {
82             in = new BufferedInputStream JavaDoc(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 JavaDoc ioe) {
102             throw new SourceException("Could not read source", ioe);
103         } finally {
104             if (in != null)
105                 try {
106                     in.close();
107                 } catch (Exception JavaDoc e) {}
108         }
109     }
110
111 }
112
113
Popular Tags