12009
|
1 |
/*
|
|
2 |
* Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.activation;
|
|
27 |
|
|
28 |
import java.io.InputStream;
|
|
29 |
import java.io.OutputStream;
|
|
30 |
import java.io.File;
|
|
31 |
import java.io.FileDescriptor;
|
|
32 |
import java.io.FileNotFoundException;
|
|
33 |
import java.io.FileInputStream;
|
|
34 |
import java.io.FileOutputStream;
|
|
35 |
import java.io.IOException;
|
|
36 |
import com.sun.activation.registries.MimeTypeFile;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* The FileDataSource class implements a simple DataSource object
|
|
40 |
* that encapsulates a file. It provides data typing services via
|
|
41 |
* a FileTypeMap object. <p>
|
|
42 |
*
|
|
43 |
* <b>FileDataSource Typing Semantics</b><p>
|
|
44 |
*
|
|
45 |
* The FileDataSource class delegates data typing of files
|
|
46 |
* to an object subclassed from the FileTypeMap class.
|
|
47 |
* The <code>setFileTypeMap</code> method can be used to explicitly
|
|
48 |
* set the FileTypeMap for an instance of FileDataSource. If no
|
|
49 |
* FileTypeMap is set, the FileDataSource will call the FileTypeMap's
|
|
50 |
* getDefaultFileTypeMap method to get the System's default FileTypeMap.
|
|
51 |
*
|
|
52 |
* @see javax.activation.DataSource
|
|
53 |
* @see javax.activation.FileTypeMap
|
|
54 |
* @see javax.activation.MimetypesFileTypeMap
|
|
55 |
*
|
|
56 |
* @since 1.6
|
|
57 |
*/
|
|
58 |
public class FileDataSource implements DataSource {
|
|
59 |
|
|
60 |
// keep track of original 'ref' passed in, non-null
|
|
61 |
// one indicated which was passed in:
|
|
62 |
private File _file = null;
|
|
63 |
private FileTypeMap 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 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 name) {
|
|
85 |
this(new File(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 getInputStream() throws IOException {
|
|
97 |
return new FileInputStream(_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 getOutputStream() throws IOException {
|
|
109 |
return new FileOutputStream(_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 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 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 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 map) {
|
|
156 |
typeMap = map;
|
|
157 |
}
|
|
158 |
}
|