aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.activation; aoqi@0: aoqi@0: import java.io.InputStream; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.File; aoqi@0: import java.io.FileDescriptor; aoqi@0: import java.io.FileNotFoundException; aoqi@0: import java.io.FileInputStream; aoqi@0: import java.io.FileOutputStream; aoqi@0: import java.io.IOException; aoqi@0: import com.sun.activation.registries.MimeTypeFile; aoqi@0: aoqi@0: /** aoqi@0: * The FileDataSource class implements a simple DataSource object aoqi@0: * that encapsulates a file. It provides data typing services via aoqi@0: * a FileTypeMap object.
aoqi@0: * aoqi@0: * FileDataSource Typing Semantics
aoqi@0: *
aoqi@0: * The FileDataSource class delegates data typing of files
aoqi@0: * to an object subclassed from the FileTypeMap class.
aoqi@0: * The setFileTypeMap
method can be used to explicitly
aoqi@0: * set the FileTypeMap for an instance of FileDataSource. If no
aoqi@0: * FileTypeMap is set, the FileDataSource will call the FileTypeMap's
aoqi@0: * getDefaultFileTypeMap method to get the System's default FileTypeMap.
aoqi@0: *
aoqi@0: * @see javax.activation.DataSource
aoqi@0: * @see javax.activation.FileTypeMap
aoqi@0: * @see javax.activation.MimetypesFileTypeMap
aoqi@0: *
aoqi@0: * @since 1.6
aoqi@0: */
aoqi@0: public class FileDataSource implements DataSource {
aoqi@0:
aoqi@0: // keep track of original 'ref' passed in, non-null
aoqi@0: // one indicated which was passed in:
aoqi@0: private File _file = null;
aoqi@0: private FileTypeMap typeMap = null;
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates a FileDataSource from a File object. Note:
aoqi@0: * The file will not actually be opened until a method is
aoqi@0: * called that requires the file to be opened.
aoqi@0: *
aoqi@0: * @param file the file
aoqi@0: */
aoqi@0: public FileDataSource(File file) {
aoqi@0: _file = file; // save the file Object...
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates a FileDataSource from
aoqi@0: * the specified path name. Note:
aoqi@0: * The file will not actually be opened until a method is
aoqi@0: * called that requires the file to be opened.
aoqi@0: *
aoqi@0: * @param name the system-dependent file name.
aoqi@0: */
aoqi@0: public FileDataSource(String name) {
aoqi@0: this(new File(name)); // use the file constructor
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * This method will return an InputStream representing the
aoqi@0: * the data and will throw an IOException if it can
aoqi@0: * not do so. This method will return a new
aoqi@0: * instance of InputStream with each invocation.
aoqi@0: *
aoqi@0: * @return an InputStream
aoqi@0: */
aoqi@0: public InputStream getInputStream() throws IOException {
aoqi@0: return new FileInputStream(_file);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * This method will return an OutputStream representing the
aoqi@0: * the data and will throw an IOException if it can
aoqi@0: * not do so. This method will return a new instance of
aoqi@0: * OutputStream with each invocation.
aoqi@0: *
aoqi@0: * @return an OutputStream
aoqi@0: */
aoqi@0: public OutputStream getOutputStream() throws IOException {
aoqi@0: return new FileOutputStream(_file);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * This method returns the MIME type of the data in the form of a
aoqi@0: * string. This method uses the currently installed FileTypeMap. If
aoqi@0: * there is no FileTypeMap explictly set, the FileDataSource will
aoqi@0: * call the getDefaultFileTypeMap
method on
aoqi@0: * FileTypeMap to acquire a default FileTypeMap. Note: By
aoqi@0: * default, the FileTypeMap used will be a MimetypesFileTypeMap.
aoqi@0: *
aoqi@0: * @return the MIME Type
aoqi@0: * @see javax.activation.FileTypeMap#getDefaultFileTypeMap
aoqi@0: */
aoqi@0: public String getContentType() {
aoqi@0: // check to see if the type map is null?
aoqi@0: if (typeMap == null)
aoqi@0: return FileTypeMap.getDefaultFileTypeMap().getContentType(_file);
aoqi@0: else
aoqi@0: return typeMap.getContentType(_file);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the name of this object. The FileDataSource
aoqi@0: * will return the file name of the object.
aoqi@0: *
aoqi@0: * @return the name of the object.
aoqi@0: * @see javax.activation.DataSource
aoqi@0: */
aoqi@0: public String getName() {
aoqi@0: return _file.getName();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the File object that corresponds to this FileDataSource.
aoqi@0: * @return the File object for the file represented by this object.
aoqi@0: */
aoqi@0: public File getFile() {
aoqi@0: return _file;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the FileTypeMap to use with this FileDataSource
aoqi@0: *
aoqi@0: * @param map The FileTypeMap for this object.
aoqi@0: */
aoqi@0: public void setFileTypeMap(FileTypeMap map) {
aoqi@0: typeMap = map;
aoqi@0: }
aoqi@0: }