Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/imageio/spi/ImageReaderWriterSpi.java
38830 views
/*1* Copyright (c) 2000, 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 java.lang.reflect.Constructor;29import java.lang.reflect.Method;30import java.util.Arrays;31import java.util.Iterator;32import javax.imageio.ImageReader;33import javax.imageio.metadata.IIOMetadata;34import javax.imageio.metadata.IIOMetadataFormat;35import javax.imageio.metadata.IIOMetadataFormatImpl;36import javax.imageio.stream.ImageInputStream;3738/**39* A superclass containing instance variables and methods common to40* <code>ImageReaderSpi</code> and <code>ImageWriterSpi</code>.41*42* @see IIORegistry43* @see ImageReaderSpi44* @see ImageWriterSpi45*46*/47public abstract class ImageReaderWriterSpi extends IIOServiceProvider {4849/**50* An array of strings to be returned from51* <code>getFormatNames</code>, initially <code>null</code>.52* Constructors should set this to a non-<code>null</code> value.53*/54protected String[] names = null;5556/**57* An array of strings to be returned from58* <code>getFileSuffixes</code>, initially <code>null</code>.59*/60protected String[] suffixes = null;6162/**63* An array of strings to be returned from64* <code>getMIMETypes</code>, initially <code>null</code>.65*/66protected String[] MIMETypes = null;6768/**69* A <code>String</code> containing the name of the associated70* plug-in class, initially <code>null</code>.71*/72protected String pluginClassName = null;7374/**75* A boolean indicating whether this plug-in supports the76* standard metadata format for stream metadata, initially77* <code>false</code>.78*/79protected boolean supportsStandardStreamMetadataFormat = false;8081/**82* A <code>String</code> containing the name of the native stream83* metadata format supported by this plug-in, initially84* <code>null</code>.85*/86protected String nativeStreamMetadataFormatName = null;8788/**89* A <code>String</code> containing the class name of the native90* stream metadata format supported by this plug-in, initially91* <code>null</code>.92*/93protected String nativeStreamMetadataFormatClassName = null;9495/**96* An array of <code>String</code>s containing the names of any97* additional stream metadata formats supported by this plug-in,98* initially <code>null</code>.99*/100protected String[] extraStreamMetadataFormatNames = null;101102/**103* An array of <code>String</code>s containing the class names of104* any additional stream metadata formats supported by this plug-in,105* initially <code>null</code>.106*/107protected String[] extraStreamMetadataFormatClassNames = null;108109/**110* A boolean indicating whether this plug-in supports the111* standard metadata format for image metadata, initially112* <code>false</code>.113*/114protected boolean supportsStandardImageMetadataFormat = false;115116/**117* A <code>String</code> containing the name of the118* native stream metadata format supported by this plug-in,119* initially <code>null</code>.120*/121protected String nativeImageMetadataFormatName = null;122123/**124* A <code>String</code> containing the class name of the125* native stream metadata format supported by this plug-in,126* initially <code>null</code>.127*/128protected String nativeImageMetadataFormatClassName = null;129130/**131* An array of <code>String</code>s containing the names of any132* additional image metadata formats supported by this plug-in,133* initially <code>null</code>.134*/135protected String[] extraImageMetadataFormatNames = null;136137/**138* An array of <code>String</code>s containing the class names of139* any additional image metadata formats supported by this140* plug-in, initially <code>null</code>.141*/142protected String[] extraImageMetadataFormatClassNames = null;143144/**145* Constructs an <code>ImageReaderWriterSpi</code> with a given146* set of values.147*148* @param vendorName the vendor name, as a non-<code>null</code>149* <code>String</code>.150* @param version a version identifier, as a non-<code>null</code>151* <code>String</code>.152* @param names a non-<code>null</code> array of153* <code>String</code>s indicating the format names. At least one154* entry must be present.155* @param suffixes an array of <code>String</code>s indicating the156* common file suffixes. If no suffixes are defined,157* <code>null</code> should be supplied. An array of length 0158* will be normalized to <code>null</code>.159* @param MIMETypes an array of <code>String</code>s indicating160* the format's MIME types. If no MIME types are defined,161* <code>null</code> should be supplied. An array of length 0162* will be normalized to <code>null</code>.163* @param pluginClassName the fully-qualified name of the164* associated <code>ImageReader</code> or <code>ImageWriter</code>165* class, as a non-<code>null</code> <code>String</code>.166* @param supportsStandardStreamMetadataFormat a167* <code>boolean</code> that indicates whether a stream metadata168* object can use trees described by the standard metadata format.169* @param nativeStreamMetadataFormatName a170* <code>String</code>, or <code>null</code>, to be returned from171* <code>getNativeStreamMetadataFormatName</code>.172* @param nativeStreamMetadataFormatClassName a173* <code>String</code>, or <code>null</code>, to be used to instantiate174* a metadata format object to be returned from175* <code>getNativeStreamMetadataFormat</code>.176* @param extraStreamMetadataFormatNames an array of177* <code>String</code>s, or <code>null</code>, to be returned from178* <code>getExtraStreamMetadataFormatNames</code>. An array of length179* 0 is normalized to <code>null</code>.180* @param extraStreamMetadataFormatClassNames an array of181* <code>String</code>s, or <code>null</code>, to be used to instantiate182* a metadata format object to be returned from183* <code>getStreamMetadataFormat</code>. An array of length184* 0 is normalized to <code>null</code>.185* @param supportsStandardImageMetadataFormat a186* <code>boolean</code> that indicates whether an image metadata187* object can use trees described by the standard metadata format.188* @param nativeImageMetadataFormatName a189* <code>String</code>, or <code>null</code>, to be returned from190* <code>getNativeImageMetadataFormatName</code>.191* @param nativeImageMetadataFormatClassName a192* <code>String</code>, or <code>null</code>, to be used to instantiate193* a metadata format object to be returned from194* <code>getNativeImageMetadataFormat</code>.195* @param extraImageMetadataFormatNames an array of196* <code>String</code>s to be returned from197* <code>getExtraImageMetadataFormatNames</code>. An array of length 0198* is normalized to <code>null</code>.199* @param extraImageMetadataFormatClassNames an array of200* <code>String</code>s, or <code>null</code>, to be used to instantiate201* a metadata format object to be returned from202* <code>getImageMetadataFormat</code>. An array of length203* 0 is normalized to <code>null</code>.204*205* @exception IllegalArgumentException if <code>vendorName</code>206* is <code>null</code>.207* @exception IllegalArgumentException if <code>version</code>208* is <code>null</code>.209* @exception IllegalArgumentException if <code>names</code>210* is <code>null</code> or has length 0.211* @exception IllegalArgumentException if <code>pluginClassName</code>212* is <code>null</code>.213*/214public ImageReaderWriterSpi(String vendorName,215String version,216String[] names,217String[] suffixes,218String[] MIMETypes,219String pluginClassName,220boolean supportsStandardStreamMetadataFormat,221String nativeStreamMetadataFormatName,222String nativeStreamMetadataFormatClassName,223String[] extraStreamMetadataFormatNames,224String[] extraStreamMetadataFormatClassNames,225boolean supportsStandardImageMetadataFormat,226String nativeImageMetadataFormatName,227String nativeImageMetadataFormatClassName,228String[] extraImageMetadataFormatNames,229String[] extraImageMetadataFormatClassNames) {230super(vendorName, version);231if (names == null) {232throw new IllegalArgumentException("names == null!");233}234if (names.length == 0) {235throw new IllegalArgumentException("names.length == 0!");236}237if (pluginClassName == null) {238throw new IllegalArgumentException("pluginClassName == null!");239}240241this.names = (String[])names.clone();242// If length == 0, leave it null243if (suffixes != null && suffixes.length > 0) {244this.suffixes = (String[])suffixes.clone();245}246// If length == 0, leave it null247if (MIMETypes != null && MIMETypes.length > 0) {248this.MIMETypes = (String[])MIMETypes.clone();249}250this.pluginClassName = pluginClassName;251252this.supportsStandardStreamMetadataFormat =253supportsStandardStreamMetadataFormat;254this.nativeStreamMetadataFormatName = nativeStreamMetadataFormatName;255this.nativeStreamMetadataFormatClassName =256nativeStreamMetadataFormatClassName;257// If length == 0, leave it null258if (extraStreamMetadataFormatNames != null &&259extraStreamMetadataFormatNames.length > 0) {260this.extraStreamMetadataFormatNames =261(String[])extraStreamMetadataFormatNames.clone();262}263// If length == 0, leave it null264if (extraStreamMetadataFormatClassNames != null &&265extraStreamMetadataFormatClassNames.length > 0) {266this.extraStreamMetadataFormatClassNames =267(String[])extraStreamMetadataFormatClassNames.clone();268}269this.supportsStandardImageMetadataFormat =270supportsStandardImageMetadataFormat;271this.nativeImageMetadataFormatName = nativeImageMetadataFormatName;272this.nativeImageMetadataFormatClassName =273nativeImageMetadataFormatClassName;274// If length == 0, leave it null275if (extraImageMetadataFormatNames != null &&276extraImageMetadataFormatNames.length > 0) {277this.extraImageMetadataFormatNames =278(String[])extraImageMetadataFormatNames.clone();279}280// If length == 0, leave it null281if (extraImageMetadataFormatClassNames != null &&282extraImageMetadataFormatClassNames.length > 0) {283this.extraImageMetadataFormatClassNames =284(String[])extraImageMetadataFormatClassNames.clone();285}286}287288/**289* Constructs a blank <code>ImageReaderWriterSpi</code>. It is up290* to the subclass to initialize instance variables and/or291* override method implementations in order to provide working292* versions of all methods.293*/294public ImageReaderWriterSpi() {295}296297/**298* Returns an array of <code>String</code>s containing299* human-readable names for the formats that are generally usable300* by the <code>ImageReader</code> or <code>ImageWriter</code>301* implementation associated with this service provider. For302* example, a single <code>ImageReader</code> might be able to303* process both PBM and PNM files.304*305* @return a non-<code>null</code> array of <code>String</code>s306* or length at least 1 containing informal format names307* associated with this reader or writer.308*/309public String[] getFormatNames() {310return (String[])names.clone();311}312313/**314* Returns an array of <code>String</code>s containing a list of315* file suffixes associated with the formats that are generally316* usable by the <code>ImageReader</code> or317* <code>ImageWriter</code> implementation associated with this318* service provider. For example, a single319* <code>ImageReader</code> might be able to process files with320* '.pbm' and '.pnm' suffixes, or both '.jpg' and '.jpeg'321* suffixes. If there are no known file suffixes,322* <code>null</code> will be returned.323*324* <p> Returning a particular suffix does not guarantee that files325* with that suffix can be processed; it merely indicates that it326* may be worthwhile attempting to decode or encode such files327* using this service provider.328*329* @return an array of <code>String</code>s or length at least 1330* containing common file suffixes associated with this reader or331* writer, or <code>null</code>.332*/333public String[] getFileSuffixes() {334return suffixes == null ? null : (String[])suffixes.clone();335}336337/**338* Returns an array of <code>String</code>s containing a list of339* MIME types associated with the formats that are generally340* usable by the <code>ImageReader</code> or341* <code>ImageWriter</code> implementation associated with this342* service provider.343*344* <p> Ideally, only a single MIME type would be required in order345* to describe a particular format. However, for several reasons346* it is necessary to associate a list of types with each service347* provider. First, many common image file formats do not have348* standard MIME types, so a list of commonly used unofficial349* names will be required, such as <code>image/x-pbm</code> and350* <code>image/x-portable-bitmap</code>. Some file formats have351* official MIME types but may sometimes be referred to using352* their previous unofficial designations, such as353* <code>image/x-png</code> instead of the official354* <code>image/png</code>. Finally, a single service provider may355* be capable of parsing multiple distinct types from the MIME356* point of view, for example <code>image/x-xbitmap</code> and357* <code>image/x-xpixmap</code>.358*359* <p> Returning a particular MIME type does not guarantee that360* files claiming to be of that type can be processed; it merely361* indicates that it may be worthwhile attempting to decode or362* encode such files using this service provider.363*364* @return an array of <code>String</code>s or length at least 1365* containing MIME types associated with this reader or writer, or366* <code>null</code>.367*/368public String[] getMIMETypes() {369return MIMETypes == null ? null : (String[])MIMETypes.clone();370}371372/**373* Returns the fully-qualified class name of the374* <code>ImageReader</code> or <code>ImageWriter</code> plug-in375* associated with this service provider.376*377* @return the class name, as a non-<code>null</code>378* <code>String</code>.379*/380public String getPluginClassName() {381return pluginClassName;382}383384/**385* Returns <code>true</code> if the standard metadata format is386* among the document formats recognized by the387* <code>getAsTree</code> and <code>setFromTree</code> methods on388* the stream metadata objects produced or consumed by this389* plug-in.390*391* @return <code>true</code> if the standard format is supported392* for stream metadata.393*/394public boolean isStandardStreamMetadataFormatSupported() {395return supportsStandardStreamMetadataFormat;396}397398/**399* Returns the name of the "native" stream metadata format for400* this plug-in, which typically allows for lossless encoding and401* transmission of the stream metadata stored in the format handled by402* this plug-in. If no such format is supported,403* <code>null</code>will be returned.404*405* <p> The default implementation returns the406* <code>nativeStreamMetadataFormatName</code> instance variable,407* which is typically set by the constructor.408*409* @return the name of the native stream metadata format, or410* <code>null</code>.411*412*/413public String getNativeStreamMetadataFormatName() {414return nativeStreamMetadataFormatName;415}416417/**418* Returns an array of <code>String</code>s containing the names419* of additional document formats, other than the native and420* standard formats, recognized by the421* <code>getAsTree</code> and <code>setFromTree</code> methods on422* the stream metadata objects produced or consumed by this423* plug-in.424*425* <p> If the plug-in does not handle metadata, null should be426* returned.427*428* <p> The set of formats may differ according to the particular429* images being read or written; this method should indicate all430* the additional formats supported by the plug-in under any431* circumstances.432*433* <p> The default implementation returns a clone of the434* <code>extraStreamMetadataFormatNames</code> instance variable,435* which is typically set by the constructor.436*437* @return an array of <code>String</code>s, or null.438*439* @see IIOMetadata#getMetadataFormatNames440* @see #getExtraImageMetadataFormatNames441* @see #getNativeStreamMetadataFormatName442*/443public String[] getExtraStreamMetadataFormatNames() {444return extraStreamMetadataFormatNames == null ?445null : (String[])extraStreamMetadataFormatNames.clone();446}447448/**449* Returns <code>true</code> if the standard metadata format is450* among the document formats recognized by the451* <code>getAsTree</code> and <code>setFromTree</code> methods on452* the image metadata objects produced or consumed by this453* plug-in.454*455* @return <code>true</code> if the standard format is supported456* for image metadata.457*/458public boolean isStandardImageMetadataFormatSupported() {459return supportsStandardImageMetadataFormat;460}461462/**463* Returns the name of the "native" image metadata format for464* this plug-in, which typically allows for lossless encoding and465* transmission of the image metadata stored in the format handled by466* this plug-in. If no such format is supported,467* <code>null</code>will be returned.468*469* <p> The default implementation returns the470* <code>nativeImageMetadataFormatName</code> instance variable,471* which is typically set by the constructor.472*473* @return the name of the native image metadata format, or474* <code>null</code>.475*476* @see #getExtraImageMetadataFormatNames477*/478public String getNativeImageMetadataFormatName() {479return nativeImageMetadataFormatName;480}481482/**483* Returns an array of <code>String</code>s containing the names484* of additional document formats, other than the native and485* standard formats, recognized by the486* <code>getAsTree</code> and <code>setFromTree</code> methods on487* the image metadata objects produced or consumed by this488* plug-in.489*490* <p> If the plug-in does not handle image metadata, null should491* be returned.492*493* <p> The set of formats may differ according to the particular494* images being read or written; this method should indicate all495* the additional formats supported by the plug-in under any circumstances.496*497* <p> The default implementation returns a clone of the498* <code>extraImageMetadataFormatNames</code> instance variable,499* which is typically set by the constructor.500*501* @return an array of <code>String</code>s, or null.502*503* @see IIOMetadata#getMetadataFormatNames504* @see #getExtraStreamMetadataFormatNames505* @see #getNativeImageMetadataFormatName506*/507public String[] getExtraImageMetadataFormatNames() {508return extraImageMetadataFormatNames == null ?509null : (String[])extraImageMetadataFormatNames.clone();510}511512/**513* Returns an <code>IIOMetadataFormat</code> object describing the514* given stream metadata format, or <code>null</code> if no515* description is available. The supplied name must be the native516* stream metadata format name, the standard metadata format name,517* or one of those returned by518* <code>getExtraStreamMetadataFormatNames</code>.519*520* @param formatName the desired stream metadata format.521*522* @return an <code>IIOMetadataFormat</code> object.523*524* @exception IllegalArgumentException if <code>formatName</code>525* is <code>null</code> or is not a supported name.526*/527public IIOMetadataFormat getStreamMetadataFormat(String formatName) {528return getMetadataFormat(formatName,529supportsStandardStreamMetadataFormat,530nativeStreamMetadataFormatName,531nativeStreamMetadataFormatClassName,532extraStreamMetadataFormatNames,533extraStreamMetadataFormatClassNames);534}535536/**537* Returns an <code>IIOMetadataFormat</code> object describing the538* given image metadata format, or <code>null</code> if no539* description is available. The supplied name must be the native540* image metadata format name, the standard metadata format name,541* or one of those returned by542* <code>getExtraImageMetadataFormatNames</code>.543*544* @param formatName the desired image metadata format.545*546* @return an <code>IIOMetadataFormat</code> object.547*548* @exception IllegalArgumentException if <code>formatName</code>549* is <code>null</code> or is not a supported name.550*/551public IIOMetadataFormat getImageMetadataFormat(String formatName) {552return getMetadataFormat(formatName,553supportsStandardImageMetadataFormat,554nativeImageMetadataFormatName,555nativeImageMetadataFormatClassName,556extraImageMetadataFormatNames,557extraImageMetadataFormatClassNames);558}559560private IIOMetadataFormat getMetadataFormat(String formatName,561boolean supportsStandard,562String nativeName,563String nativeClassName,564String [] extraNames,565String [] extraClassNames) {566if (formatName == null) {567throw new IllegalArgumentException("formatName == null!");568}569if (supportsStandard && formatName.equals570(IIOMetadataFormatImpl.standardMetadataFormatName)) {571572return IIOMetadataFormatImpl.getStandardFormatInstance();573}574String formatClassName = null;575if (formatName.equals(nativeName)) {576formatClassName = nativeClassName;577} else if (extraNames != null) {578for (int i = 0; i < extraNames.length; i++) {579if (formatName.equals(extraNames[i])) {580formatClassName = extraClassNames[i];581break; // out of for582}583}584}585if (formatClassName == null) {586throw new IllegalArgumentException("Unsupported format name");587}588try {589Class cls = Class.forName(formatClassName, true,590ClassLoader.getSystemClassLoader());591Method meth = cls.getMethod("getInstance");592return (IIOMetadataFormat) meth.invoke(null);593} catch (Exception e) {594RuntimeException ex =595new IllegalStateException ("Can't obtain format");596ex.initCause(e);597throw ex;598}599}600}601602603