KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > FileDataSource


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)FileDataSource.java 1.9 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.activation;
29
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileDescriptor JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.FileOutputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import com.sun.activation.registries.MimeTypeFile;
39
40 /**
41  * The FileDataSource class implements a simple DataSource object
42  * that encapsulates a file. It provides data typing services via
43  * a FileTypeMap object. <p>
44  *
45  * <b>FileDataSource Typing Semantics</b><p>
46  *
47  * The FileDataSource class delegates data typing of files
48  * to an object subclassed from the FileTypeMap class.
49  * The <code>setFileTypeMap</code> method can be used to explicitly
50  * set the FileTypeMap for an instance of FileDataSource. If no
51  * FileTypeMap is set, the FileDataSource will call the FileTypeMap's
52  * getDefaultFileTypeMap method to get the System's default FileTypeMap.
53  *
54  * @see javax.activation.DataSource
55  * @see javax.activation.FileTypeMap
56  * @see javax.activation.MimetypesFileTypeMap
57  */

58 public class FileDataSource implements DataSource JavaDoc {
59
60     // keep track of original 'ref' passed in, non-null
61
// one indicated which was passed in:
62
private File JavaDoc _file = null;
63     private FileTypeMap JavaDoc typeMap = null;
64
65     /**
66      * Creates a FileDataSource from a File object. <i>Note:
67      * The file will not actually be opened until a method is
68      * called that requires the file to be opened.</i>
69      *
70      * @param file the file
71      */

72     public FileDataSource(File JavaDoc file) {
73     _file = file; // save the file Object...
74
}
75
76     /**
77      * Creates a FileDataSource from
78      * the specified path name. <i>Note:
79      * The file will not actually be opened until a method is
80      * called that requires the file to be opened.</i>
81      *
82      * @param name the system-dependent file name.
83      */

84     public FileDataSource(String JavaDoc name) {
85     this(new File JavaDoc(name)); // use the file constructor
86
}
87
88     /**
89      * This method will return an InputStream representing the
90      * the data and will throw an IOException if it can
91      * not do so. This method will return a new
92      * instance of InputStream with each invocation.
93      *
94      * @return an InputStream
95      */

96     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
97     return new FileInputStream JavaDoc(_file);
98     }
99
100     /**
101      * This method will return an OutputStream representing the
102      * the data and will throw an IOException if it can
103      * not do so. This method will return a new instance of
104      * OutputStream with each invocation.
105      *
106      * @return an OutputStream
107      */

108     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
109     return new FileOutputStream JavaDoc(_file);
110     }
111
112     /**
113      * This method returns the MIME type of the data in the form of a
114      * string. This method uses the currently installed FileTypeMap. If
115      * there is no FileTypeMap explictly set, the FileDataSource will
116      * call the <code>getDefaultFileTypeMap</code> method on
117      * FileTypeMap to acquire a default FileTypeMap. <i>Note: By
118      * default, the FileTypeMap used will be a MimetypesFileTypeMap.</i>
119      *
120      * @return the MIME Type
121      * @see javax.activation.FileTypeMap#getDefaultFileTypeMap
122      */

123     public String JavaDoc getContentType() {
124     // check to see if the type map is null?
125
if (typeMap == null)
126         return FileTypeMap.getDefaultFileTypeMap().getContentType(_file);
127     else
128         return typeMap.getContentType(_file);
129     }
130
131     /**
132      * Return the <i>name</i> of this object. The FileDataSource
133      * will return the file name of the object.
134      *
135      * @return the name of the object.
136      * @see javax.activation.DataSource
137      */

138     public String JavaDoc getName() {
139     return _file.getName();
140     }
141
142     /**
143      * Return the File object that corresponds to this FileDataSource.
144      * @return the File object for the file represented by this object.
145      */

146     public File JavaDoc getFile() {
147     return _file;
148     }
149
150     /**
151      * Set the FileTypeMap to use with this FileDataSource
152      *
153      * @param map The FileTypeMap for this object.
154      */

155     public void setFileTypeMap(FileTypeMap JavaDoc map) {
156     typeMap = map;
157     }
158 }
159
Popular Tags