View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.mimetype.aperture;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  import org.modeshape.graph.mimetype.MimeTypeDetector;
29  import org.semanticdesktop.aperture.mime.identifier.MimeTypeIdentifier;
30  import org.semanticdesktop.aperture.mime.identifier.magic.MagicMimeTypeIdentifier;
31  import org.semanticdesktop.aperture.util.IOUtil;
32  
33  /**
34   * A {@link MimeTypeDetector} that uses the Aperture library.
35   */
36  public class ApertureMimeTypeDetector implements MimeTypeDetector {
37  
38      /**
39       * {@inheritDoc}
40       * 
41       * @throws IOException
42       * @see org.modeshape.graph.mimetype.MimeTypeDetector#mimeTypeOf(java.lang.String, java.io.InputStream)
43       */
44      public String mimeTypeOf( String name,
45                                InputStream content ) throws IOException {
46          /*
47              MimeTypes identifier = TikaConfig.getDefaultConfig().getMimeRepository();
48              MimeTypeDetectors mimeType = identifier.getMimeType(path.getLastSegment().getName().getLocalName(), stream);
49              return mimeType == null ? null : mimeType.getName();
50          */
51          MimeTypeIdentifier identifier = new MagicMimeTypeIdentifier();
52          // Read as many bytes of the file as desired by the MIME-type identifier
53          int minimumArrayLength = identifier.getMinArrayLength();
54          byte[] bytes = IOUtil.readBytes(content, minimumArrayLength);
55          // let the MimeTypeIdentifier determine the MIME-type of this file
56          return identifier.identify(bytes, name, null);
57      }
58  }