Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaf_classes/javax/activation/FileDataSource.java
38877 views
/*1* Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.activation;2627import java.io.InputStream;28import java.io.OutputStream;29import java.io.File;30import java.io.FileDescriptor;31import java.io.FileNotFoundException;32import java.io.FileInputStream;33import java.io.FileOutputStream;34import java.io.IOException;35import com.sun.activation.registries.MimeTypeFile;3637/**38* The FileDataSource class implements a simple DataSource object39* that encapsulates a file. It provides data typing services via40* a FileTypeMap object. <p>41*42* <b>FileDataSource Typing Semantics</b><p>43*44* The FileDataSource class delegates data typing of files45* to an object subclassed from the FileTypeMap class.46* The <code>setFileTypeMap</code> method can be used to explicitly47* set the FileTypeMap for an instance of FileDataSource. If no48* FileTypeMap is set, the FileDataSource will call the FileTypeMap's49* getDefaultFileTypeMap method to get the System's default FileTypeMap.50*51* @see javax.activation.DataSource52* @see javax.activation.FileTypeMap53* @see javax.activation.MimetypesFileTypeMap54*55* @since 1.656*/57public class FileDataSource implements DataSource {5859// keep track of original 'ref' passed in, non-null60// one indicated which was passed in:61private File _file = null;62private FileTypeMap typeMap = null;6364/**65* Creates a FileDataSource from a File object. <i>Note:66* The file will not actually be opened until a method is67* called that requires the file to be opened.</i>68*69* @param file the file70*/71public FileDataSource(File file) {72_file = file; // save the file Object...73}7475/**76* Creates a FileDataSource from77* the specified path name. <i>Note:78* The file will not actually be opened until a method is79* called that requires the file to be opened.</i>80*81* @param name the system-dependent file name.82*/83public FileDataSource(String name) {84this(new File(name)); // use the file constructor85}8687/**88* This method will return an InputStream representing the89* the data and will throw an IOException if it can90* not do so. This method will return a new91* instance of InputStream with each invocation.92*93* @return an InputStream94*/95public InputStream getInputStream() throws IOException {96return new FileInputStream(_file);97}9899/**100* This method will return an OutputStream representing the101* the data and will throw an IOException if it can102* not do so. This method will return a new instance of103* OutputStream with each invocation.104*105* @return an OutputStream106*/107public OutputStream getOutputStream() throws IOException {108return new FileOutputStream(_file);109}110111/**112* This method returns the MIME type of the data in the form of a113* string. This method uses the currently installed FileTypeMap. If114* there is no FileTypeMap explictly set, the FileDataSource will115* call the <code>getDefaultFileTypeMap</code> method on116* FileTypeMap to acquire a default FileTypeMap. <i>Note: By117* default, the FileTypeMap used will be a MimetypesFileTypeMap.</i>118*119* @return the MIME Type120* @see javax.activation.FileTypeMap#getDefaultFileTypeMap121*/122public String getContentType() {123// check to see if the type map is null?124if (typeMap == null)125return FileTypeMap.getDefaultFileTypeMap().getContentType(_file);126else127return typeMap.getContentType(_file);128}129130/**131* Return the <i>name</i> of this object. The FileDataSource132* will return the file name of the object.133*134* @return the name of the object.135* @see javax.activation.DataSource136*/137public String getName() {138return _file.getName();139}140141/**142* Return the File object that corresponds to this FileDataSource.143* @return the File object for the file represented by this object.144*/145public File getFile() {146return _file;147}148149/**150* Set the FileTypeMap to use with this FileDataSource151*152* @param map The FileTypeMap for this object.153*/154public void setFileTypeMap(FileTypeMap map) {155typeMap = map;156}157}158159160