1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaf_classes/javax/activation/FileDataSource.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,158 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.activation; 1.30 + 1.31 +import java.io.InputStream; 1.32 +import java.io.OutputStream; 1.33 +import java.io.File; 1.34 +import java.io.FileDescriptor; 1.35 +import java.io.FileNotFoundException; 1.36 +import java.io.FileInputStream; 1.37 +import java.io.FileOutputStream; 1.38 +import java.io.IOException; 1.39 +import com.sun.activation.registries.MimeTypeFile; 1.40 + 1.41 +/** 1.42 + * The FileDataSource class implements a simple DataSource object 1.43 + * that encapsulates a file. It provides data typing services via 1.44 + * a FileTypeMap object. <p> 1.45 + * 1.46 + * <b>FileDataSource Typing Semantics</b><p> 1.47 + * 1.48 + * The FileDataSource class delegates data typing of files 1.49 + * to an object subclassed from the FileTypeMap class. 1.50 + * The <code>setFileTypeMap</code> method can be used to explicitly 1.51 + * set the FileTypeMap for an instance of FileDataSource. If no 1.52 + * FileTypeMap is set, the FileDataSource will call the FileTypeMap's 1.53 + * getDefaultFileTypeMap method to get the System's default FileTypeMap. 1.54 + * 1.55 + * @see javax.activation.DataSource 1.56 + * @see javax.activation.FileTypeMap 1.57 + * @see javax.activation.MimetypesFileTypeMap 1.58 + * 1.59 + * @since 1.6 1.60 + */ 1.61 +public class FileDataSource implements DataSource { 1.62 + 1.63 + // keep track of original 'ref' passed in, non-null 1.64 + // one indicated which was passed in: 1.65 + private File _file = null; 1.66 + private FileTypeMap typeMap = null; 1.67 + 1.68 + /** 1.69 + * Creates a FileDataSource from a File object. <i>Note: 1.70 + * The file will not actually be opened until a method is 1.71 + * called that requires the file to be opened.</i> 1.72 + * 1.73 + * @param file the file 1.74 + */ 1.75 + public FileDataSource(File file) { 1.76 + _file = file; // save the file Object... 1.77 + } 1.78 + 1.79 + /** 1.80 + * Creates a FileDataSource from 1.81 + * the specified path name. <i>Note: 1.82 + * The file will not actually be opened until a method is 1.83 + * called that requires the file to be opened.</i> 1.84 + * 1.85 + * @param name the system-dependent file name. 1.86 + */ 1.87 + public FileDataSource(String name) { 1.88 + this(new File(name)); // use the file constructor 1.89 + } 1.90 + 1.91 + /** 1.92 + * This method will return an InputStream representing the 1.93 + * the data and will throw an IOException if it can 1.94 + * not do so. This method will return a new 1.95 + * instance of InputStream with each invocation. 1.96 + * 1.97 + * @return an InputStream 1.98 + */ 1.99 + public InputStream getInputStream() throws IOException { 1.100 + return new FileInputStream(_file); 1.101 + } 1.102 + 1.103 + /** 1.104 + * This method will return an OutputStream representing the 1.105 + * the data and will throw an IOException if it can 1.106 + * not do so. This method will return a new instance of 1.107 + * OutputStream with each invocation. 1.108 + * 1.109 + * @return an OutputStream 1.110 + */ 1.111 + public OutputStream getOutputStream() throws IOException { 1.112 + return new FileOutputStream(_file); 1.113 + } 1.114 + 1.115 + /** 1.116 + * This method returns the MIME type of the data in the form of a 1.117 + * string. This method uses the currently installed FileTypeMap. If 1.118 + * there is no FileTypeMap explictly set, the FileDataSource will 1.119 + * call the <code>getDefaultFileTypeMap</code> method on 1.120 + * FileTypeMap to acquire a default FileTypeMap. <i>Note: By 1.121 + * default, the FileTypeMap used will be a MimetypesFileTypeMap.</i> 1.122 + * 1.123 + * @return the MIME Type 1.124 + * @see javax.activation.FileTypeMap#getDefaultFileTypeMap 1.125 + */ 1.126 + public String getContentType() { 1.127 + // check to see if the type map is null? 1.128 + if (typeMap == null) 1.129 + return FileTypeMap.getDefaultFileTypeMap().getContentType(_file); 1.130 + else 1.131 + return typeMap.getContentType(_file); 1.132 + } 1.133 + 1.134 + /** 1.135 + * Return the <i>name</i> of this object. The FileDataSource 1.136 + * will return the file name of the object. 1.137 + * 1.138 + * @return the name of the object. 1.139 + * @see javax.activation.DataSource 1.140 + */ 1.141 + public String getName() { 1.142 + return _file.getName(); 1.143 + } 1.144 + 1.145 + /** 1.146 + * Return the File object that corresponds to this FileDataSource. 1.147 + * @return the File object for the file represented by this object. 1.148 + */ 1.149 + public File getFile() { 1.150 + return _file; 1.151 + } 1.152 + 1.153 + /** 1.154 + * Set the FileTypeMap to use with this FileDataSource 1.155 + * 1.156 + * @param map The FileTypeMap for this object. 1.157 + */ 1.158 + public void setFileTypeMap(FileTypeMap map) { 1.159 + typeMap = map; 1.160 + } 1.161 +}