Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/imageio/spi/ImageReaderSpi.java
38830 views
/*1* Copyright (c) 1999, 2013, 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</code>s.33* For more information on service provider classes, see the class comment34* for the <code>IIORegistry</code> class.35*36* <p> Each <code>ImageReaderSpi</code> provides several types of information37* about the <code>ImageReader</code> 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>, <code>getDescription</code>,42* and <code>getVersion</code> 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>, <code>getFileSuffixes</code>, and51* <code>getMIMETypes</code> methods. These methods may be used to52* identify candidate <code>ImageReader</code>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</code>s58* are likely to be able to parse a particular data stream is provided59* by the <code>canDecodeInput</code> method. This methods allows the60* service provider to inspect the actual stream contents.61*62* <p> Finally, an instance of the <code>ImageReader</code> class63* associated with this service provider may be obtained by calling64* the <code>createReaderInstance</code> 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</code>, to be returned from78* <code>getInputTypes</code>.79* @deprecated Instead of using this field, directly create80* the equivalent array <code>{ ImageInputStream.class }</code>.81*/82@Deprecated83public static final Class[] STANDARD_INPUT_TYPE =84{ ImageInputStream.class };8586/**87* An array of <code>Class</code> objects to be returned from88* <code>getInputTypes</code>, initially <code>null</code>.89*/90protected Class[] inputTypes = null;9192/**93* An array of strings to be returned from94* <code>getImageWriterSpiNames</code>, initially95* <code>null</code>.96*/97protected String[] writerSpiNames = null;9899/**100* The <code>Class</code> of the reader, initially101* <code>null</code>.102*/103private Class readerClass = null;104105/**106* Constructs a blank <code>ImageReaderSpi</code>. 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</code> with a given116* set of values.117*118* @param vendorName the vendor name, as a non-<code>null</code>119* <code>String</code>.120* @param version a version identifier, as a non-<code>null</code>121* <code>String</code>.122* @param names a non-<code>null</code> array of123* <code>String</code>s indicating the format names. At least one124* entry must be present.125* @param suffixes an array of <code>String</code>s indicating the126* common file suffixes. If no suffixes are defined,127* <code>null</code> should be supplied. An array of length 0128* will be normalized to <code>null</code>.129* @param MIMETypes an array of <code>String</code>s indicating130* the format's MIME types. If no MIME types are defined,131* <code>null</code> should be supplied. An array of length 0132* will be normalized to <code>null</code>.133* @param readerClassName the fully-qualified name of the134* associated <code>ImageReader</code> class, as a135* non-<code>null</code> <code>String</code>.136* @param inputTypes a non-<code>null</code> array of137* <code>Class</code> objects of length at least 1 indicating the138* legal input types.139* @param writerSpiNames an array <code>String</code>s naming the140* classes of all associated <code>ImageWriter</code>s, or141* <code>null</code>. An array of length 0 is normalized to142* <code>null</code>.143* @param supportsStandardStreamMetadataFormat a144* <code>boolean</code> that indicates whether a stream metadata145* object can use trees described by the standard metadata format.146* @param nativeStreamMetadataFormatName a147* <code>String</code>, or <code>null</code>, to be returned from148* <code>getNativeStreamMetadataFormatName</code>.149* @param nativeStreamMetadataFormatClassName a150* <code>String</code>, or <code>null</code>, to be used to instantiate151* a metadata format object to be returned from152* <code>getNativeStreamMetadataFormat</code>.153* @param extraStreamMetadataFormatNames an array of154* <code>String</code>s, or <code>null</code>, to be returned from155* <code>getExtraStreamMetadataFormatNames</code>. An array of length156* 0 is normalized to <code>null</code>.157* @param extraStreamMetadataFormatClassNames an array of158* <code>String</code>s, or <code>null</code>, to be used to instantiate159* a metadata format object to be returned from160* <code>getStreamMetadataFormat</code>. An array of length161* 0 is normalized to <code>null</code>.162* @param supportsStandardImageMetadataFormat a163* <code>boolean</code> that indicates whether an image metadata164* object can use trees described by the standard metadata format.165* @param nativeImageMetadataFormatName a166* <code>String</code>, or <code>null</code>, to be returned from167* <code>getNativeImageMetadataFormatName</code>.168* @param nativeImageMetadataFormatClassName a169* <code>String</code>, or <code>null</code>, to be used to instantiate170* a metadata format object to be returned from171* <code>getNativeImageMetadataFormat</code>.172* @param extraImageMetadataFormatNames an array of173* <code>String</code>s to be returned from174* <code>getExtraImageMetadataFormatNames</code>. An array of length 0175* is normalized to <code>null</code>.176* @param extraImageMetadataFormatClassNames an array of177* <code>String</code>s, or <code>null</code>, to be used to instantiate178* a metadata format object to be returned from179* <code>getImageMetadataFormat</code>. An array of length180* 0 is normalized to <code>null</code>.181*182* @exception IllegalArgumentException if <code>vendorName</code>183* is <code>null</code>.184* @exception IllegalArgumentException if <code>version</code>185* is <code>null</code>.186* @exception IllegalArgumentException if <code>names</code>187* is <code>null</code> or has length 0.188* @exception IllegalArgumentException if <code>readerClassName</code>189* is <code>null</code>.190* @exception IllegalArgumentException if <code>inputTypes</code>191* is <code>null</code> 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 = (String[])writerSpiNames.clone();240}241}242243/**244* Returns an array of <code>Class</code> objects indicating what245* types of objects may be used as arguments to the reader's246* <code>setInput</code> method.247*248* <p> For most readers, which only accept input from an249* <code>ImageInputStream</code>, a single-element array250* containing <code>ImageInputStream.class</code> should be251* returned.252*253* @return a non-<code>null</code> array of254* <code>Class</code>objects of length at least 1.255*/256public Class[] getInputTypes() {257return (Class[])inputTypes.clone();258}259260/**261* Returns <code>true</code> if the supplied source object appears262* to be of the format supported by this reader. Returning263* <code>true</code> 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</code>, 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</code>s can275* properly determine whether they are able to decode the object.276* In particular, if the source is an277* <code>ImageInputStream</code>, a278* <code>mark</code>/<code>reset</code> 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</code>283* in order to avoid being invoked in preference to a closer284* match.285*286* <p> If <code>source</code> is not an instance of one of the287* classes returned by <code>getInputTypes</code>, the method288* should simply return <code>false</code>.289*290* @param source the object (typically an291* <code>ImageInputStream</code>) to be decoded.292*293* @return <code>true</code> if it is likely that this stream can294* be decoded.295*296* @exception IllegalArgumentException if <code>source</code> is297* <code>null</code>.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</code>305* implementation associated with this service provider.306* The returned object will initially be in an initial state307* as if its <code>reset</code> method had been called.308*309* <p> The default implementation simply returns310* <code>createReaderInstance(null)</code>.311*312* @return an <code>ImageReader</code> 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</code>324* implementation associated with this service provider.325* The returned object will initially be in an initial state326* as if its <code>reset</code> method had been called.327*328* <p> An <code>Object</code> 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)</code>.334*335* @param extension a plug-in specific extension object, which may336* be <code>null</code>.337*338* @return an <code>ImageReader</code> instance.339*340* @exception IOException if the attempt to instantiate341* the reader fails.342* @exception IllegalArgumentException if the343* <code>ImageReader</code>'s constructor throws an344* <code>IllegalArgumentException</code> to indicate that the345* extension object is unsuitable.346*/347public abstract ImageReader createReaderInstance(Object extension)348throws IOException;349350/**351* Returns <code>true</code> if the <code>ImageReader</code> object352* passed in is an instance of the <code>ImageReader</code>353* associated with this service provider.354*355* <p> The default implementation compares the fully-qualified356* class name of the <code>reader</code> 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</code> instance.361*362* @return <code>true</code> if <code>reader</code> is recognized.363*364* @exception IllegalArgumentException if <code>reader</code> is365* <code>null</code>.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</code>s containing the fully377* qualified names of all the <code>ImageWriterSpi</code> classes378* that can understand the internal metadata representation used379* by the <code>ImageReader</code> associated with this service380* provider, or <code>null</code> if there are no such381* <code>ImageWriter</code>s specified. If a382* non-<code>null</code> 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</code> returned by388* <code>ImageIO.getImageWriter(ImageReader)</code>.389*390* <p> This mechanism may be used to obtain391* <code>ImageWriters</code> that will understand the internal392* structure of non-pixel meta-data (see393* <code>IIOTreeInfo</code>) generated by an394* <code>ImageReader</code>. By obtaining this data from the395* <code>ImageReader</code> and passing it on to one of the396* <code>ImageWriters</code> 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</code>s of length at least 1403* containing names of <code>ImageWriterSpi</code>, or404* <code>null</code>.405*406* @see javax.imageio.ImageIO#getImageWriter(ImageReader)407*/408public String[] getImageWriterSpiNames() {409return writerSpiNames == null ?410null : (String[])writerSpiNames.clone();411}412}413414415