package ch.werner_randelshofer.activation;/*Copyright (C) 1999 Werner Randelshoferwerner.randelshofer@mythen.chhttp://www.mythen.ch/w.randelshofer/    Permission to use this release of CTDFileTypeMap is herebygranted without fee provided that the complete copyrightnotice and this permission notice appear in all copiesand in supporting documentation.*/import java.io.*;import javax.activation.*;/**This file data source uses a ContentTypeDetector todetermine the content type of the file.@author	Werner Randelshofer, Staldenmattweg 2, CH-6405 Immensee, Switzerland.@version	1998-06-12	Created.*/public class CTDFileTypeMapextends FileTypeMap	{	private ContentTypeDetector detector_;	/**	Creates a new instance	No ContentTypeDetector will be used to determine the	content type of the file (#getContentType will always	return "application/octet-stream".	*/	public CTDFileTypeMap()		{ }	/**	Creates a new instance.	The specified ContentTypeDetector will be used to determine the	content type of the file.	*/	public CTDFileTypeMap(ContentTypeDetector detector)		{		detector_ = detector;		}	/**	Sets the ContentTypeDetector that will be	used for determining the content type of the file.	*/	public void setCTD(ContentTypeDetector detector)		{		detector_ = detector;		}	/**	Returns the ContentTypeDetector that will be	used for determining the content type of the file.	*/	public ContentTypeDetector getCTD()		{		return detector_;		}	/**	Return the base MIME Type of this data. This method is	expected to ALWAYS return a valid (non-null) MIME Type.	We suggest that if the DataSource implementation cannot 	determine the type of the data, it use the convention of	returning "application/octet-stream".		@return MIME Type	*/	public String getContentType(File file)		{		InputStream in = null;		ContentTypeDetector d = getCTD();		String contentType = ContentTypeDetector.CONTENT_TYPE_UNKNOWN;		if (d == null)			{ return contentType; }		try	{			byte[] snippet = new byte[d.getMinimalDeterminableDataLength()];			in = new FileInputStream(file);			in.read(snippet);			contentType = d.getContentType(snippet);			}		catch (IOException e)			{ }		finally			{			try	{				if (in != null)					{ in.close(); }				}			catch (IOException e)				{}			}		return contentType;		}	/**	Return the base MIME Type of this data. This method is	expected to ALWAYS return a valid (non-null) MIME Type.	We suggest that if the DataSource implementation cannot 	determine the type of the data, it use the convention of	returning "application/octet-stream".		@return MIME Type	*/	public String getContentType(String filename)		{		return ContentTypeDetector.CONTENT_TYPE_UNKNOWN;		}	}