private Optional<String> guessExtension(byte[] bytes) { try { // No extension : try to guess it MagicMatch match = Magic.getMagicMatch(bytes); String guessedExtension = match.getExtension(); if (!Strings.isNullOrEmpty(guessedExtension)) { return Optional.of(guessedExtension); } else { return Optional.absent(); } } catch (Exception e) { this.logger.warn("Error while attempting to guess attachment extension", e); return Optional.absent(); } }
/** * 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 + " "); } }
/** * 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 + " "); } }
String ext = matcher.getMatch().getExtension();
String ext = matcher.getMatch().getExtension();
/** 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; } }
fileExtension = "txt"; else if (magicMatch == null || magicMatch.getExtension().equals("")) { fileExtension = magicMatch.getExtension();