001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package javax.activation;
019
020 import java.io.File;
021 import java.io.FileInputStream;
022 import java.io.FileOutputStream;
023 import java.io.IOException;
024 import java.io.InputStream;
025 import java.io.OutputStream;
026
027 /**
028 * @version $Rev: 123383 $ $Date: 2004-12-26 19:11:00 -0800 (Sun, 26 Dec 2004) $
029 */
030 public class FileDataSource implements DataSource {
031 private final File file;
032 private FileTypeMap fileTypeMap;
033
034 /**
035 * Creates a FileDataSource from a File object
036 */
037 public FileDataSource(File file) {
038 this.file = file;
039 }
040
041 /**
042 * Creates a FileDataSource from the specified path name
043 */
044 public FileDataSource(String name) {
045 this(new File(name));
046 }
047
048 /**
049 * Return the InputStream obtained from the data source
050 */
051 public InputStream getInputStream() throws IOException {
052 return new FileInputStream(file);
053 }
054
055 /**
056 * Return the OutputStream obtained from the data source
057 */
058 public OutputStream getOutputStream() throws IOException {
059 return new FileOutputStream(file);
060 }
061
062 /**
063 * Returns the content type of the data source
064 */
065 public String getContentType() {
066 if (fileTypeMap == null) {
067 return FileTypeMap.getDefaultFileTypeMap().getContentType(file);
068 } else {
069 return fileTypeMap.getContentType(file);
070 }
071 }
072
073 /**
074 * Returns the name of the data source object
075 */
076 public String getName() {
077 return file.getName();
078 }
079
080 /**
081 * Returns the data source file
082 */
083 public File getFile() {
084 return file;
085 }
086
087 /**
088 * Sets the FileTypeMap associated with the data source
089 */
090 public void setFileTypeMap(FileTypeMap map) {
091 fileTypeMap = map;
092 }
093 }