1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.activation;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26
27 /**
28 * @version $Rev: 123383 $ $Date: 2004-12-26 19:11:00 -0800 (Sun, 26 Dec 2004) $
29 */
30 public class FileDataSource implements DataSource {
31 private final File file;
32 private FileTypeMap fileTypeMap;
33
34 /**
35 * Creates a FileDataSource from a File object
36 */
37 public FileDataSource(File file) {
38 this.file = file;
39 }
40
41 /**
42 * Creates a FileDataSource from the specified path name
43 */
44 public FileDataSource(String name) {
45 this(new File(name));
46 }
47
48 /**
49 * Return the InputStream obtained from the data source
50 */
51 public InputStream getInputStream() throws IOException {
52 return new FileInputStream(file);
53 }
54
55 /**
56 * Return the OutputStream obtained from the data source
57 */
58 public OutputStream getOutputStream() throws IOException {
59 return new FileOutputStream(file);
60 }
61
62 /**
63 * Returns the content type of the data source
64 */
65 public String getContentType() {
66 if (fileTypeMap == null) {
67 return FileTypeMap.getDefaultFileTypeMap().getContentType(file);
68 } else {
69 return fileTypeMap.getContentType(file);
70 }
71 }
72
73 /**
74 * Returns the name of the data source object
75 */
76 public String getName() {
77 return file.getName();
78 }
79
80 /**
81 * Returns the data source file
82 */
83 public File getFile() {
84 return file;
85 }
86
87 /**
88 * Sets the FileTypeMap associated with the data source
89 */
90 public void setFileTypeMap(FileTypeMap map) {
91 fileTypeMap = map;
92 }
93 }