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.BufferedOutputStream;
44 import java.io.ByteArrayOutputStream;
45 import java.io.File;
46 import java.io.FileOutputStream;
47 import java.io.IOException;
48 import java.io.OutputStream;
49 import javax.imageio.ImageIO;
50 import javax.imageio.spi.IIORegistry;
51 import javax.imageio.spi.ImageOutputStreamSpi;
52 import javax.imageio.stream.FileCacheImageOutputStream;
53 import javax.imageio.stream.FileImageOutputStream;
54 import javax.imageio.stream.ImageOutputStream;
55 import javax.imageio.stream.MemoryCacheImageOutputStream;
56 
57 import j2dbench.Group;
58 import j2dbench.Option;
59 import j2dbench.Result;
60 import j2dbench.TestEnvironment;
61 
62 abstract class OutputTests extends IIOTests {
63 
64     protected static final int OUTPUT_FILE        = 1;
65     protected static final int OUTPUT_ARRAY       = 2;
66     protected static final int OUTPUT_FILECHANNEL = 3;
67 
68     protected static ImageOutputStreamSpi fileChannelIOSSpi;
69     static {
70         if (hasImageIO) {
ImageIO.scanForPlugins()71             ImageIO.scanForPlugins();
72             IIORegistry registry = IIORegistry.getDefaultInstance();
73             java.util.Iterator spis =
74                 registry.getServiceProviders(ImageOutputStreamSpi.class,
75                                              false);
76             while (spis.hasNext()) {
77                 ImageOutputStreamSpi spi = (ImageOutputStreamSpi)spis.next();
78                 String klass = spi.getClass().getName();
79                 if (klass.endsWith("ChannelImageOutputStreamSpi")) {
80                     fileChannelIOSSpi = spi;
81                     break;
82                 }
83             }
84         }
85     }
86 
87     protected static Group outputRoot;
88     protected static Group outputOptRoot;
89 
90     protected static Group generalOptRoot;
91     protected static Group.EnableSet generalDestRoot;
92     protected static Option destFileOpt;
93     protected static Option destByteArrayOpt;
94 
95     protected static Group imageioGeneralOptRoot;
96     protected static Option destFileChannelOpt;
97     protected static Option useCacheTog;
98 
init()99     public static void init() {
100         outputRoot = new Group(iioRoot, "output", "Output Benchmarks");
101         outputRoot.setTabbed();
102 
103         // Options
104         outputOptRoot = new Group(outputRoot, "opts", "Options");
105 
106         // General Options
107         generalOptRoot = new Group(outputOptRoot,
108                                    "general", "General Options");
109         generalDestRoot = new Group.EnableSet(generalOptRoot,
110                                               "dest", "Destintations");
111         destFileOpt = new OutputType("file", "File", OUTPUT_FILE);
112         destByteArrayOpt = new OutputType("byteArray", "byte[]", OUTPUT_ARRAY);
113 
114         if (hasImageIO) {
115             // Image I/O Options
116             imageioGeneralOptRoot = new Group(outputOptRoot,
117                                               "imageio", "Image I/O Options");
118             if (fileChannelIOSSpi != null) {
119                 destFileChannelOpt =
120                     new OutputType("fileChannel", "FileChannel",
121                                    OUTPUT_FILECHANNEL);
122             }
123             useCacheTog = new Option.Toggle(imageioGeneralOptRoot, "useCache",
124                                             "ImageIO.setUseCache()",
125                                             Option.Toggle.Off);
126         }
127 
128         OutputImageTests.init();
129         if (hasImageIO) {
130             OutputStreamTests.init();
131         }
132     }
133 
OutputTests(Group parent, String nodeName, String description)134     protected OutputTests(Group parent, String nodeName, String description) {
135         super(parent, nodeName, description);
136     }
137 
138     protected static class OutputType extends Option.Enable {
139         private int type;
140 
OutputType(String nodeName, String description, int type)141         public OutputType(String nodeName, String description, int type) {
142             super(generalDestRoot, nodeName, description, false);
143             this.type = type;
144         }
145 
getType()146         public int getType() {
147             return type;
148         }
149 
getAbbreviatedModifierDescription(Object value)150         public String getAbbreviatedModifierDescription(Object value) {
151             return getModifierValueName(value);
152         }
153 
getModifierValueName(Object val)154         public String getModifierValueName(Object val) {
155             return getNodeName();
156         }
157     }
158 
159     protected abstract static class Context {
160         int size;
161         Object output;
162         int outputType;
163         OutputStream origStream;
164 
Context(TestEnvironment env, Result result)165         Context(TestEnvironment env, Result result) {
166             size = env.getIntValue(sizeList);
167             if (hasImageIO) {
168                 if (env.getModifier(useCacheTog) != null) {
169                     ImageIO.setUseCache(env.isEnabled(useCacheTog));
170                 }
171             }
172 
173             OutputType t = (OutputType)env.getModifier(generalDestRoot);
174             outputType = t.getType();
175         }
176 
initOutput()177         void initOutput() {
178             if ((outputType == OUTPUT_FILE) ||
179                 (outputType == OUTPUT_FILECHANNEL))
180             {
181                 try {
182                     File outputfile = File.createTempFile("iio", ".tmp");
183                     outputfile.deleteOnExit();
184                     output = outputfile;
185                 } catch (IOException e) {
186                     System.err.println("error creating temp file");
187                     e.printStackTrace();
188                 }
189             }
190         }
191 
createImageOutputStream()192         ImageOutputStream createImageOutputStream() throws IOException {
193             ImageOutputStream ios;
194             switch (outputType) {
195             case OUTPUT_FILE:
196                 ios = new FileImageOutputStream((File)output);
197                 break;
198             case OUTPUT_ARRAY:
199                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
200                 BufferedOutputStream bos = new BufferedOutputStream(baos);
201                 if (ImageIO.getUseCache()) {
202                     ios = new FileCacheImageOutputStream(bos, null);
203                 } else {
204                     ios = new MemoryCacheImageOutputStream(bos);
205                 }
206                 break;
207             case OUTPUT_FILECHANNEL:
208                 FileOutputStream fos = new FileOutputStream((File)output);
209                 origStream = fos;
210                 java.nio.channels.FileChannel fc = fos.getChannel();
211                 ios = fileChannelIOSSpi.createOutputStreamInstance(fc, false,
212                                                                    null);
213                 break;
214             default:
215                 ios = null;
216                 break;
217             }
218             return ios;
219         }
220 
closeOriginalStream()221         void closeOriginalStream() throws IOException {
222             if (origStream != null) {
223                 origStream.close();
224                 origStream = null;
225             }
226         }
227 
cleanup(TestEnvironment env)228         void cleanup(TestEnvironment env) {
229         }
230     }
231 }
232