Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/com/j256/simplemagic/ContentInfoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.zip.GZIPInputStream;

import com.j256.simplemagic.entries.MagicEntries;
import java.net.URL;

/**
* <p>
Expand Down Expand Up @@ -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.
Expand All @@ -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();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/j256/simplemagic/ContentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -974,5 +976,5 @@ public List<String> getReferences() {
*/
public List<String> getReferenceUrls() {
return ianaDB.getIanaMetadata(this.getMimeType()).getReferenceURL();
}
}
}
4 changes: 1 addition & 3 deletions src/main/java/com/j256/simplemagic/entries/IanaEntries.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -23,7 +21,7 @@
* </ul>
* 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 ([email protected])
* @author Jean-Christophe Malapert
*/
public class IanaEntries {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/j256/simplemagic/entries/IanaEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* <a href="https://www.iana.org/assignments/media-types/media-types.xhtml">IANA metadata</a> coming from
* @author Jean-Christophe Malapert ([email protected])
* @author Jean-Christophe Malapert
*/
public class IanaEntry {

Expand Down
Binary file modified src/main/resources/magic.gz
Binary file not shown.
18 changes: 16 additions & 2 deletions src/test/java/com/j256/simplemagic/ContentInfoUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/j256/simplemagic/ContentTypeTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,5 +43,5 @@ public void testPrintDuplicates() {
public void testFileExtensions() {
assertEquals(ContentType.GIF, ContentType.fromFileExtension("gif"));
assertEquals(ContentType.OTHER, ContentType.fromFileExtension("xyzzy"));
}
}
}
23 changes: 20 additions & 3 deletions src/test/resources/magic
Original file line number Diff line number Diff line change
@@ -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 \<?xml\ version="
>15 string >\0
>>23 search/400 \<VOTABLE VOTABLE data exchange format
!:mime application/x-votable+xml

0 string \<?xml\ version="
>15 string >\0
>>23 search/400 \<xs:schema XSD schema

0 string OggS Ogg, an open source media container format
!:mime application/ogg


0 lelong 0xc3cbc6c5 RISC OS Chunk data
>12 string OBJ_ \b, AOF object
Expand Down Expand Up @@ -5733,6 +5746,7 @@
0 lelong 20000630 OpenEXR image data

0 string CDF\001 NetCDF Data Format data
!:mime application/x-netcdf

0 belong 0x0e031301 Hierarchical Data Format (version 4) data
!:mime application/x-hdf
Expand Down Expand Up @@ -10329,7 +10343,7 @@
>10 byte&0x0B 2 - ADPCM3 encoding
>10 byte&0x0B 3 - ADPCM4 encoding
>10 byte&0x0B 8 - New ADPCM3 encoding
>10 byte&0x04 4 with resync
>10 byte&0x04 4 with resync

# EBML id:
0 belong 0x1a45dfa3
Expand All @@ -10340,3 +10354,6 @@
!:mime video/webm
>>&1 string matroska Matroska data
!:mime video/x-matroska