diff --git a/src/main/java/com/j256/simplemagic/ContentInfoUtil.java b/src/main/java/com/j256/simplemagic/ContentInfoUtil.java index e36dcb1..391a188 100644 --- a/src/main/java/com/j256/simplemagic/ContentInfoUtil.java +++ b/src/main/java/com/j256/simplemagic/ContentInfoUtil.java @@ -15,6 +15,7 @@ import java.util.zip.GZIPInputStream; import com.j256.simplemagic.entries.MagicEntries; +import java.net.URL; /** *

@@ -186,6 +187,30 @@ public ContentInfoUtil(Reader reader, ErrorCallBack errorCallBack) throws IOExce this.errorCallBack = errorCallBack; this.magicEntries = readEntries(reader); } + + /** + * Return the content type for the URL or null if none of the magic entries matched. You might want to use + * the {@link ContentInfoInputStreamWrapper} class to delegate to an input-stream and determine content information + * at the same time. + * + * @throws IOException + * If there was a problem reading from the input-stream. + * @see ContentInfoInputStreamWrapper + */ + public ContentInfo findMatch(final URL url) throws IOException { + InputStream is = null; + try { + is = url.openStream(); + ContentInfo contentInfo = findMatch(is); + return contentInfo; + } catch (IOException ex) { + throw new IOException(ex); + } finally { + if(is != null) { + is.close(); + } + } + } /** * Return the content type for the file-path or null if none of the magic entries matched. @@ -205,6 +230,9 @@ public ContentInfo findMatch(String filePath) throws IOException { */ public ContentInfo findMatch(File file) throws IOException { int readSize = fileReadSize; + if(!file.canRead()) { + throw new IOException("Unable to read "+ file.getName()); + } if (file.length() < readSize) { readSize = (int) file.length(); } diff --git a/src/main/java/com/j256/simplemagic/ContentType.java b/src/main/java/com/j256/simplemagic/ContentType.java index be68484..f3b9353 100644 --- a/src/main/java/com/j256/simplemagic/ContentType.java +++ b/src/main/java/com/j256/simplemagic/ContentType.java @@ -179,6 +179,8 @@ public enum ContentType { VCARD("text/x-vcard", "vcard", "vcf"), /** Mpeg video */ VIDEO_MPEG("video/mpeg", "mpeg", "mpeg", "mpg", "mpe", "m1v", "m2v"), + /** VOTable data exchange format */ + VOTABLE("application/x-votable+xml", "votable", "vot", "xml"), /** VRML modeling file */ VRML("model/vrml", "vrml", "wrl", "vrml"), /** WAV audio */ @@ -974,5 +976,5 @@ public List getReferences() { */ public List getReferenceUrls() { return ianaDB.getIanaMetadata(this.getMimeType()).getReferenceURL(); - } + } } diff --git a/src/main/java/com/j256/simplemagic/entries/IanaEntries.java b/src/main/java/com/j256/simplemagic/entries/IanaEntries.java index b433c9e..028f328 100644 --- a/src/main/java/com/j256/simplemagic/entries/IanaEntries.java +++ b/src/main/java/com/j256/simplemagic/entries/IanaEntries.java @@ -9,8 +9,6 @@ import java.io.Reader; import java.util.HashMap; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import java.util.zip.GZIPInputStream; /** @@ -23,7 +21,7 @@ * * In addition to these elements, two URLs are created in order to locate the * description of the mime type and the URL of the articles. - * @author Jean-Christophe Malapert (jcmalapert@gmail.com) + * @author Jean-Christophe Malapert */ public class IanaEntries { diff --git a/src/main/java/com/j256/simplemagic/entries/IanaEntry.java b/src/main/java/com/j256/simplemagic/entries/IanaEntry.java index b18ad05..dff35c5 100644 --- a/src/main/java/com/j256/simplemagic/entries/IanaEntry.java +++ b/src/main/java/com/j256/simplemagic/entries/IanaEntry.java @@ -8,7 +8,7 @@ /** * IANA metadata coming from - * @author Jean-Christophe Malapert (jcmalapert@gmail.com) + * @author Jean-Christophe Malapert */ public class IanaEntry { diff --git a/src/main/resources/magic.gz b/src/main/resources/magic.gz index c7fa0f9..18b159c 100644 Binary files a/src/main/resources/magic.gz and b/src/main/resources/magic.gz differ diff --git a/src/test/java/com/j256/simplemagic/ContentInfoUtilTest.java b/src/test/java/com/j256/simplemagic/ContentInfoUtilTest.java index 7a3b705..1524660 100644 --- a/src/test/java/com/j256/simplemagic/ContentInfoUtilTest.java +++ b/src/test/java/com/j256/simplemagic/ContentInfoUtilTest.java @@ -20,6 +20,7 @@ import java.io.OutputStream; import java.io.Reader; import java.io.StringReader; +import java.net.URL; import java.util.Arrays; import org.easymock.EasyMock; @@ -206,8 +207,15 @@ public void testMultipleMagicFiles() throws Exception { checkFile(util, new FileType("/files/x.gif", ContentType.GIF, "gif", "image/gif", "GIF image data, version 89a, 32 x 32", false)); checkFile(util, new FileType("/files/jfif.jpg", ContentType.JPEG, "jpeg", "image/jpeg", - "JPEG image data, JFIF standard 1.01", false)); - } + "JPEG image data, JFIF standard 1.01", false)); + } + + @Test + public void testNetCDFMagicFile() throws IOException { + ContentInfoUtil util = new ContentInfoUtil(); + ContentInfo info = util.findMatch(new URL("https://www.unidata.ucar.edu/software/netcdf/examples/WMI_Lear.nc")); + assertEquals("Mime-type of netCDF", "application/x-netcdf", info.getMimeType()); + } @Test public void testPerformanceRun() throws Exception { @@ -229,6 +237,12 @@ public void testEmptyMimeType() { ContentInfoUtil util = getContentInfoUtil(); assertEquals(ContentType.EMPTY, util.findMatch(new byte[0]).getContentType()); } + + @Test(expected = IOException.class) + public void testUnableToReadFile() throws IOException { + ContentInfoUtil util = getContentInfoUtil(); + util.findMatch(new File("/ttttttt/fileNotExist")); + } @Test public void testFileRead() throws IOException { diff --git a/src/test/java/com/j256/simplemagic/ContentTypeTest.java b/src/test/java/com/j256/simplemagic/ContentTypeTest.java index f848fd8..a9d404e 100644 --- a/src/test/java/com/j256/simplemagic/ContentTypeTest.java +++ b/src/test/java/com/j256/simplemagic/ContentTypeTest.java @@ -1,5 +1,7 @@ package com.j256.simplemagic; +import java.io.IOException; +import java.net.URL; import static org.junit.Assert.assertEquals; import java.util.HashMap; @@ -41,5 +43,5 @@ public void testPrintDuplicates() { public void testFileExtensions() { assertEquals(ContentType.GIF, ContentType.fromFileExtension("gif")); assertEquals(ContentType.OTHER, ContentType.fromFileExtension("xyzzy")); - } + } } diff --git a/src/test/resources/magic b/src/test/resources/magic index 1223b3a..fa7a9a2 100644 --- a/src/test/resources/magic +++ b/src/test/resources/magic @@ -1,5 +1,18 @@ -0 string SIMPLE\x20\x20= FITS data -!:mime application/fits +0 string SIMPLE\x20\x20= FITS data +!:mime application/fits + +0 string \15 string >\0 +>>23 search/400 \