1 10 package org.mmbase.util.magicfile; 11 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.IOException ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import org.mmbase.util.logging.*; 18 19 26 public class MagicFile { 27 private static final Logger log = Logging.getLoggerInstance(MagicFile.class); 28 29 public static final String FAILED = "Failed to determine type"; 30 32 protected static int BUFSIZE = 4598; 33 36 private static MagicFile instance; 37 38 protected DetectorProvider detectors; 39 40 44 public static MagicFile getInstance() { 45 if (instance == null) { 46 instance = new MagicFile(); 47 } 48 return instance; 49 } 50 51 private MagicFile() { 52 DetectorProvider d = MagicXMLReader.getInstance(); 53 if (d == null) { 55 d = new MagicParser(); 56 } 57 detectors = d; 58 } 59 60 63 64 public List getDetectors() { 65 return detectors.getDetectors(); 66 } 67 68 71 protected String test(String path) { 72 try { 73 return getMimeType(new File (path)); 74 } catch (IOException e) { 75 return "File not found " + path; 76 } 77 } 78 82 protected String getMimeType(File file) throws IOException { 83 byte[] lithmus = new byte[BUFSIZE]; 84 FileInputStream fir = new FileInputStream (file); 86 int res = fir.read(lithmus, 0, BUFSIZE); 87 log.debug("read " + res + " bytes from " + file.getAbsolutePath()); 88 return getMimeType(lithmus); 89 } 90 91 96 public String getMimeType(byte[] input) { 97 byte[] lithmus; 98 99 if (input.length > BUFSIZE) { 100 lithmus = new byte[BUFSIZE]; 101 System.arraycopy(input, 0, lithmus, 0, BUFSIZE); 102 log.debug("getMimeType was called with big bytearray cutting to " + BUFSIZE + " bytes"); 103 } else { 104 lithmus = input; 105 } 106 107 List list = getDetectors(); 108 if (list == null) { 109 log.warn("No detectors found"); 110 return FAILED; 111 } 112 Iterator i = list.iterator(); 113 while (i.hasNext()) { 114 Detector detector = (Detector)i.next(); 115 log.debug("Trying " + detector.getMimeType()); 116 if (detector != null && detector.test(lithmus)) { 117 return detector.getMimeType(); 119 } 120 } 121 return FAILED; 122 } 123 124 127 public String extensionToMimeType(String extension) { 128 Iterator i = getDetectors().iterator(); 129 while (i.hasNext()) { 130 Detector detector = (Detector)i.next(); 131 Iterator j = detector.getExtensions().iterator(); 132 while (j.hasNext()) { 133 String ex = (String )j.next(); 134 if (ex.equalsIgnoreCase(extension)) { 135 return detector.getMimeType(); 136 } 137 } 138 } 139 return FAILED; 140 } 141 142 147 public String mimeTypeToExtension(String mimeType) { 148 Iterator i = getDetectors().iterator(); 149 while (i.hasNext()) { 150 Detector detector = (Detector)i.next(); 151 if (mimeType.equalsIgnoreCase(detector.getMimeType())) { 152 Iterator j = detector.getExtensions().iterator(); 153 if (j.hasNext()) { 154 String ex = (String )j.next(); 155 return ex; 156 } 157 } 158 } 159 return ""; 160 } 161 162 165 public String getMimeType(byte[] data, String extension) { 166 String result; 167 result = getMimeType(data); 168 if (result.equals(FAILED)) { 169 result = extensionToMimeType(extension); 170 } 171 return result; 172 } 173 174 178 public static void main(String [] argv) { 179 MagicFile magicFile = MagicFile.getInstance(); 180 181 if (argv.length == 1) { 182 try { 183 log.info(magicFile.getMimeType(new File (argv[0]))); 185 } catch (IOException e) { 186 log.info(argv[0] + " cannot be opened or read: " + e.toString()); 187 } 188 } else { 189 Iterator i = magicFile.getDetectors().iterator(); 191 while (i.hasNext()) { 192 Detector d = (Detector)i.next(); 193 log.info(d.toString()); 194 } 195 } 196 } 197 } 198 | Popular Tags |