1. /*
  2. * @(#)JPEGImageReaderSpi.java 1.6 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.imageio.plugins.jpeg;
  8. import java.util.Locale;
  9. import javax.imageio.spi.ImageReaderSpi;
  10. import javax.imageio.stream.ImageInputStream;
  11. import javax.imageio.spi.IIORegistry;
  12. import javax.imageio.spi.ServiceRegistry;
  13. import java.io.IOException;
  14. import javax.imageio.ImageReader;
  15. import javax.imageio.IIOException;
  16. public class JPEGImageReaderSpi extends ImageReaderSpi {
  17. private static String [] writerSpiNames =
  18. {"com.sun.imageio.plugins.jpeg.JPEGImageWriterSpi"};
  19. private boolean registered = false;
  20. public JPEGImageReaderSpi() {
  21. super(JPEG.vendor,
  22. JPEG.version,
  23. JPEG.names,
  24. JPEG.suffixes,
  25. JPEG.MIMETypes,
  26. "com.sun.imageio.plugins.jpeg.JPEGImageReader",
  27. STANDARD_INPUT_TYPE,
  28. writerSpiNames,
  29. true,
  30. JPEG.nativeStreamMetadataFormatName,
  31. JPEG.nativeStreamMetadataFormatClassName,
  32. null, null,
  33. true,
  34. JPEG.nativeImageMetadataFormatName,
  35. JPEG.nativeImageMetadataFormatClassName,
  36. null, null
  37. );
  38. }
  39. public void onRegistration(ServiceRegistry registry,
  40. Class category) {
  41. if (registered) {
  42. return;
  43. }
  44. try {
  45. java.security.AccessController.doPrivileged(
  46. new sun.security.action.LoadLibraryAction("jpeg"));
  47. // Stuff it all into one lib for first pass
  48. //java.security.AccessController.doPrivileged(
  49. //new sun.security.action.LoadLibraryAction("imageioIJG"));
  50. } catch (Throwable e) { // Fail on any Throwable
  51. // if it can't be loaded, deregister and return
  52. registry.deregisterServiceProvider(this);
  53. return;
  54. }
  55. registered = true;
  56. }
  57. public String getDescription(Locale locale) {
  58. return "Standard JPEG Image Reader";
  59. }
  60. public boolean canDecodeInput(Object source) throws IOException {
  61. if (!(source instanceof ImageInputStream)) {
  62. return false;
  63. }
  64. ImageInputStream iis = (ImageInputStream) source;
  65. iis.mark();
  66. // If the first two bytes are a JPEG SOI marker, it's probably
  67. // a JPEG file. If they aren't, it definitely isn't a JPEG file.
  68. int byte1 = iis.read();
  69. int byte2 = iis.read();
  70. iis.reset();
  71. if ((byte1 == 0xFF) && (byte2 == JPEG.SOI)) {
  72. return true;
  73. }
  74. return false;
  75. }
  76. public ImageReader createReaderInstance(Object extension)
  77. throws IIOException {
  78. return new JPEGImageReader(this);
  79. }
  80. }