src/share/jaf_classes/javax/activation/FileDataSource.java

Mon, 08 Aug 2016 13:10:38 -0700

author
asaha
date
Mon, 08 Aug 2016 13:10:38 -0700
changeset 1267
f3d512b73229
parent 0
373ffda63c9a
permissions
-rw-r--r--

Merge

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

mercurial