KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRTypeSniffer


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import net.sf.jasperreports.engine.JRRenderable;
31
32
33 /**
34  * @author George Stojanoff (gstojanoff@jaspersoft.com), Flavius Sana (flavius_sana@users.sourceforge.net)
35  * @version $Id: JRTypeSniffer.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
36  */

37 public class JRTypeSniffer
38 {
39
40     /**
41      * Sniffs an incoming byte array to see if the first 3 characters
42      * are GIF. This is also known as the GIF signiture. See
43      * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF87a.txt for more details
44      * Note: Perhaps we should be checking for the more restive string GIF87a but
45      * I am not sure if older GIF images are sill out there in use on the web.
46      * Note: This method only really needs the first 3 bytes.
47      */

48     public static boolean isGIF(byte[] data) {
49         // chech if the image data length is less than 3 bytes
50
if(data.length < 3) {
51             return false;
52         }
53         
54         // get the first three characters
55
byte[] first = new byte[3];
56         System.arraycopy(data, 0, first, 0, 3);
57         if((new String JavaDoc(first)).equalsIgnoreCase("GIF")){
58             return true;
59         }
60     
61         return false;
62     }
63     
64     /**
65      * Sniffs an incoming byte array to see if the starting value is 0xffd8
66      * which is the "header" for JPEG data
67      * Note: This method only really needs the first 2 bytes.
68      */

69     public static boolean isJPEG(byte[] data) {
70         // check if the image data length is less than 2 bytes
71
if(data.length < 2) {
72             return false;
73         }
74         
75         //0xff is -1 and 0xd8 is -40
76
if(data[0] == -1 && data[1] == -40) {
77             return true;
78         }
79         
80         return false;
81     }
82     
83     /**
84      * Sniffs an incoming byte array to see if the first eight
85      * bytes are the following (decimal) values:
86      * 137 80 78 71 13 10 26 10
87      * which is the "signature" for PNG data
88      * See http://www.w3.org/TR/PNG/#5PNG-file-signature
89      * for more details.
90      * Note: This method only really needs the first 8 bytes.
91      */

92     public static boolean isPNG(byte[] data) {
93         if(data.length < 8) {
94             return false;
95         }
96         
97         if(data[0] == -119 &&
98             data[1] == 80 &&
99             data[2] == 78 &&
100             data[3] == 71 &&
101             data[4] == 13 &&
102             data[5] == 10 &&
103             data[6] == 26 &&
104             data[7] == 10) {
105             return true;
106         }
107         
108         return false;
109     }
110     
111     /**
112      * Sniffs an incoming byte array to see if the starting value is 0x4949
113      * (little endian) or 0x4D4D (big endian) which is the "header" for TIFF data
114      * The TIFF standards supports both endians.
115      * See http://palimpsest.stanford.edu/bytopic/imaging/std/tiff5.html
116      * for more details.
117      * Note: This method only really needs the first 2 bytes.
118      */

119     public static boolean isTIFF(byte[] data) {
120         if(data.length < 2) {
121             return false;
122         }
123         
124         if((data[0] == 73 && data[1] == 73) ||
125            (data[0] == 77 && data[1] == 77)) {
126             return true;
127         }
128         
129         return false;
130     }
131
132     /**
133      *
134      */

135     public static byte getImageType(byte[] data)
136     {
137         if (JRTypeSniffer.isGIF(data))
138         {
139             return JRRenderable.IMAGE_TYPE_GIF;
140         }
141         else if (JRTypeSniffer.isJPEG(data))
142         {
143             return JRRenderable.IMAGE_TYPE_JPEG;
144         }
145         else if (JRTypeSniffer.isPNG(data))
146         {
147             return JRRenderable.IMAGE_TYPE_PNG;
148         }
149         else if (JRTypeSniffer.isTIFF(data))
150         {
151             return JRRenderable.IMAGE_TYPE_TIFF;
152         }
153         
154         return JRRenderable.IMAGE_TYPE_UNKNOWN;
155     }
156
157     /**
158      *
159      */

160     public static String JavaDoc getImageMimeType(byte imageType)
161     {
162         String JavaDoc mimeType = null;
163         
164         switch (imageType)
165         {
166             case JRRenderable.IMAGE_TYPE_GIF :
167             {
168                 mimeType = JRRenderable.MIME_TYPE_GIF;
169                 break;
170             }
171             case JRRenderable.IMAGE_TYPE_JPEG :
172             {
173                 mimeType = JRRenderable.MIME_TYPE_JPEG;
174                 break;
175             }
176             case JRRenderable.IMAGE_TYPE_PNG :
177             {
178                 mimeType = JRRenderable.MIME_TYPE_PNG;
179                 break;
180             }
181             case JRRenderable.IMAGE_TYPE_TIFF :
182             {
183                 mimeType = JRRenderable.MIME_TYPE_TIFF;
184                 break;
185             }
186             default :
187             {
188             }
189         }
190
191         return mimeType;
192     }
193     
194 }
195
Popular Tags