1 /* 2 * Copyright (c) 2000, 2004, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.imageio.spi; 27 28 import java.io.File; 29 import java.io.IOException; 30 import javax.imageio.stream.ImageInputStream; 31 32 /** 33 * The service provider interface (SPI) for 34 * {@code ImageInputStream}s. For more information on service 35 * provider interfaces, see the class comment for the 36 * {@code IIORegistry} class. 37 * 38 * <p> This interface allows arbitrary objects to be "wrapped" by 39 * instances of {@code ImageInputStream}. For example, 40 * a particular {@code ImageInputStreamSpi} might allow 41 * a generic {@code InputStream} to be used as an input source; 42 * another might take input from a {@code URL}. 43 * 44 * <p> By treating the creation of {@code ImageInputStream}s as a 45 * pluggable service, it becomes possible to handle future input 46 * sources without changing the API. Also, high-performance 47 * implementations of {@code ImageInputStream} (for example, 48 * native implementations for a particular platform) can be installed 49 * and used transparently by applications. 50 * 51 * @see IIORegistry 52 * @see javax.imageio.stream.ImageInputStream 53 * 54 */ 55 public abstract class ImageInputStreamSpi extends IIOServiceProvider { 56 57 /** 58 * A {@code Class} object indicating the legal object type 59 * for use by the {@code createInputStreamInstance} method. 60 */ 61 protected Class<?> inputClass; 62 63 /** 64 * Constructs a blank {@code ImageInputStreamSpi}. It is up 65 * to the subclass to initialize instance variables and/or 66 * override method implementations in order to provide working 67 * versions of all methods. 68 */ 69 protected ImageInputStreamSpi() { 70 } 71 72 /** 73 * Constructs an {@code ImageInputStreamSpi} with a given set 74 * of values. 75 * 76 * @param vendorName the vendor name. 77 * @param version a version identifier. 78 * @param inputClass a {@code Class} object indicating the 79 * legal object type for use by the 80 * {@code createInputStreamInstance} method. 81 * 82 * @exception IllegalArgumentException if {@code vendorName} 83 * is {@code null}. 84 * @exception IllegalArgumentException if {@code version} 85 * is {@code null}. 86 */ 87 public ImageInputStreamSpi(String vendorName, 88 String version, 89 Class<?> inputClass) { 90 super(vendorName, version); 91 this.inputClass = inputClass; 92 } 93 94 /** 95 * Returns a {@code Class} object representing the class or 96 * interface type that must be implemented by an input source in 97 * order to be "wrapped" in an {@code ImageInputStream} via 98 * the {@code createInputStreamInstance} method. 99 * 100 * <p> Typical return values might include 101 * {@code InputStream.class} or {@code URL.class}, but 102 * any class may be used. 103 * 104 * @return a {@code Class} variable. 105 * 106 * @see #createInputStreamInstance(Object, boolean, File) 107 */ 108 public Class<?> getInputClass() { 109 return inputClass; 110 } 111 112 /** 113 * Returns {@code true} if the {@code ImageInputStream} 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}, the value of 117 * the {@code useCache} argument to 118 * {@code createInputStreamInstance} will be ignored. 119 * 120 * <p> The default implementation returns {@code false}. 121 * 122 * @return {@code true} 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} if the {@code ImageInputStream} 131 * implementation associated with this service provider requires 132 * the use of a cache {@code File}. If {@code true}, 133 * the value of the {@code useCache} argument to 134 * {@code createInputStreamInstance} will be ignored. 135 * 136 * <p> The default implementation returns {@code false}. 137 * 138 * @return {@code true} 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} 147 * implementation associated with this service provider. If the 148 * use of a cache file is optional, the {@code useCache} 149 * parameter will be consulted. Where a cache is required, or 150 * not applicable, the value of {@code useCache} will be ignored. 151 * 152 * @param input an object of the class type returned by 153 * {@code getInputClass}. 154 * @param useCache a {@code boolean} indicating whether a 155 * cache file should be used, in cases where it is optional. 156 * @param cacheDir a {@code File} indicating where the 157 * cache file should be created, or {@code null} to use the 158 * system directory. 159 * 160 * @return an {@code ImageInputStream} instance. 161 * 162 * @exception IllegalArgumentException if {@code input} is 163 * not an instance of the correct class or is {@code null}. 164 * @exception IllegalArgumentException if a cache file is needed 165 * but {@code cacheDir} is non-{@code null} 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 175 createInputStreamInstance(Object input, 176 boolean useCache, 177 File cacheDir) throws IOException; 178 179 /** 180 * Returns an instance of the {@code ImageInputStream} 181 * implementation associated with this service provider. A cache 182 * file will be created in the system-dependent default 183 * temporary-file directory, if needed. 184 * 185 * @param input an object of the class type returned by 186 * {@code getInputClass}. 187 * 188 * @return an {@code ImageInputStream} instance. 189 * 190 * @exception IllegalArgumentException if {@code input} is 191 * not an instance of the correct class or is {@code null}. 192 * @exception IOException if a cache file is needed but cannot be 193 * created. 194 * 195 * @see #getInputClass() 196 */ 197 public ImageInputStream createInputStreamInstance(Object input) 198 throws IOException { 199 return createInputStreamInstance(input, true, null); 200 } 201 }