1 28 package net.sf.jasperreports.engine.util; 29 30 import net.sf.jasperreports.engine.JRRenderable; 31 32 33 37 public class JRTypeSniffer 38 { 39 40 48 public static boolean isGIF(byte[] data) { 49 if(data.length < 3) { 51 return false; 52 } 53 54 byte[] first = new byte[3]; 56 System.arraycopy(data, 0, first, 0, 3); 57 if((new String (first)).equalsIgnoreCase("GIF")){ 58 return true; 59 } 60 61 return false; 62 } 63 64 69 public static boolean isJPEG(byte[] data) { 70 if(data.length < 2) { 72 return false; 73 } 74 75 if(data[0] == -1 && data[1] == -40) { 77 return true; 78 } 79 80 return false; 81 } 82 83 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 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 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 160 public static String getImageMimeType(byte imageType) 161 { 162 String 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 |