/** Get a match from a stream of data. */ public static MagicMatch getMagicMatch(final byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
/** * print the contents of a magic file * * @param stream DOCUMENT ME! * * @throws MagicParseException DOCUMENT ME! */ public static void printMagicFile(PrintStream stream) throws MagicParseException { if (!initialized) { initialize(); } Collection<MagicMatcher> matchers = Magic.getMatchers(); log.debug("have " + matchers.size() + " matches"); MagicMatcher matcher = null; Iterator<MagicMatcher> i = matchers.iterator(); while (i.hasNext()) { matcher = (MagicMatcher) i.next(); log.debug("printing"); printMagicMatcher(stream, matcher, ""); } }
/** * DOCUMENT ME! * * @param args DOCUMENT ME! */ public static void main(String[] args) { try { if (args.length == 0) { System.err.println("usage: test <file>"); System.exit(1);; } File f = new File(args[0]); if (f.exists()) { MagicMatch match = Magic.getMagicMatch(f, true, false); System.out.println("filename: " + args[0]); printMagicMatch(System.out, match, ""); } else { System.err.println("file '" + f.getCanonicalPath() + "' not found"); } } catch (MagicMatchNotFoundException e) { System.out.println("no match found"); } catch (Exception e) { System.err.println("error: " + e); e.printStackTrace(System.err); } } }
/** * return the parsed MagicMatch objects that were created from the magic.xml * definitions * * @return the parsed MagicMatch objects * * @throws MagicParseException DOCUMENT ME! */ public static Collection getMatchers() throws MagicParseException { log.debug("getMatchers()"); if (!initialized) { initialize(); } Iterator i = magicParser.getMatchers().iterator(); ArrayList m = new ArrayList(); while (i.hasNext()) { MagicMatcher matcher = (MagicMatcher) i.next(); try { m.add(matcher.clone()); } catch (CloneNotSupportedException e) { log.error("failed to clone matchers"); throw new MagicParseException("failed to clone matchers"); } } return m; }
/** * print a magic match * * @param stream DOCUMENT ME! * @param matcher DOCUMENT ME! * @param spacing DOCUMENT ME! */ private static void printMagicMatcher(PrintStream stream, MagicMatcher matcher, String spacing) { stream.println(spacing + "name: " + matcher.getMatch().getDescription()); stream.println(spacing + "children: "); Collection matchers = matcher.getSubMatchers(); Iterator i = matchers.iterator(); while (i.hasNext()) { printMagicMatcher(stream, (MagicMatcher) i.next(), spacing + " "); } }
/** * print a magic match * * @param stream DOCUMENT ME! * @param match DOCUMENT ME! * @param spacing DOCUMENT ME! */ public static void printMagicMatch(PrintStream stream, MagicMatch match, String spacing) { stream.println(spacing + "============================="); stream.println(spacing + "mime type: " + match.getMimeType()); stream.println(spacing + "description: " + match.getDescription()); stream.println(spacing + "extension: " + match.getExtension()); stream.println(spacing + "test: " + new String(match.getTest().array())); stream.println(spacing + "bitmask: " + match.getBitmask()); stream.println(spacing + "offset: " + match.getOffset()); stream.println(spacing + "length: " + match.getLength()); stream.println(spacing + "type: " + match.getType()); stream.println(spacing + "comparator: " + match.getComparator()); stream.println(spacing + "============================="); Collection submatches = match.getSubMatches(); Iterator i = submatches.iterator(); while (i.hasNext()) { printMagicMatch(stream, (MagicMatch) i.next(), spacing + " "); } }
/** * return the parsed MagicMatch objects that were created from the magic.xml * definitions * * @return the parsed MagicMatch objects * * @throws MagicParseException DOCUMENT ME! */ public static Collection<MagicMatcher> getMatchers() throws MagicParseException { log.debug("getMatchers()"); if (!initialized) { initialize(); } Iterator<MagicMatcher> i = magicParser.getMatchers().iterator(); List<MagicMatcher> m = new ArrayList<MagicMatcher>(); while (i.hasNext()) { MagicMatcher matcher = (MagicMatcher) i.next(); try { m.add(matcher.clone()); } catch (CloneNotSupportedException e) { log.error("failed to clone matchers"); throw new MagicParseException("failed to clone matchers"); } } return m; }
/** Create a parser and initialize it. */ public static synchronized void initialize() throws MagicParseException { if (!initialized) { magicParser = new MagicParser(); magicParser.initialize(); // build hint map final Iterator<MagicMatcher> i = magicParser.getMatchers().iterator(); while (i.hasNext()) { final MagicMatcher matcher = i.next(); final String ext = matcher.getMatch().getExtension(); if (ext != null && !ext.trim().equals("")) { //$NON-NLS-1$ addHint(ext, matcher); } else if (matcher.getMatch().getType().equals("detector")) { //$NON-NLS-1$ final String[] exts = matcher.getDetectorExtensions(); for (final String ext2 : exts) { addHint(ext2, matcher); } } } initialized = true; } }
/** * print a magic match * * @param stream DOCUMENT ME! * @param matcher DOCUMENT ME! * @param spacing DOCUMENT ME! */ private static void printMagicMatcher(PrintStream stream, MagicMatcher matcher, String spacing) { stream.println(spacing + "name: " + matcher.getMatch().getDescription()); stream.println(spacing + "children: "); Collection<MagicMatcher> matchers = matcher.getSubMatchers(); Iterator<MagicMatcher> i = matchers.iterator(); while (i.hasNext()) { printMagicMatcher(stream, (MagicMatcher) i.next(), spacing + " "); } }
/** * print a magic match * * @param stream DOCUMENT ME! * @param match DOCUMENT ME! * @param spacing DOCUMENT ME! */ public static void printMagicMatch(PrintStream stream, MagicMatch match, String spacing) { stream.println(spacing + "============================="); stream.println(spacing + "mime type: " + match.getMimeType()); stream.println(spacing + "description: " + match.getDescription()); stream.println(spacing + "extension: " + match.getExtension()); stream.println(spacing + "test: " + new String(match.getTest().array())); stream.println(spacing + "bitmask: " + match.getBitmask()); stream.println(spacing + "offset: " + match.getOffset()); stream.println(spacing + "length: " + match.getLength()); stream.println(spacing + "type: " + match.getType()); stream.println(spacing + "comparator: " + match.getComparator()); stream.println(spacing + "============================="); Collection<MagicMatch> submatches = match.getSubMatches(); Iterator<MagicMatch> i = submatches.iterator(); while (i.hasNext()) { printMagicMatch(stream, (MagicMatch) i.next(), spacing + " "); } }
/** * get a match from a stream of data * * @param data DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
MagicMatch match = Magic.getMagicMatch(f, true, false); printMagicMatch(System.out, match, "");
/** * print the contents of a magic file * * @param stream DOCUMENT ME! * * @throws MagicParseException DOCUMENT ME! */ public static void printMagicFile(PrintStream stream) throws MagicParseException { if (!initialized) { initialize(); } Collection matchers = Magic.getMatchers(); log.debug("have " + matchers.size() + " matches"); MagicMatcher matcher = null; Iterator i = matchers.iterator(); while (i.hasNext()) { matcher = (MagicMatcher) i.next(); log.debug("printing"); printMagicMatcher(stream, matcher, ""); } }
/** Get a match from a stream of data. */ public static MagicMatch getMagicMatch(final byte[] data, final boolean onlyMimeMatch) throws MagicParseException, MagicMatchNotFoundException, MagicException { if (!initialized) { initialize(); } final Collection<MagicMatcher> matchers = magicParser.getMatchers(); MagicMatcher matcher = null; MagicMatch match = null; final Iterator<MagicMatcher> i = matchers.iterator(); while (i.hasNext()) { matcher = i.next(); try { if ((match = matcher.test(data, onlyMimeMatch)) != null) { return match; } } catch (final Throwable e) { throw new MagicException(e); } } throw new MagicMatchNotFoundException(); } }
/** * get a match from a stream of data * * @param data DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(byte[] data) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(data, false); }
/** * get a match from a file * * @param file the file to match content in * @param extensionHints whether or not to use extension to optimize order of content tests * * @return the MagicMatch object representing a match in the file * * @throws MagicParseException DOCUMENT ME! * @throws MagicMatchNotFoundException DOCUMENT ME! * @throws MagicException DOCUMENT ME! */ public static MagicMatch getMagicMatch(File file, boolean extensionHints) throws MagicParseException, MagicMatchNotFoundException, MagicException { return getMagicMatch(file, extensionHints, false); }