001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.source.impl;
018:
019: import java.io.BufferedInputStream;
020: import java.io.IOException;
021:
022: import org.apache.avalon.framework.thread.ThreadSafe;
023: import org.apache.excalibur.source.Source;
024: import org.apache.excalibur.source.SourceException;
025:
026: /**
027: * This source inspector adds extra attributes for image files.
028: *
029: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
030: * @author <a href="mailto:balld@webslingerZ.com">Donald A. Ball Jr.</a>
031: * @version CVS $Id: GIFSourceInspector.java 433543 2006-08-22 06:22:54Z crossley $
032: */
033: public class GIFSourceInspector extends AbstractImageSourceInspector
034: implements ThreadSafe {
035:
036: public GIFSourceInspector() {
037: }
038:
039: /**
040: * Checks the source uri for the .gif extension.
041: */
042: protected final boolean isImageMimeType(Source source) {
043: final String uri = source.getURI();
044: final int index = uri.lastIndexOf('.');
045: if (index != -1) {
046: String extension = uri.substring(index);
047: return extension.equalsIgnoreCase(".gif");
048: }
049: return false;
050: }
051:
052: /**
053: * Checks that this is in fact a gif file.
054: */
055: protected final boolean isImageFileType(Source source)
056: throws SourceException {
057: BufferedInputStream in = null;
058: try {
059: in = new BufferedInputStream(source.getInputStream());
060: byte[] buf = new byte[3];
061: int count = in.read(buf, 0, 3);
062: if (count < 3)
063: return false;
064: if ((buf[0] == (byte) 'G') && (buf[1] == (byte) 'I')
065: && (buf[2] == (byte) 'F'))
066: return true;
067: } catch (IOException ioe) {
068: throw new SourceException("Could not read source", ioe);
069: } finally {
070: if (in != null)
071: try {
072: in.close();
073: } catch (Exception e) {
074: }
075: }
076: return false;
077: }
078:
079: /**
080: * Returns width as first element, height as second
081: */
082: protected final int[] getImageSize(Source source)
083: throws SourceException {
084: BufferedInputStream in = null;
085: try {
086: in = new BufferedInputStream(source.getInputStream());
087: byte[] buf = new byte[10];
088: int count = in.read(buf, 0, 10);
089: if (count < 10)
090: throw new SourceException("Not a valid GIF file!");
091: if ((buf[0]) != (byte) 'G' || (buf[1]) != (byte) 'I'
092: || (buf[2]) != (byte) 'F')
093: throw new SourceException("Not a valid GIF file!");
094:
095: int w1 = (buf[6] & 0xff) | (buf[6] & 0x80);
096: int w2 = (buf[7] & 0xff) | (buf[7] & 0x80);
097: int h1 = (buf[8] & 0xff) | (buf[8] & 0x80);
098: int h2 = (buf[9] & 0xff) | (buf[9] & 0x80);
099:
100: int width = w1 + (w2 << 8);
101: int height = h1 + (h2 << 8);
102:
103: int[] dim = { width, height };
104: return dim;
105: } catch (IOException ioe) {
106: throw new SourceException("Could not read source", ioe);
107: } finally {
108: if (in != null)
109: try {
110: in.close();
111: } catch (Exception e) {
112: }
113: }
114: }
115:
116: }
|