001 /*
002 * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.imageio.spi;
027
028 import java.io.File;
029 import java.io.IOException;
030 import javax.imageio.stream.ImageInputStream;
031
032 /**
033 * The service provider interface (SPI) for
034 * <code>ImageInputStream</code>s. For more information on service
035 * provider interfaces, see the class comment for the
036 * <code>IIORegistry</code> class.
037 *
038 * <p> This interface allows arbitrary objects to be "wrapped" by
039 * instances of <code>ImageInputStream</code>. For example,
040 * a particular <code>ImageInputStreamSpi</code> might allow
041 * a generic <code>InputStream</code> to be used as an input source;
042 * another might take input from a <code>URL</code>.
043 *
044 * <p> By treating the creation of <code>ImageInputStream</code>s as a
045 * pluggable service, it becomes possible to handle future input
046 * sources without changing the API. Also, high-performance
047 * implementations of <code>ImageInputStream</code> (for example,
048 * native implementations for a particular platform) can be installed
049 * and used transparently by applications.
050 *
051 * @see IIORegistry
052 * @see javax.imageio.stream.ImageInputStream
053 *
054 * @version 0.5
055 */
056 public abstract class ImageInputStreamSpi extends IIOServiceProvider {
057
058 /**
059 * A <code>Class</code> object indicating the legal object type
060 * for use by the <code>createInputStreamInstance</code> method.
061 */
062 protected Class<?> inputClass;
063
064 /**
065 * Constructs a blank <code>ImageInputStreamSpi</code>. It is up
066 * to the subclass to initialize instance variables and/or
067 * override method implementations in order to provide working
068 * versions of all methods.
069 */
070 protected ImageInputStreamSpi() {
071 }
072
073 /**
074 * Constructs an <code>ImageInputStreamSpi</code> with a given set
075 * of values.
076 *
077 * @param vendorName the vendor name.
078 * @param version a version identifier.
079 * @param inputClass a <code>Class</code> object indicating the
080 * legal object type for use by the
081 * <code>createInputStreamInstance</code> method.
082 *
083 * @exception IllegalArgumentException if <code>vendorName</code>
084 * is <code>null</code>.
085 * @exception IllegalArgumentException if <code>version</code>
086 * is <code>null</code>.
087 */
088 public ImageInputStreamSpi(String vendorName, String version,
089 Class<?> inputClass) {
090 super (vendorName, version);
091 this .inputClass = inputClass;
092 }
093
094 /**
095 * Returns a <code>Class</code> object representing the class or
096 * interface type that must be implemented by an input source in
097 * order to be "wrapped" in an <code>ImageInputStream</code> via
098 * the <code>createInputStreamInstance</code> method.
099 *
100 * <p> Typical return values might include
101 * <code>InputStream.class</code> or <code>URL.class</code>, but
102 * any class may be used.
103 *
104 * @return a <code>Class</code> variable.
105 *
106 * @see #createInputStreamInstance(Object, boolean, File)
107 */
108 public Class<?> getInputClass() {
109 return inputClass;
110 }
111
112 /**
113 * Returns <code>true</code> if the <code>ImageInputStream</code>
114 * implementation associated with this service provider can
115 * optionally make use of a cache file for improved performance
116 * and/or memory footrprint. If <code>false</code>, the value of
117 * the <code>useCache</code> argument to
118 * <code>createInputStreamInstance</code> will be ignored.
119 *
120 * <p> The default implementation returns <code>false</code>.
121 *
122 * @return <code>true</code> if a cache file can be used by the
123 * input streams created by this service provider.
124 */
125 public boolean canUseCacheFile() {
126 return false;
127 }
128
129 /**
130 * Returns <code>true</code> if the <code>ImageInputStream</code>
131 * implementation associated with this service provider requires
132 * the use of a cache <code>File</code>. If <code>true</code>,
133 * the value of the <code>useCache</code> argument to
134 * <code>createInputStreamInstance</code> will be ignored.
135 *
136 * <p> The default implementation returns <code>false</code>.
137 *
138 * @return <code>true</code> if a cache file is needed by the
139 * input streams created by this service provider.
140 */
141 public boolean needsCacheFile() {
142 return false;
143 }
144
145 /**
146 * Returns an instance of the <code>ImageInputStream</code>
147 * implementation associated with this service provider. If the
148 * use of a cache file is optional, the <code>useCache</code>
149 * parameter will be consulted. Where a cache is required, or
150 * not applicable, the value of <code>useCache</code> will be ignored.
151 *
152 * @param input an object of the class type returned by
153 * <code>getInputClass</code>.
154 * @param useCache a <code>boolean</code> indicating whether a
155 * cache file should be used, in cases where it is optional.
156 * @param cacheDir a <code>File</code> indicating where the
157 * cache file should be created, or <code>null</code> to use the
158 * system directory.
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 IllegalArgumentException if a cache file is needed
165 * but <code>cacheDir</code> is non-<code>null</code> and is not a
166 * directory.
167 * @exception IOException if a cache file is needed but cannot be
168 * created.
169 *
170 * @see #getInputClass
171 * @see #canUseCacheFile
172 * @see #needsCacheFile
173 */
174 public abstract ImageInputStream createInputStreamInstance(
175 Object input, boolean useCache, File cacheDir)
176 throws IOException;
177
178 /**
179 * Returns an instance of the <code>ImageInputStream</code>
180 * implementation associated with this service provider. A cache
181 * file will be created in the system-dependent default
182 * temporary-file directory, if needed.
183 *
184 * @param input an object of the class type returned by
185 * <code>getInputClass</code>.
186 *
187 * @return an <code>ImageInputStream</code> instance.
188 *
189 * @exception IllegalArgumentException if <code>input</code> is
190 * not an instance of the correct class or is <code>null</code>.
191 * @exception IOException if a cache file is needed but cannot be
192 * created.
193 *
194 * @see #getInputClass()
195 */
196 public ImageInputStream createInputStreamInstance(Object input)
197 throws IOException {
198 return createInputStreamInstance(input, true, null);
199 }
200 }
|