jaxws/src/share/classes/javax/activation/FileDataSource.java
author kvn
Tue, 03 Mar 2009 18:25:57 -0800
changeset 2147 9088094e3e81
parent 8 474761f14bca
permissions -rw-r--r--
6812721: Block's frequency should not be NaN Summary: Set MIN_BLOCK_FREQUENCY block's frequency when calculated block's frequency is NaN Reviewed-by: never
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
474761f14bca Initial load
duke
parents:
diff changeset
     1
/*
474761f14bca Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
474761f14bca Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
474761f14bca Initial load
duke
parents:
diff changeset
     4
 *
474761f14bca Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
474761f14bca Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
474761f14bca Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
474761f14bca Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
474761f14bca Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
474761f14bca Initial load
duke
parents:
diff changeset
    10
 *
474761f14bca Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
474761f14bca Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
474761f14bca Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
474761f14bca Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
474761f14bca Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
474761f14bca Initial load
duke
parents:
diff changeset
    16
 *
474761f14bca Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
474761f14bca Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
474761f14bca Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
474761f14bca Initial load
duke
parents:
diff changeset
    20
 *
474761f14bca Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
474761f14bca Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
474761f14bca Initial load
duke
parents:
diff changeset
    23
 * have any questions.
474761f14bca Initial load
duke
parents:
diff changeset
    24
 */
474761f14bca Initial load
duke
parents:
diff changeset
    25
474761f14bca Initial load
duke
parents:
diff changeset
    26
package javax.activation;
474761f14bca Initial load
duke
parents:
diff changeset
    27
474761f14bca Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    30
import java.io.File;
474761f14bca Initial load
duke
parents:
diff changeset
    31
import java.io.FileDescriptor;
474761f14bca Initial load
duke
parents:
diff changeset
    32
import java.io.FileNotFoundException;
474761f14bca Initial load
duke
parents:
diff changeset
    33
import java.io.FileInputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    34
import java.io.FileOutputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
474761f14bca Initial load
duke
parents:
diff changeset
    36
import com.sun.activation.registries.MimeTypeFile;
474761f14bca Initial load
duke
parents:
diff changeset
    37
474761f14bca Initial load
duke
parents:
diff changeset
    38
/**
474761f14bca Initial load
duke
parents:
diff changeset
    39
 * The FileDataSource class implements a simple DataSource object
474761f14bca Initial load
duke
parents:
diff changeset
    40
 * that encapsulates a file. It provides data typing services via
474761f14bca Initial load
duke
parents:
diff changeset
    41
 * a FileTypeMap object. <p>
474761f14bca Initial load
duke
parents:
diff changeset
    42
 *
474761f14bca Initial load
duke
parents:
diff changeset
    43
 * <b>FileDataSource Typing Semantics</b><p>
474761f14bca Initial load
duke
parents:
diff changeset
    44
 *
474761f14bca Initial load
duke
parents:
diff changeset
    45
 * The FileDataSource class delegates data typing of files
474761f14bca Initial load
duke
parents:
diff changeset
    46
 * to an object subclassed from the FileTypeMap class.
474761f14bca Initial load
duke
parents:
diff changeset
    47
 * The <code>setFileTypeMap</code> method can be used to explicitly
474761f14bca Initial load
duke
parents:
diff changeset
    48
 * set the FileTypeMap for an instance of FileDataSource. If no
474761f14bca Initial load
duke
parents:
diff changeset
    49
 * FileTypeMap is set, the FileDataSource will call the FileTypeMap's
474761f14bca Initial load
duke
parents:
diff changeset
    50
 * getDefaultFileTypeMap method to get the System's default FileTypeMap.
474761f14bca Initial load
duke
parents:
diff changeset
    51
 *
474761f14bca Initial load
duke
parents:
diff changeset
    52
 * @see javax.activation.DataSource
474761f14bca Initial load
duke
parents:
diff changeset
    53
 * @see javax.activation.FileTypeMap
474761f14bca Initial load
duke
parents:
diff changeset
    54
 * @see javax.activation.MimetypesFileTypeMap
474761f14bca Initial load
duke
parents:
diff changeset
    55
 *
474761f14bca Initial load
duke
parents:
diff changeset
    56
 * @since 1.6
474761f14bca Initial load
duke
parents:
diff changeset
    57
 */
474761f14bca Initial load
duke
parents:
diff changeset
    58
