Path: blob/master/src/java.desktop/share/classes/javax/imageio/spi/ImageReaderSpi.java
41155 views
/*1* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.imageio.spi;2627import java.io.IOException;28import javax.imageio.ImageReader;29import javax.imageio.stream.ImageInputStream;3031/**32* The service provider interface (SPI) for {@code ImageReader}s.33* For more information on service provider classes, see the class comment34* for the {@code IIORegistry} class.35*36* <p> Each {@code ImageReaderSpi} provides several types of information37* about the {@code ImageReader} class with which it is associated.38*39* <p> The name of the vendor who defined the SPI class and a40* brief description of the class are available via the41* {@code getVendorName}, {@code getDescription},42* and {@code getVersion} methods.43* These methods may be internationalized to provide locale-specific44* output. These methods are intended mainly to provide short,45* human-readable information that might be used to organize a pop-up46* menu or other list.47*48* <p> Lists of format names, file suffixes, and MIME types associated49* with the service may be obtained by means of the50* {@code getFormatNames}, {@code getFileSuffixes}, and51* {@code getMIMETypes} methods. These methods may be used to52* identify candidate {@code ImageReader}s for decoding a53* particular file or stream based on manual format selection, file54* naming, or MIME associations (for example, when accessing a file55* over HTTP or as an email attachment).56*57* <p> A more reliable way to determine which {@code ImageReader}s58* are likely to be able to parse a particular data stream is provided59* by the {@code canDecodeInput} method. This methods allows the60* service provider to inspect the actual stream contents.61*62* <p> Finally, an instance of the {@code ImageReader} class63* associated with this service provider may be obtained by calling64* the {@code createReaderInstance} method. Any heavyweight65* initialization, such as the loading of native libraries or creation66* of large tables, should be deferred at least until the first67* invocation of this method.68*69* @see IIORegistry70* @see javax.imageio.ImageReader71*72*/73public abstract class ImageReaderSpi extends ImageReaderWriterSpi {7475/**76* A single-element array, initially containing77* {@code ImageInputStream.class}, to be returned from78* {@code getInputTypes}.79* @deprecated Instead of using this field, directly create80* the equivalent array {@code { ImageInputStream.class }}.81*/82@Deprecated83public static final Class<?>[] STANDARD_INPUT_TYPE =84{ ImageInputStream.class };8586/**87* An array of {@code Class} objects to be returned from88* {@code getInputTypes}, initially {@code null}.89*/90protected Class<?>[] inputTypes = null;9192/**93* An array of strings to be returned from94* {@code getImageWriterSpiNames}, initially95* {@code null}.96*/97protected String[] writerSpiNames = null;9899/**100* The {@code Class} of the reader, initially101* {@code null}.102*/103private Class<?> readerClass = null;104105/**106* Constructs a blank {@code ImageReaderSpi}. It is up to107* the subclass to initialize instance variables and/or override108* method implementations in order to provide working versions of109* all methods.110*/111protected ImageReaderSpi() {112}113114/**115* Constructs an {@code ImageReaderSpi} with a given116* set of values.117*118* @param vendorName the vendor name, as a non-{@code null}119* {@code String}.120* @param version a version identifier, as a non-{@code null}121* {@code String}.122* @param names a non-{@code null} array of123* {@code String}s indicating the format names. At least one124* entry must be present.125* @param suffixes an array of {@code String}s indicating the126* common file suffixes. If no suffixes are defined,127* {@code null} should be supplied. An array of length 0128* will be normalized to {@code null}.129* @param MIMETypes an array of {@code String}s indicating130* the format's MIME types. If no MIME types are defined,131* {@code null} should be supplied. An array of length 0132* will be normalized to {@code null}.133* @param readerClassName the fully-qualified name of the134* associated {@code ImageReader} class, as a135* non-{@code null String}.136* @param inputTypes a non-{@code null} array of137* {@code Class} objects of length at least 1 indicating the138* legal input types.139* @param writerSpiNames an array {@code String}s naming the140* classes of all associated {@code ImageWriter}s, or141* {@code null}. An array of length 0 is normalized to142* {@code null}.143* @param supportsStandardStreamMetadataFormat a144* {@code boolean} that indicates whether a stream metadata145* object can use trees described by the standard metadata format.146* @param nativeStreamMetadataFormatName a147* {@code String}, or {@code null}, to be returned from148* {@code getNativeStreamMetadataFormatName}.149* @param nativeStreamMetadataFormatClassName a150* {@code String}, or {@code null}, to be used to instantiate151* a metadata format object to be returned from152* {@code getNativeStreamMetadataFormat}.153* @param extraStreamMetadataFormatNames an array of154* {@code String}s, or {@code null}, to be returned from155* {@code getExtraStreamMetadataFormatNames}. An array of length156* 0 is normalized to {@code null}.157* @param extraStreamMetadataFormatClassNames an array of158* {@code String}s, or {@code null}, to be used to instantiate159* a metadata format object to be returned from160* {@code getStreamMetadataFormat}. An array of length161* 0 is normalized to {@code null}.162* @param supportsStandardImageMetadataFormat a163* {@code boolean} that indicates whether an image metadata164* object can use trees described by the standard metadata format.165* @param nativeImageMetadataFormatName a166* {@code String}, or {@code null}, to be returned from167* {@code getNativeImageMetadataFormatName}.168* @param nativeImageMetadataFormatClassName a169* {@code String}, or {@code null}, to be used to instantiate170* a metadata format object to be returned from171* {@code getNativeImageMetadataFormat}.172* @param extraImageMetadataFormatNames an array of173* {@code String}s to be returned from174* {@code getExtraImageMetadataFormatNames}. An array of length 0175* is normalized to {@code null}.176* @param extraImageMetadataFormatClassNames an array of177* {@code String}s, or {@code null}, to be used to instantiate178* a metadata format object to be returned from179* {@code getImageMetadataFormat}. An array of length180* 0 is normalized to {@code null}.181*182* @exception IllegalArgumentException if {@code vendorName}183* is {@code null}.184* @exception IllegalArgumentException if {@code version}185* is {@code null}.186* @exception IllegalArgumentException if {@code names}187* is {@code null} or has length 0.188* @exception IllegalArgumentException if {@code readerClassName}189* is {@code null}.190* @exception IllegalArgumentException if {@code inputTypes}191* is {@code null} or has length 0.192*/193public ImageReaderSpi(String vendorName,194String version,195String[] names,196String[] suffixes,197String[] MIMETypes,198String readerClassName,199Class<?>[] inputTypes,200String[] writerSpiNames,201boolean supportsStandardStreamMetadataFormat,202String nativeStreamMetadataFormatName,203String nativeStreamMetadataFormatClassName,204String[] extraStreamMetadataFormatNames,205String[] extraStreamMetadataFormatClassNames,206boolean supportsStandardImageMetadataFormat,207String nativeImageMetadataFormatName,208String nativeImageMetadataFormatClassName,209String[] extraImageMetadataFormatNames,210String[] extraImageMetadataFormatClassNames) {211super(vendorName, version,212names, suffixes, MIMETypes, readerClassName,213supportsStandardStreamMetadataFormat,214nativeStreamMetadataFormatName,215nativeStreamMetadataFormatClassName,216extraStreamMetadataFormatNames,217extraStreamMetadataFormatClassNames,218supportsStandardImageMetadataFormat,219nativeImageMetadataFormatName,220nativeImageMetadataFormatClassName,221extraImageMetadataFormatNames,222extraImageMetadataFormatClassNames);223224if (inputTypes == null) {225throw new IllegalArgumentException226("inputTypes == null!");227}228if (inputTypes.length == 0) {229throw new IllegalArgumentException230("inputTypes.length == 0!");231}232233this.inputTypes = (inputTypes == STANDARD_INPUT_TYPE) ?234new Class<?>[] { ImageInputStream.class } :235inputTypes.clone();236237// If length == 0, leave it null238if (writerSpiNames != null && writerSpiNames.length > 0) {239this.writerSpiNames = writerSpiNames.clone();240}241}242243/**244* Returns an array of {@code Class} objects indicating what245* types of objects may be used as arguments to the reader's246* {@code setInput} method.247*248* <p> For most readers, which only accept input from an249* {@code ImageInputStream}, a single-element array250* containing {@code ImageInputStream.class} should be251* returned.252*253* @return a non-{@code null} array of254* {@code Class} objects of length at least 1.255*/256public Class<?>[] getInputTypes() {257return inputTypes.clone();258}259260/**261* Returns {@code true} if the supplied source object appears262* to be of the format supported by this reader. Returning263* {@code true} from this method does not guarantee that264* reading will succeed, only that there appears to be a265* reasonable chance of success based on a brief inspection of the266* stream contents. If the source is an267* {@code ImageInputStream}, implementations will commonly268* check the first several bytes of the stream for a "magic269* number" associated with the format. Once actual reading has270* commenced, the reader may still indicate failure at any time271* prior to the completion of decoding.272*273* <p> It is important that the state of the object not be274* disturbed in order that other {@code ImageReaderSpi}s can275* properly determine whether they are able to decode the object.276* In particular, if the source is an277* {@code ImageInputStream}, a278* {@code mark}/{@code reset} pair should be used to279* preserve the stream position.280*281* <p> Formats such as "raw," which can potentially attempt282* to read nearly any stream, should return {@code false}283* in order to avoid being invoked in preference to a closer284* match.285*286* <p> If {@code source} is not an instance of one of the287* classes returned by {@code getInputTypes}, the method288* should simply return {@code false}.289*290* @param source the object (typically an291* {@code ImageInputStream}) to be decoded.292*293* @return {@code true} if it is likely that this stream can294* be decoded.295*296* @exception IllegalArgumentException if {@code source} is297* {@code null}.298* @exception IOException if an I/O error occurs while reading the299* stream.300*/301public abstract boolean canDecodeInput(Object source) throws IOException;302303/**304* Returns an instance of the {@code ImageReader}305* implementation associated with this service provider.306* The returned object will initially be in an initial state307* as if its {@code reset} method had been called.308*309* <p> The default implementation simply returns310* {@code createReaderInstance(null)}.311*312* @return an {@code ImageReader} instance.313*314* @exception IOException if an error occurs during loading,315* or initialization of the reader class, or during instantiation316* or initialization of the reader object.317*/318public ImageReader createReaderInstance() throws IOException {319return createReaderInstance(null);320}321322/**323* Returns an instance of the {@code ImageReader}324* implementation associated with this service provider.325* The returned object will initially be in an initial state326* as if its {@code reset} method had been called.327*328* <p> An {@code Object} may be supplied to the plug-in at329* construction time. The nature of the object is entirely330* plug-in specific.331*332* <p> Typically, a plug-in will implement this method using code333* such as {@code return new MyImageReader(this)}.334*335* @param extension a plug-in specific extension object, which may336* be {@code null}.337*338* @return an {@code ImageReader} instance.339*340* @exception IOException if the attempt to instantiate341* the reader fails.342* @exception IllegalArgumentException if the343* {@code ImageReader}'s constructor throws an344* {@code IllegalArgumentException} to indicate that the345* extension object is unsuitable.346*/347public abstract ImageReader createReaderInstance(Object extension)348throws IOException;349350/**351* Returns {@code true} if the {@code ImageReader} object352* passed in is an instance of the {@code ImageReader}353* associated with this service provider.354*355* <p> The default implementation compares the fully-qualified356* class name of the {@code reader} argument with the class357* name passed into the constructor. This method may be overridden358* if more sophisticated checking is required.359*360* @param reader an {@code ImageReader} instance.361*362* @return {@code true} if {@code reader} is recognized.363*364* @exception IllegalArgumentException if {@code reader} is365* {@code null}.366*/367public boolean isOwnReader(ImageReader reader) {368if (reader == null) {369throw new IllegalArgumentException("reader == null!");370}371String name = reader.getClass().getName();372return name.equals(pluginClassName);373}374375/**376* Returns an array of {@code String}s containing the fully377* qualified names of all the {@code ImageWriterSpi} classes378* that can understand the internal metadata representation used379* by the {@code ImageReader} associated with this service380* provider, or {@code null} if there are no such381* {@code ImageWriter}s specified. If a382* non-{@code null} value is returned, it must have non-zero383* length.384*385* <p> The first item in the array must be the name of the service386* provider for the "preferred" writer, as it will be used to387* instantiate the {@code ImageWriter} returned by388* {@code ImageIO.getImageWriter(ImageReader)}.389*390* <p> This mechanism may be used to obtain391* {@code ImageWriters} that will understand the internal392* structure of non-pixel meta-data (see393* {@code IIOTreeInfo}) generated by an394* {@code ImageReader}. By obtaining this data from the395* {@code ImageReader} and passing it on to one of the396* {@code ImageWriters} obtained with this method, a client397* program can read an image, modify it in some way, and write it398* back out while preserving all meta-data, without having to399* understand anything about the internal structure of the400* meta-data, or even about the image format.401*402* @return an array of {@code String}s of length at least 1403* containing names of {@code ImageWriterSpi}, or404* {@code null}.405*406* @see javax.imageio.ImageIO#getImageWriter(ImageReader)407*/408public String[] getImageWriterSpiNames() {409return writerSpiNames == null ?410null : writerSpiNames.clone();411}412}413414415