1. /*
  2. * @(#)ImageInputStreamSpi.java 1.23 04/05/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.imageio.spi;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import javax.imageio.stream.ImageInputStream;
  11. /**
  12. * The service provider interface (SPI) for
  13. * <code>ImageInputStream</code>s. For more information on service
  14. * provider interfaces, see the class comment for the
  15. * <code>IIORegistry</code> class.
  16. *
  17. * <p> This interface allows arbitrary objects to be "wrapped" by
  18. * instances of <code>ImageInputStream</code>. For example,
  19. * a particular <code>ImageInputStreamSpi</code> might allow
  20. * a generic <code>InputStream</code> to be used as an input source;
  21. * another might take input from a <code>URL</code>.
  22. *
  23. * <p> By treating the creation of <code>ImageInputStream</code>s as a
  24. * pluggable service, it becomes possible to handle future input
  25. * sources without changing the API. Also, high-performance
  26. * implementations of <code>ImageInputStream</code> (for example,
  27. * native implementations for a particular platform) can be installed
  28. * and used transparently by applications.
  29. *
  30. * @see IIORegistry
  31. * @see javax.imageio.stream.ImageInputStream
  32. *
  33. * @version 0.5
  34. */
  35. public abstract class ImageInputStreamSpi extends IIOServiceProvider {
  36. /**
  37. * A <code>Class</code> object indicating the legal object type
  38. * for use by the <code>createInputStreamInstance</code> method.
  39. */
  40. protected Class<?> inputClass;
  41. /**
  42. * Constructs a blank <code>ImageInputStreamSpi</code>. It is up
  43. * to the subclass to initialize instance variables and/or
  44. * override method implementations in order to provide working
  45. * versions of all methods.
  46. */
  47. protected ImageInputStreamSpi() {
  48. }
  49. /**
  50. * Constructs an <code>ImageInputStreamSpi</code> with a given set
  51. * of values.
  52. *
  53. * @param vendorName the vendor name.
  54. * @param version a version identifier.
  55. * @param inputClass a <code>Class</code> object indicating the
  56. * legal object type for use by the
  57. * <code>createInputStreamInstance</code> method.
  58. *
  59. * @exception IllegalArgumentException if <code>vendorName</code>
  60. * is <code>null</code>.
  61. * @exception IllegalArgumentException if <code>version</code>
  62. * is <code>null</code>.
  63. */
  64. public ImageInputStreamSpi(String vendorName,
  65. String version,
  66. Class<?> inputClass) {
  67. super(vendorName, version);
  68. this.inputClass = inputClass;
  69. }
  70. /**
  71. * Returns a <code>Class</code> object representing the class or
  72. * interface type that must be implemented by an input source in
  73. * order to be "wrapped" in an <code>ImageInputStream</code> via
  74. * the <code>createInputStreamInstance</code> method.
  75. *
  76. * <p> Typical return values might include
  77. * <code>InputStream.class</code> or <code>URL.class</code>, but
  78. * any class may be used.
  79. *
  80. * @return a <code>Class</code> variable.
  81. *
  82. * @see #createInputStreamInstance(Object, boolean, File)
  83. */
  84. public Class<?> getInputClass() {
  85. return inputClass;
  86. }
  87. /**
  88. * Returns <code>true</code> if the <code>ImageInputStream</code>
  89. * implementation associated with this service provider can
  90. * optionally make use of a cache file for improved performance
  91. * and/or memory footrprint. If <code>false</code>, the value of
  92. * the <code>useCache</code> argument to
  93. * <code>createInputStreamInstance</code> will be ignored.
  94. *
  95. * <p> The default implementation returns <code>false</code>.
  96. *
  97. * @return <code>true</code> if a cache file can be used by the
  98. * input streams created by this service provider.
  99. */
  100. public boolean canUseCacheFile() {
  101. return false;
  102. }
  103. /**
  104. * Returns <code>true</code> if the <code>ImageInputStream</code>
  105. * implementation associated with this service provider requires
  106. * the use of a cache <code>File</code>. If <code>true</code>,
  107. * the value of the <code>useCache</code> argument to
  108. * <code>createInputStreamInstance</code> will be ignored.
  109. *
  110. * <p> The default implementation returns <code>false</code>.
  111. *
  112. * @return <code>true</code> if a cache file is needed by the
  113. * input streams created by this service provider.
  114. */
  115. public boolean needsCacheFile() {
  116. return false;
  117. }
  118. /**
  119. * Returns an instance of the <code>ImageInputStream</code>
  120. * implementation associated with this service provider. If the
  121. * use of a cache file is optional, the <code>useCache</code>
  122. * parameter will be consulted. Where a cache is required, or
  123. * not applicable, the value of <code>useCache</code> will be ignored.
  124. *
  125. * @param input an object of the class type returned by
  126. * <code>getInputClass</code>.
  127. * @param useCache a <code>boolean</code> indicating whether a
  128. * cache file should be used, in cases where it is optional.
  129. * @param cacheDir a <code>File</code> indicating where the
  130. * cache file should be created, or <code>null</code> to use the
  131. * system directory.
  132. *
  133. * @return an <code>ImageInputStream</code> instance.
  134. *
  135. * @exception IllegalArgumentException if <code>input</code> is
  136. * not an instance of the correct class or is <code>null</code>.
  137. * @exception IllegalArgumentException if a cache file is needed
  138. * but <code>cacheDir</code> is non-<code>null</code> and is not a
  139. * directory.
  140. * @exception IOException if a cache file is needed but cannot be
  141. * created.
  142. *
  143. * @see #getInputClass
  144. * @see #canUseCacheFile
  145. * @see #needsCacheFile
  146. */
  147. public abstract ImageInputStream
  148. createInputStreamInstance(Object input,
  149. boolean useCache,
  150. File cacheDir) throws IOException;
  151. /**
  152. * Returns an instance of the <code>ImageInputStream</code>
  153. * implementation associated with this service provider. A cache
  154. * file will be created in the system-dependent default
  155. * temporary-file directory, if needed.
  156. *
  157. * @param input an object of the class type returned by
  158. * <code>getInputClass</code>.
  159. *
  160. * @return an <code>ImageInputStream</code> instance.
  161. *
  162. * @exception IllegalArgumentException if <code>input</code> is
  163. * not an instance of the correct class or is <code>null</code>.
  164. * @exception IOException if a cache file is needed but cannot be
  165. * created.
  166. *
  167. * @see #getInputClass()
  168. */
  169. public ImageInputStream createInputStreamInstance(Object input)
  170. throws IOException {
  171. return createInputStreamInstance(input, true, null);
  172. }
  173. }