public class FileDataSource implements DataSource {
474761f14bca Initial load
duke
parents:
diff changeset
    59
474761f14bca Initial load
duke
parents:
diff changeset
    60
    // keep track of original 'ref' passed in, non-null
474761f14bca Initial load
duke
parents:
diff changeset
    61
    // one indicated which was passed in:
474761f14bca Initial load
duke
parents:
diff changeset
    62
    private File _file = null;
474761f14bca Initial load
duke
parents:
diff changeset
    63
    private FileTypeMap typeMap = null;
474761f14bca Initial load
duke
parents:
diff changeset
    64
474761f14bca Initial load
duke
parents:
diff changeset
    65
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    66
     * Creates a FileDataSource from a File object. <i>Note:
474761f14bca Initial load
duke
parents:
diff changeset
    67
     * The file will not actually be opened until a method is
474761f14bca Initial load
duke
parents:
diff changeset
    68
     * called that requires the file to be opened.</i>
474761f14bca Initial load
duke
parents:
diff changeset
    69
     *
474761f14bca Initial load
duke
parents:
diff changeset
    70
     * @param file the file
474761f14bca Initial load
duke
parents:
diff changeset
    71
     */
474761f14bca Initial load
duke
parents:
diff changeset
    72
    public FileDataSource(File file) {
474761f14bca Initial load
duke
parents:
diff changeset
    73
        _file = file;   // save the file Object...
474761f14bca Initial load
duke
parents:
diff changeset
    74
    }
474761f14bca Initial load
duke
parents:
diff changeset
    75
474761f14bca Initial load
duke
parents:
diff changeset
    76
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    77
     * Creates a FileDataSource from
474761f14bca Initial load
duke
parents:
diff changeset
    78
     * the specified path name. <i>Note:
474761f14bca Initial load
duke
parents:
diff changeset
    79
     * The file will not actually be opened until a method is
474761f14bca Initial load
duke
parents:
diff changeset
    80
     * called that requires the file to be opened.</i>
474761f14bca Initial load
duke
parents:
diff changeset
    81
     *
474761f14bca Initial load
duke
parents:
diff changeset
    82
     * @param name the system-dependent file name.
474761f14bca Initial load
duke
parents:
diff changeset
    83
     */
474761f14bca Initial load
duke
parents:
diff changeset
    84
    public FileDataSource(String name) {
474761f14bca Initial load
duke
parents:
diff changeset
    85
        this(new File(name));   // use the file constructor
474761f14bca Initial load
duke
parents:
diff changeset
    86
    }
474761f14bca Initial load
duke
parents:
diff changeset
    87
474761f14bca Initial load
duke
parents:
diff changeset
    88
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    89
     * This method will return an InputStream representing the
474761f14bca Initial load
duke
parents:
diff changeset
    90
     * the data and will throw an IOException if it can
474761f14bca Initial load
duke
parents:
diff changeset
    91
     * not do so. This method will return a new
474761f14bca Initial load
duke
parents:
diff changeset
    92
     * instance of InputStream with each invocation.
474761f14bca Initial load
duke
parents:
diff changeset
    93
     *
474761f14bca Initial load
duke
parents:
diff changeset
    94
     * @return an InputStream
474761f14bca Initial load
duke
parents:
diff changeset
    95
     */
474761f14bca Initial load
duke
parents:
diff changeset
    96
    public InputStream getInputStream() throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
    97
        return new FileInputStream(_file);
474761f14bca Initial load
duke
parents:
diff changeset
    98
    }
474761f14bca Initial load
duke
parents:
diff changeset
    99
474761f14bca Initial load
duke
parents:
diff changeset
   100
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   101
     * This method will return an OutputStream representing the
474761f14bca Initial load
duke
parents:
diff changeset
   102
     * the data and will throw an IOException if it can
474761f14bca Initial load
duke
parents:
diff changeset
   103
     * not do so. This method will return a new instance of
474761f14bca Initial load
duke
parents:
diff changeset
   104
     * OutputStream with each invocation.
474761f14bca Initial load
duke
parents:
diff changeset
   105
     *
474761f14bca Initial load
duke
parents:
diff changeset
   106
     * @return an OutputStream
474761f14bca Initial load
duke
parents:
diff changeset
   107
     */
474761f14bca Initial load
duke
parents:
diff changeset
   108
    public OutputStream getOutputStream() throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   109
        return new FileOutputStream(_file);
474761f14bca Initial load
duke
parents:
diff changeset
   110
    }
