1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.activation;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.File;
  9. import java.io.FileDescriptor;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import com.sun.activation.registries.MimeTypeFile;
  15. /**
  16. * The FileDataSource class implements a simple DataSource object
  17. * that encapsulates a file. It provides data typing services via
  18. * a FileTypeMap object. <p>
  19. *
  20. * <b>FileDataSource Typing Semantics</b><p>
  21. *
  22. * The FileDataSource class delegates data typing of files
  23. * to an object subclassed from the FileTypeMap class.
  24. * The <code>setFileTypeMap</code> method can be used to explicitly
  25. * set the FileTypeMap for an instance of FileDataSource. If no
  26. * FileTypeMap is set, the FileDataSource will call the FileTypeMap's
  27. * getDefaultFileTypeMap method to get the System's default FileTypeMap.
  28. *
  29. * @see javax.activation.DataSource
  30. * @see javax.activation.FileTypeMap
  31. * @see javax.activation.MimetypesFileTypeMap
  32. */
  33. public class FileDataSource implements DataSource {
  34. // keep track of original 'ref' passed in, non-null
  35. // one indicated which was passed in:
  36. private File _file = null;
  37. private FileTypeMap typeMap = null;
  38. /**
  39. * Creates a FileDataSource from a File object. <i>Note:
  40. * The file will not actually be opened until a method is
  41. * called that requires the file to be opened.</i>
  42. *
  43. * @param file the file
  44. */
  45. public FileDataSource(File file) {
  46. _file = file; // save the file Object...
  47. }
  48. /**
  49. * Creates a FileDataSource from
  50. * the specified path name. <i>Note:
  51. * The file will not actually be opened until a method is
  52. * called that requires the file to be opened.</i>
  53. *
  54. * @param name the system-dependent file name.
  55. */
  56. public FileDataSource(String name) {
  57. this(new File(name)); // use the file constructor
  58. }
  59. /**
  60. * This method will return an InputStream representing the
  61. * the data and will throw an IOException if it can
  62. * not do so. This method will return a new
  63. * instance of InputStream with each invocation.
  64. *
  65. * @return an InputStream
  66. */
  67. public InputStream getInputStream() throws IOException {
  68. return new FileInputStream(_file);
  69. }
  70. /**
  71. * This method will return an OutputStream representing the
  72. * the data and will throw an IOException if it can
  73. * not do so. This method will return a new instance of
  74. * OutputStream with each invocation.
  75. *
  76. * @return an OutputStream
  77. */
  78. public OutputStream getOutputStream() throws IOException {
  79. return new FileOutputStream(_file);
  80. }
  81. /**
  82. * This method returns the MIME type of the data in the form of a
  83. * string. This method uses the currently installed FileTypeMap. If
  84. * there is no FileTypeMap explictly set, the FileDataSource will
  85. * call the <code>getDefaultFileTypeMap</code> method on
  86. * FileTypeMap to acquire a default FileTypeMap. <i>Note: By
  87. * default, the FileTypeMap used will be a MimetypesFileTypeMap.</i>
  88. *
  89. * @return the MIME Type
  90. * @see javax.activation.FileTypeMap#getDefaultFileTypeMap
  91. */
  92. public String getContentType() {
  93. // check to see if the type map is null?
  94. if (typeMap == null)
  95. return FileTypeMap.getDefaultFileTypeMap().getContentType(_file);
  96. else
  97. return typeMap.getContentType(_file);
  98. }
  99. /**
  100. * Return the <i>name</i> of this object. The FileDataSource
  101. * will return the file name of the object.
  102. *
  103. * @return the name of the object.
  104. * @see javax.activation.DataSource
  105. */
  106. public String getName() {
  107. return _file.getName();
  108. }
  109. /**
  110. * Return the File object that corresponds to this FileDataSource.
  111. * @return the File object for the file represented by this object.
  112. */
  113. public File getFile() {
  114. return _file;
  115. }
  116. /**
  117. * Set the FileTypeMap to use with this FileDataSource
  118. *
  119. * @param map The FileTypeMap for this object.
  120. */
  121. public void setFileTypeMap(FileTypeMap map) {
  122. typeMap = map;
  123. }
  124. }