1 /* 2 * Copyright (c) 2003, 2017, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4835841 27 * @summary This test verifies that we will able to register new SPI after 28 * deregistration all previously registered SPIs by using 29 * deregisterAll() method 30 */ 31 32 import java.io.IOException; 33 import java.util.Locale; 34 35 import javax.imageio.ImageIO; 36 import javax.imageio.ImageReader; 37 import javax.imageio.spi.IIORegistry; 38 import javax.imageio.spi.ImageReaderSpi; 39 import javax.imageio.spi.ServiceRegistry; 40 import javax.imageio.stream.ImageInputStream; 41 42 public class DeregisterAllSpiTest { 43 44 public DeregisterAllSpiTest() throws Exception { 45 ImageReaderSpi BMPSpi = new BMPImageReaderSPI(); 46 IIORegistry.getDefaultInstance().registerServiceProvider(BMPSpi); 47 48 System.out.println("Reader Format Names available in the registry"); 49 String formatNames[] = ImageIO.getReaderFormatNames(); 50 if( formatNames == null || formatNames.length <= 0) { 51 throw new RuntimeException("No registered ImageReaders!"); 52 } 53 for (int x=0; x < formatNames.length; x++) { 54 System.out.println("format "+formatNames[x]); 55 } 56 57 IIORegistry.getDefaultInstance().deregisterAll(); 58 59 System.out.println("\nReader Format Names after deregistering all SPIs"); 60 formatNames = ImageIO.getReaderFormatNames(); 61 if(formatNames.length == 0) { 62 System.out.println("No readers available\n"); 63 } else { 64 throw new RuntimeException("Some providers was not deregistered!"); 65 } 66 67 IIORegistry.getDefaultInstance().registerServiceProvider(BMPSpi); 68 System.out.println("Reader Format Names after re-register of BMP Plugin"); 69 formatNames = ImageIO.getReaderFormatNames(); 70 if(formatNames.length == 0) { 71 throw new RuntimeException("Unable to register new SPI after deregisterAll()!"); 72 } 73 } 74 75 76 public static void main(String args[]) throws Exception{ 77 DeregisterAllSpiTest regis = new DeregisterAllSpiTest(); 78 } 79 80 81 public static class BMPImageReaderSPI extends javax.imageio.spi.ImageReaderSpi{ 82 83 private static final String vendorName = "Javasoft"; 84 85 private static final String version = "2.0"; 86 87 private static final String[] names = { "bmp" }; 88 89 private static final String[] suffixes = { "bmp" }; 90 91 private static final String[] MIMETypes = { "image/x-bmp"}; 92 93 private static final String readerClassName = 94 "com.sun.imageio.plugins.png.PNGImageReader"; 95 96 private static final String[] writerSpiNames = { 97 "com.sun.imageio.plugins.png.PNGImageWriterSpi" 98 }; 99 100 public BMPImageReaderSPI() { 101 super(vendorName, 102 version, 103 names, 104 suffixes, 105 MIMETypes, 106 readerClassName, 107 STANDARD_INPUT_TYPE, 108 writerSpiNames, 109 false, 110 null, null, 111 null, null, 112 true, 113 "BMP Native Metadata", 114 "com.sun.imageio.plugins.png.PNGMetadataFormat", 115 null, null 116 ); 117 } 118 119 public String getDescription(Locale locale) { 120 return "Standard BMP image reader"; 121 } 122 123 public boolean canDecodeInput(Object input) throws IOException { 124 if (!(input instanceof ImageInputStream)) { 125 return false; 126 } 127 128 ImageInputStream stream = (ImageInputStream)input; 129 byte[] b = new byte[8]; 130 stream.mark(); 131 stream.readFully(b); 132 stream.reset(); 133 134 return (b[0] == (byte)137 && 135 b[1] == (byte)80 && 136 b[2] == (byte)78 && 137 b[3] == (byte)71 && 138 b[4] == (byte)13 && 139 b[5] == (byte)10 && 140 b[6] == (byte)26 && 141 b[7] == (byte)10); 142 } 143 144 public ImageReader createReaderInstance(Object extension) { 145 //return new PNGImageReader(this); 146 return null; 147 } 148 public void onRegistration(ServiceRegistry sr, Class<?> category) { 149 System.out.println("\nfrom OnRegistration: BMP plugin Registered\n"); 150 super.onRegistration(sr, category); 151 } 152 153 public void onDeregistration(ServiceRegistry sr, Class<?> category) { 154 System.out.println("\nfrom OnDeregistration: BMP plugin De-Registered\n"); 155 //super.onRegistration(sr, category); 156 } 157 } 158 }