1 /* 2 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 package j2dbench.tests.iio; 42 43 import java.io.BufferedInputStream; 44 import java.io.ByteArrayInputStream; 45 import java.io.ByteArrayOutputStream; 46 import java.io.File; 47 import java.io.FileInputStream; 48 import java.io.IOException; 49 import java.io.InputStream; 50 import java.io.OutputStream; 51 import java.net.URL; 52 import javax.imageio.ImageIO; 53 import javax.imageio.spi.IIORegistry; 54 import javax.imageio.spi.ImageInputStreamSpi; 55 import javax.imageio.stream.FileCacheImageInputStream; 56 import javax.imageio.stream.FileImageInputStream; 57 import javax.imageio.stream.ImageInputStream; 58 import javax.imageio.stream.MemoryCacheImageInputStream; 59 60 import j2dbench.Group; 61 import j2dbench.Option; 62 import j2dbench.Result; 63 import j2dbench.TestEnvironment; 64 65 abstract class InputTests extends IIOTests { 66 67 protected static final int INPUT_FILE = 1; 68 protected static final int INPUT_URL = 2; 69 protected static final int INPUT_ARRAY = 3; 70 protected static final int INPUT_FILECHANNEL = 4; 71 72 protected static ImageInputStreamSpi fileChannelIISSpi; 73 static { 74 if (hasImageIO) { 75 ImageIO.scanForPlugins(); 76 IIORegistry registry = IIORegistry.getDefaultInstance(); 77 java.util.Iterator spis = 78 registry.getServiceProviders(ImageInputStreamSpi.class, false); 79 while (spis.hasNext()) { 80 ImageInputStreamSpi spi = (ImageInputStreamSpi)spis.next(); 81 String klass = spi.getClass().getName(); 82 if (klass.endsWith("ChannelImageInputStreamSpi")) { 83 fileChannelIISSpi = spi; 84 break; 85 } 86 } 87 } 88 } 89 90 protected static Group inputRoot; 91 protected static Group inputOptRoot; 92 93 protected static Group generalOptRoot; 94 protected static Group.EnableSet generalSourceRoot; 95 protected static Option sourceFileOpt; 96 protected static Option sourceUrlOpt; 97 protected static Option sourceByteArrayOpt; 98 99 protected static Group imageioGeneralOptRoot; 100 protected static Option sourceFileChannelOpt; 101 protected static Option useCacheTog; 102 103 public static void init() { 104 inputRoot = new Group(iioRoot, "input", "Input Benchmarks"); 105 inputRoot.setTabbed(); 106 107 // Options 108 inputOptRoot = new Group(inputRoot, "opts", "Options"); 109 110 // General Options 111 generalOptRoot = new Group(inputOptRoot, 112 "general", "General Options"); 113 generalSourceRoot = new Group.EnableSet(generalOptRoot, 114 "source", "Sources"); 115 sourceFileOpt = new InputType("file", "File", INPUT_FILE); 116 sourceUrlOpt = new InputType("url", "URL", INPUT_URL); 117 sourceByteArrayOpt = new InputType("byteArray", "byte[]", INPUT_ARRAY); 118 119 if (hasImageIO) { 120 // Image I/O Options 121 imageioGeneralOptRoot = new Group(inputOptRoot, 122 "imageio", "Image I/O Options"); 123 if (fileChannelIISSpi != null) { 124 sourceFileChannelOpt = 125 new InputType("fileChannel", "FileChannel", 126 INPUT_FILECHANNEL); 127 } 128 useCacheTog = new Option.Toggle(imageioGeneralOptRoot, "useCache", 129 "ImageIO.setUseCache()", 130 Option.Toggle.Off); 131 } 132 133 InputImageTests.init(); 134 if (hasImageIO) { 135 InputStreamTests.init(); 136 } 137 } 138 139 protected InputTests(Group parent, String nodeName, String description) { 140 super(parent, nodeName, description); 141 } 142 143 protected static class InputType extends Option.Enable { 144 private int type; 145 146 public InputType(String nodeName, String description, int type) { 147 super(generalSourceRoot, nodeName, description, false); 148 this.type = type; 149 } 150 151 public int getType() { 152 return type; 153 } 154 155 public String getAbbreviatedModifierDescription(Object value) { 156 return getModifierValueName(value); 157 } 158 159 public String getModifierValueName(Object val) { 160 return getNodeName(); 161 } 162 } 163 164 protected static abstract class Context { 165 int size; 166 Object input; 167 int inputType; 168 InputStream origStream; 169 170 Context(TestEnvironment env, Result result) { 171 size = env.getIntValue(sizeList); 172 if (hasImageIO) { 173 if (env.getModifier(useCacheTog) != null) { 174 ImageIO.setUseCache(env.isEnabled(useCacheTog)); 175 } 176 } 177 178 InputType t = (InputType)env.getModifier(generalSourceRoot); 179 inputType = t.getType(); 180 } 181 182 void initInput() { 183 if ((inputType == INPUT_FILE) || 184 (inputType == INPUT_URL) || 185 (inputType == INPUT_FILECHANNEL)) 186 { 187 try { 188 // REMIND: this approach will fail for GIF on pre-1.6 VM's 189 // (since earlier releases do not include a 190 // GIFImageWriter in the core JDK) 191 File inputfile = File.createTempFile("iio", ".tmp"); 192 inputfile.deleteOnExit(); 193 initContents(inputfile); 194 if (inputType == INPUT_FILE) { 195 input = inputfile; 196 } else if (inputType == INPUT_FILECHANNEL) { 197 input = inputfile; 198 } else { // inputType == INPUT_URL 199 try { 200 input = inputfile.toURI().toURL(); 201 } catch (Exception e) { 202 System.err.println("error creating URL"); 203 } 204 } 205 } catch (IOException e) { 206 System.err.println("error creating image file"); 207 e.printStackTrace(); 208 } 209 } else { 210 ByteArrayOutputStream out; 211 try { 212 out = new ByteArrayOutputStream(); 213 initContents(out); 214 } catch (IOException e) { 215 System.err.println("error creating image array"); 216 e.printStackTrace(); 217 return; 218 } 219 input = out.toByteArray(); 220 } 221 } 222 223 abstract void initContents(File f) throws IOException; 224 abstract void initContents(OutputStream out) throws IOException; 225 226 ImageInputStream createImageInputStream() throws IOException { 227 ImageInputStream iis; 228 BufferedInputStream bis; 229 switch (inputType) { 230 case INPUT_FILE: 231 iis = new FileImageInputStream((File)input); 232 break; 233 case INPUT_URL: 234 origStream = ((URL)input).openStream(); 235 bis = new BufferedInputStream(origStream); 236 if (ImageIO.getUseCache()) { 237 iis = new FileCacheImageInputStream(bis, null); 238 } else { 239 iis = new MemoryCacheImageInputStream(bis); 240 } 241 break; 242 case INPUT_ARRAY: 243 origStream = new ByteArrayInputStream((byte[])input); 244 bis = new BufferedInputStream(origStream); 245 if (ImageIO.getUseCache()) { 246 iis = new FileCacheImageInputStream(bis, null); 247 } else { 248 iis = new MemoryCacheImageInputStream(bis); 249 } 250 break; 251 case INPUT_FILECHANNEL: 252 FileInputStream fis = new FileInputStream((File)input); 253 origStream = fis; 254 java.nio.channels.FileChannel fc = fis.getChannel(); 255 iis = fileChannelIISSpi.createInputStreamInstance(fc, false, 256 null); 257 break; 258 default: 259 iis = null; 260 break; 261 } 262 return iis; 263 } 264 265 void closeOriginalStream() throws IOException { 266 if (origStream != null) { 267 origStream.close(); 268 origStream = null; 269 } 270 } 271 272 void cleanup(TestEnvironment env) { 273 } 274 } 275 }