1 /*
   2  * Copyright (c) 2015, 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 package simp;
  25 
  26 import java.io.IOException;
  27 import java.util.Locale;
  28 import javax.imageio.IIOException;
  29 import javax.imageio.ImageReader;
  30 import javax.imageio.spi.ImageReaderSpi;
  31 import javax.imageio.stream.ImageInputStream;
  32 
  33 public class SIMPImageReaderSpi extends ImageReaderSpi {
  34 
  35     private static final String[] FORMATNAMES = {"simp, SIMP"};
  36     private static final String[] SUFFIXES = {"simp"};
  37     private static final String[] MIMETYPES = {"image/simp"};
  38 
  39     public SIMPImageReaderSpi() {
  40         super("J Duke",                   // vendor
  41               "1.0",                      // version
  42                FORMATNAMES,               // format names
  43                SUFFIXES,                  // file suffixes
  44                MIMETYPES,                 // mimetypes
  45                "simp.SIMPImageReader",    // reader class name
  46                new Class<?>[] { ImageInputStream.class }, // input types
  47                null,              // writer class names. TBD.
  48                true,              // supports native metadata,
  49                null,              // [no] native stream metadata format
  50                null,              // [no] native stream metadata class
  51                null,              // [no] native extra stream metadata format
  52                null,              // [no] native extra stream metadata class
  53                true,              // supports standard metadata,
  54                "simp_metadata_1.0",       // metadata format name,
  55                "simp.SIMPMetadataFormat", // metadata format class name
  56                null,              // [no] extra image metadata format
  57                null               // [no] extra image metadata format class
  58          );
  59          System.out.println("INIT SIMPImageReaderSpi");
  60     }
  61 
  62     public String getDescription(Locale locale) {
  63         return "SIMPle Image Format Reader";
  64     }
  65 
  66     public boolean canDecodeInput(Object source) throws IOException {
  67         if (!(source instanceof ImageInputStream)) {
  68             return false;
  69         }
  70         ImageInputStream stream = (ImageInputStream)source;
  71 
  72         stream.mark();
  73         try {
  74              byte[] sig = new byte[4];
  75              stream.read(sig);
  76              return sig[0]=='S' && sig[1]=='I' && sig[2]=='M' && sig[3]=='P';
  77         } finally {
  78             stream.reset();
  79         }
  80     }
  81 
  82     public ImageReader createReaderInstance(Object extension)
  83         throws IIOException {
  84         return new SIMPImageReader(this);
  85     }
  86 }