474761f14bca Initial load
duke
parents:
diff changeset
   111
474761f14bca Initial load
duke
parents:
diff changeset
   112
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   113
     * This method returns the MIME type of the data in the form of a
474761f14bca Initial load
duke
parents:
diff changeset
   114
     * string. This method uses the currently installed FileTypeMap. If
474761f14bca Initial load
duke
parents:
diff changeset
   115
     * there is no FileTypeMap explictly set, the FileDataSource will
474761f14bca Initial load
duke
parents:
diff changeset
   116
     * call the <code>getDefaultFileTypeMap</code> method on
474761f14bca Initial load
duke
parents:
diff changeset
   117
     * FileTypeMap to acquire a default FileTypeMap. <i>Note: By
474761f14bca Initial load
duke
parents:
diff changeset
   118
     * default, the FileTypeMap used will be a MimetypesFileTypeMap.</i>
474761f14bca Initial load
duke
parents:
diff changeset
   119
     *
474761f14bca Initial load
duke
parents:
diff changeset
   120
     * @return the MIME Type
474761f14bca Initial load
duke
parents:
diff changeset
   121
     * @see javax.activation.FileTypeMap#getDefaultFileTypeMap
474761f14bca Initial load
duke
parents:
diff changeset
   122
     */
474761f14bca Initial load
duke
parents:
diff changeset
   123
    public String getContentType() {
474761f14bca Initial load
duke
parents:
diff changeset
   124
        // check to see if the type map is null?
474761f14bca Initial load
duke
parents:
diff changeset
   125
        if (typeMap == null)
474761f14bca Initial load
duke
parents:
diff changeset
   126
            return FileTypeMap.getDefaultFileTypeMap().getContentType(_file);
474761f14bca Initial load
duke
parents:
diff changeset
   127
        else
474761f14bca Initial load
duke
parents:
diff changeset
   128
            return typeMap.getContentType(_file);
474761f14bca Initial load
duke
parents:
diff changeset
   129
    }
474761f14bca Initial load
duke
parents:
diff changeset
   130
474761f14bca Initial load
duke
parents:
diff changeset
   131
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   132
     * Return the <i>name</i> of this object. The FileDataSource
474761f14bca Initial load
duke
parents:
diff changeset
   133
     * will return the file name of the object.
474761f14bca Initial load
duke
parents:
diff changeset
   134
     *
474761f14bca Initial load
duke
parents:
diff changeset
   135
     * @return the name of the object.
474761f14bca Initial load
duke
parents:
diff changeset
   136
     * @see javax.activation.DataSource
474761f14bca Initial load
duke
parents:
diff changeset
   137
     */
474761f14bca Initial load
duke
parents:
diff changeset
   138
    public String getName() {
474761f14bca Initial load
duke
parents:
diff changeset
   139
        return _file.getName();
474761f14bca Initial load
duke
parents:
diff changeset
   140
    }
474761f14bca Initial load
duke
parents:
diff changeset
   141
474761f14bca Initial load
duke
parents:
diff changeset
   142
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   143
     * Return the File object that corresponds to this FileDataSource.
474761f14bca Initial load
duke
parents:
diff changeset
   144
     * @return the File object for the file represented by this object.
474761f14bca Initial load
duke
parents:
diff changeset
   145
     */
474761f14bca Initial load
duke
parents:
diff changeset
   146
    public File getFile() {
474761f14bca Initial load
duke
parents:
diff changeset
   147
        return _file;
474761f14bca Initial load
duke
parents:
diff changeset
   148
    }
474761f14bca Initial load
duke
parents:
diff changeset
   149
474761f14bca Initial load
duke
parents:
diff changeset
   150
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   151
     * Set the FileTypeMap to use with this FileDataSource
474761f14bca Initial load
duke
parents:
diff changeset
   152
     *
474761f14bca Initial load
duke
parents:
diff changeset
   153
     * @param map The FileTypeMap for this object.
474761f14bca Initial load
duke
parents:
diff changeset
   154
     */
474761f14bca Initial load
duke
parents:
diff changeset
   155
    public void setFileTypeMap(FileTypeMap map) {
474761f14bca Initial load
duke
parents:
diff changeset
   156
        typeMap = map;
474761f14bca Initial load
duke
parents:
diff changeset
   157
    }
474761f14bca Initial load
duke
parents:
diff changeset
   158
}