J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2001 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.imageio.spi; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import javax.imageio.ImageReader; |
| 30 | import javax.imageio.stream.ImageInputStream; |
| 31 | |
| 32 | /** |
| 33 | * The service provider interface (SPI) for <code>ImageReader</code>s. |
| 34 | * For more information on service provider classes, see the class comment |
| 35 | * for the <code>IIORegistry</code> class. |
| 36 | * |
| 37 | * <p> Each <code>ImageReaderSpi</code> provides several types of information |
| 38 | * about the <code>ImageReader</code> class with which it is associated. |
| 39 | * |
| 40 | * <p> The name of the vendor who defined the SPI class and a |
| 41 | * brief description of the class are available via the |
| 42 | * <code>getVendorName</code>, <code>getDescription</code>, |
| 43 | * and <code>getVersion</code> methods. |
| 44 | * These methods may be internationalized to provide locale-specific |
| 45 | * output. These methods are intended mainly to provide short, |
| 46 | * human-readable information that might be used to organize a pop-up |
| 47 | * menu or other list. |
| 48 | * |
| 49 | * <p> Lists of format names, file suffixes, and MIME types associated |
| 50 | * with the service may be obtained by means of the |
| 51 | * <code>getFormatNames</code>, <code>getFileSuffixes</code>, and |
| 52 | * <code>getMIMETypes</code> methods. These methods may be used to |
| 53 | * identify candidate <code>ImageReader</code>s for decoding a |
| 54 | * particular file or stream based on manual format selection, file |
| 55 | * naming, or MIME associations (for example, when accessing a file |
| 56 | * over HTTP or as an email attachment). |
| 57 | * |
| 58 | * <p> A more reliable way to determine which <code>ImageReader</code>s |
| 59 | * are likely to be able to parse a particular data stream is provided |
| 60 | * by the <code>canDecodeInput</code> method. This methods allows the |
| 61 | * service provider to inspect the actual stream contents. |
| 62 | * |
| 63 | * <p> Finally, an instance of the <code>ImageReader</code> class |
| 64 | * associated with this service provider may be obtained by calling |
| 65 | * the <code>createReaderInstance</code> method. Any heavyweight |
| 66 | * initialization, such as the loading of native libraries or creation |
| 67 | * of large tables, should be deferred at least until the first |
| 68 | * invocation of this method. |
| 69 | * |
| 70 | * @see IIORegistry |
| 71 | * @see javax.imageio.ImageReader |
| 72 | * |
| 73 | */ |
| 74 | public abstract class ImageReaderSpi extends ImageReaderWriterSpi { |
| 75 | |
| 76 | /** |
| 77 | * A single-element array, initially containing |
| 78 | * <code>ImageInputStream.class</code>, to be returned from |
| 79 | * <code>getInputTypes</code>. |
| 80 | */ |
| 81 | public static final Class[] STANDARD_INPUT_TYPE = |
| 82 | { ImageInputStream.class }; |
| 83 | |
| 84 | /** |
| 85 | * An array of <code>Class</code> objects to be returned from |
| 86 | * <code>getInputTypes</code>, initially <code>null</code>. |
| 87 | */ |
| 88 | protected Class[] inputTypes = null; |
| 89 | |
| 90 | /** |
| 91 | * An array of strings to be returned from |
| 92 | * <code>getImageWriterSpiNames</code>, initially |
| 93 | * <code>null</code>. |
| 94 | */ |
| 95 | protected String[] writerSpiNames = null; |
| 96 | |
| 97 | /** |
| 98 | * The <code>Class</code> of the reader, initially |
| 99 | * <code>null</code>. |
| 100 | */ |
| 101 | private Class readerClass = null; |
| 102 | |
| 103 | /** |
| 104 | * Constructs a blank <code>ImageReaderSpi</code>. It is up to |
| 105 | * the subclass to initialize instance variables and/or override |
| 106 | * method implementations in order to provide working versions of |
| 107 | * all methods. |
| 108 | */ |
| 109 | protected ImageReaderSpi() { |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Constructs an <code>ImageReaderSpi</code> with a given |
| 114 | * set of values. |
| 115 | * |
| 116 | * @param vendorName the vendor name, as a non-<code>null</code> |
| 117 | * <code>String</code>. |
| 118 | * @param version a version identifier, as a non-<code>null</code> |
| 119 | * <code>String</code>. |
| 120 | * @param names a non-<code>null</code> array of |
| 121 | * <code>String</code>s indicating the format names. At least one |
| 122 | * entry must be present. |
| 123 | * @param suffixes an array of <code>String</code>s indicating the |
| 124 | * common file suffixes. If no suffixes are defined, |
| 125 | * <code>null</code> should be supplied. An array of length 0 |
| 126 | * will be normalized to <code>null</code>. |
| 127 | * @param MIMETypes an array of <code>String</code>s indicating |
| 128 | * the format's MIME types. If no MIME types are defined, |
| 129 | * <code>null</code> should be supplied. An array of length 0 |
| 130 | * will be normalized to <code>null</code>. |
| 131 | * @param readerClassName the fully-qualified name of the |
| 132 | * associated <code>ImageReader</code> class, as a |
| 133 | * non-<code>null</code> <code>String</code>. |
| 134 | * @param inputTypes a non-<code>null</code> array of |
| 135 | * <code>Class</code> objects of length at least 1 indicating the |
| 136 | * legal input types. |
| 137 | * @param writerSpiNames an array <code>String</code>s naming the |
| 138 | * classes of all associated <code>ImageWriter</code>s, or |
| 139 | * <code>null</code>. An array of length 0 is normalized to |
| 140 | * <code>null</code>. |
| 141 | * @param supportsStandardStreamMetadataFormat a |
| 142 | * <code>boolean</code> that indicates whether a stream metadata |
| 143 | * object can use trees described by the standard metadata format. |
| 144 | * @param nativeStreamMetadataFormatName a |
| 145 | * <code>String</code>, or <code>null</code>, to be returned from |
| 146 | * <code>getNativeStreamMetadataFormatName</code>. |
| 147 | * @param nativeStreamMetadataFormatClassName a |
| 148 | * <code>String</code>, or <code>null</code>, to be used to instantiate |
| 149 | * a metadata format object to be returned from |
| 150 | * <code>getNativeStreamMetadataFormat</code>. |
| 151 | * @param extraStreamMetadataFormatNames an array of |
| 152 | * <code>String</code>s, or <code>null</code>, to be returned from |
| 153 | * <code>getExtraStreamMetadataFormatNames</code>. An array of length |
| 154 | * 0 is normalized to <code>null</code>. |
| 155 | * @param extraStreamMetadataFormatClassNames an array of |
| 156 | * <code>String</code>s, or <code>null</code>, to be used to instantiate |
| 157 | * a metadata format object to be returned from |
| 158 | * <code>getStreamMetadataFormat</code>. An array of length |
| 159 | * 0 is normalized to <code>null</code>. |
| 160 | * @param supportsStandardImageMetadataFormat a |
| 161 | * <code>boolean</code> that indicates whether an image metadata |
| 162 | * object can use trees described by the standard metadata format. |
| 163 | * @param nativeImageMetadataFormatName a |
| 164 | * <code>String</code>, or <code>null</code>, to be returned from |
| 165 | * <code>getNativeImageMetadataFormatName</code>. |
| 166 | * @param nativeImageMetadataFormatClassName a |
| 167 | * <code>String</code>, or <code>null</code>, to be used to instantiate |
| 168 | * a metadata format object to be returned from |
| 169 | * <code>getNativeImageMetadataFormat</code>. |
| 170 | * @param extraImageMetadataFormatNames an array of |
| 171 | * <code>String</code>s to be returned from |
| 172 | * <code>getExtraImageMetadataFormatNames</code>. An array of length 0 |
| 173 | * is normalized to <code>null</code>. |
| 174 | * @param extraImageMetadataFormatClassNames an array of |
| 175 | * <code>String</code>s, or <code>null</code>, to be used to instantiate |
| 176 | * a metadata format object to be returned from |
| 177 | * <code>getImageMetadataFormat</code>. An array of length |
| 178 | * 0 is normalized to <code>null</code>. |
| 179 | * |
| 180 | * @exception IllegalArgumentException if <code>vendorName</code> |
| 181 | * is <code>null</code>. |
| 182 | * @exception IllegalArgumentException if <code>version</code> |
| 183 | * is <code>null</code>. |
| 184 | * @exception IllegalArgumentException if <code>names</code> |
| 185 | * is <code>null</code> or has length 0. |
| 186 | * @exception IllegalArgumentException if <code>readerClassName</code> |
| 187 | * is <code>null</code>. |
| 188 | * @exception IllegalArgumentException if <code>inputTypes</code> |
| 189 | * is <code>null</code> or has length 0. |
| 190 | */ |
| 191 | public ImageReaderSpi(String vendorName, |
| 192 | String version, |
| 193 | String[] names, |
| 194 | String[] suffixes, |
| 195 | String[] MIMETypes, |
| 196 | String readerClassName, |
| 197 | Class[] inputTypes, |
| 198 | String[] writerSpiNames, |
| 199 | boolean supportsStandardStreamMetadataFormat, |
| 200 | String nativeStreamMetadataFormatName, |
| 201 | String nativeStreamMetadataFormatClassName, |
| 202 | String[] extraStreamMetadataFormatNames, |
| 203 | String[] extraStreamMetadataFormatClassNames, |
| 204 | boolean supportsStandardImageMetadataFormat, |
| 205 | String nativeImageMetadataFormatName, |
| 206 | String nativeImageMetadataFormatClassName, |
| 207 | String[] extraImageMetadataFormatNames, |
| 208 | String[] extraImageMetadataFormatClassNames) { |
| 209 | super(vendorName, version, |
| 210 | names, suffixes, MIMETypes, readerClassName, |
| 211 | supportsStandardStreamMetadataFormat, |
| 212 | nativeStreamMetadataFormatName, |
| 213 | nativeStreamMetadataFormatClassName, |
| 214 | extraStreamMetadataFormatNames, |
| 215 | extraStreamMetadataFormatClassNames, |
| 216 | supportsStandardImageMetadataFormat, |
| 217 | nativeImageMetadataFormatName, |
| 218 | nativeImageMetadataFormatClassName, |
| 219 | extraImageMetadataFormatNames, |
| 220 | extraImageMetadataFormatClassNames); |
| 221 | |
| 222 | if (inputTypes == null) { |
| 223 | throw new IllegalArgumentException |
| 224 | ("inputTypes == null!"); |
| 225 | } |
| 226 | if (inputTypes.length == 0) { |
| 227 | throw new IllegalArgumentException |
| 228 | ("inputTypes.length == 0!"); |
| 229 | } |
| 230 | this.inputTypes = (Class[])inputTypes.clone(); |
| 231 | // If length == 0, leave it null |
| 232 | if (writerSpiNames != null && writerSpiNames.length > 0) { |
| 233 | this.writerSpiNames = (String[])writerSpiNames.clone(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns an array of <code>Class</code> objects indicating what |
| 239 | * types of objects may be used as arguments to the reader's |
| 240 | * <code>setInput</code> method. |
| 241 | * |
| 242 | * <p> For most readers, which only accept input from an |
| 243 | * <code>ImageInputStream</code>, a single-element array |
| 244 | * containing <code>ImageInputStream.class</code> should be |
| 245 | * returned. |
| 246 | * |
| 247 | * @return a non-<code>null</code> array of |
| 248 | * <code>Class</code>objects of length at least 1. |
| 249 | */ |
| 250 | public Class[] getInputTypes() { |
| 251 | return (Class[])inputTypes.clone(); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Returns <code>true</code> if the supplied source object appears |
| 256 | * to be of the format supported by this reader. Returning |
| 257 | * <code>true</code> from this method does not guarantee that |
| 258 | * reading will succeed, only that there appears to be a |
| 259 | * reasonable chance of success based on a brief inspection of the |
| 260 | * stream contents. If the source is an |
| 261 | * <code>ImageInputStream</code>, implementations will commonly |
| 262 | * check the first several bytes of the stream for a "magic |
| 263 | * number" associated with the format. Once actual reading has |
| 264 | * commenced, the reader may still indicate failure at any time |
| 265 | * prior to the completion of decoding. |
| 266 | * |
| 267 | * <p> It is important that the state of the object not be |
| 268 | * disturbed in order that other <code>ImageReaderSpi</code>s can |
| 269 | * properly determine whether they are able to decode the object. |
| 270 | * In particular, if the source is an |
| 271 | * <code>ImageInputStream</code>, a |
| 272 | * <code>mark</code>/<code>reset</code> pair should be used to |
| 273 | * preserve the stream position. |
| 274 | * |
| 275 | * <p> Formats such as "raw," which can potentially attempt |
| 276 | * to read nearly any stream, should return <code>false</code> |
| 277 | * in order to avoid being invoked in preference to a closer |
| 278 | * match. |
| 279 | * |
| 280 | * <p> If <code>source</code> is not an instance of one of the |
| 281 | * classes returned by <code>getInputTypes</code>, the method |
| 282 | * should simply return <code>false</code>. |
| 283 | * |
| 284 | * @param source the object (typically an |
| 285 | * <code>ImageInputStream</code>) to be decoded. |
| 286 | * |
| 287 | * @return <code>true</code> if it is likely that this stream can |
| 288 | * be decoded. |
| 289 | * |
| 290 | * @exception IllegalArgumentException if <code>source</code> is |
| 291 | * <code>null</code>. |
| 292 | * @exception IOException if an I/O error occurs while reading the |
| 293 | * stream. |
| 294 | */ |
| 295 | public abstract boolean canDecodeInput(Object source) throws IOException; |
| 296 | |
| 297 | /** |
| 298 | * Returns an instance of the <code>ImageReader</code> |
| 299 | * implementation associated with this service provider. |
| 300 | * The returned object will initially be in an initial state |
| 301 | * as if its <code>reset</code> method had been called. |
| 302 | * |
| 303 | * <p> The default implementation simply returns |
| 304 | * <code>createReaderInstance(null)</code>. |
| 305 | * |
| 306 | * @return an <code>ImageReader</code> instance. |
| 307 | * |
| 308 | * @exception IOException if an error occurs during loading, |
| 309 | * or initialization of the reader class, or during instantiation |
| 310 | * or initialization of the reader object. |
| 311 | */ |
| 312 | public ImageReader createReaderInstance() throws IOException { |
| 313 | return createReaderInstance(null); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Returns an instance of the <code>ImageReader</code> |
| 318 | * implementation associated with this service provider. |
| 319 | * The returned object will initially be in an initial state |
| 320 | * as if its <code>reset</code> method had been called. |
| 321 | * |
| 322 | * <p> An <code>Object</code> may be supplied to the plug-in at |
| 323 | * construction time. The nature of the object is entirely |
| 324 | * plug-in specific. |
| 325 | * |
| 326 | * <p> Typically, a plug-in will implement this method using code |
| 327 | * such as <code>return new MyImageReader(this)</code>. |
| 328 | * |
| 329 | * @param extension a plug-in specific extension object, which may |
| 330 | * be <code>null</code>. |
| 331 | * |
| 332 | * @return an <code>ImageReader</code> instance. |
| 333 | * |
| 334 | * @exception IOException if the attempt to instantiate |
| 335 | * the reader fails. |
| 336 | * @exception IllegalArgumentException if the |
| 337 | * <code>ImageReader</code>'s contructor throws an |
| 338 | * <code>IllegalArgumentException</code> to indicate that the |
| 339 | * extension object is unsuitable. |
| 340 | */ |
| 341 | public abstract ImageReader createReaderInstance(Object extension) |
| 342 | throws IOException; |
| 343 | |
| 344 | /** |
| 345 | * Returns <code>true</code> if the <code>ImageReader</code> object |
| 346 | * passed in is an instance of the <code>ImageReader</code> |
| 347 | * associated with this service provider. |
| 348 | * |
| 349 | * <p> The default implementation compares the fully-qualified |
| 350 | * class name of the <code>reader</code> argument with the class |
| 351 | * name passed into the constructor. This method may be overridden |
| 352 | * if more sophisticated checking is required. |
| 353 | * |
| 354 | * @param reader an <code>ImageReader</code> instance. |
| 355 | * |
| 356 | * @return <code>true</code> if <code>reader</code> is recognized. |
| 357 | * |
| 358 | * @exception IllegalArgumentException if <code>reader</code> is |
| 359 | * <code>null</code>. |
| 360 | */ |
| 361 | public boolean isOwnReader(ImageReader reader) { |
| 362 | if (reader == null) { |
| 363 | throw new IllegalArgumentException("reader == null!"); |
| 364 | } |
| 365 | String name = reader.getClass().getName(); |
| 366 | return name.equals(pluginClassName); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Returns an array of <code>String</code>s containing the fully |
| 371 | * qualified names of all the <code>ImageWriterSpi</code> classes |
| 372 | * that can understand the internal metadata representation used |
| 373 | * by the <code>ImageReader</code> associated with this service |
| 374 | * provider, or <code>null</code> if there are no such |
| 375 | * <code>ImageWriter</code>s specified. If a |
| 376 | * non-<code>null</code> value is returned, it must have non-zero |
| 377 | * length. |
| 378 | * |
| 379 | * <p> The first item in the array must be the name of the service |
| 380 | * provider for the "preferred" writer, as it will be used to |
| 381 | * instantiate the <code>ImageWriter</code> returned by |
| 382 | * <code>ImageIO.getImageWriter(ImageReader)</code>. |
| 383 | * |
| 384 | * <p> This mechanism may be used to obtain |
| 385 | * <code>ImageWriters</code> that will understand the internal |
| 386 | * structure of non-pixel meta-data (see |
| 387 | * <code>IIOTreeInfo</code>) generated by an |
| 388 | * <code>ImageReader</code>. By obtaining this data from the |
| 389 | * <code>ImageReader</code> and passing it on to one of the |
| 390 | * <code>ImageWriters</code> obtained with this method, a client |
| 391 | * program can read an image, modify it in some way, and write it |
| 392 | * back out while preserving all meta-data, without having to |
| 393 | * understand anything about the internal structure of the |
| 394 | * meta-data, or even about the image format. |
| 395 | * |
| 396 | * @return an array of <code>String</code>s of length at least 1 |
| 397 | * containing names of <code>ImageWriterSpi</code>, or |
| 398 | * <code>null</code>. |
| 399 | * |
| 400 | * @see javax.imageio.ImageIO#getImageWriter(ImageReader) |
| 401 | */ |
| 402 | public String[] getImageWriterSpiNames() { |
| 403 | return writerSpiNames == null ? |
| 404 | null : (String[])writerSpiNames.clone(); |
| 405 | } |
| 406 | } |