@Override
  public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage)
      throws IOException, HttpMessageNotReadableException {

    ImageInputStream imageInputStream = null;
    ImageReader imageReader = null;
    try {
      imageInputStream = createImageInputStream(inputMessage.getBody());
      MediaType contentType = inputMessage.getHeaders().getContentType();
      Iterator<ImageReader> imageReaders =
          ImageIO.getImageReadersByMIMEType(contentType.toString());
      if (imageReaders.hasNext()) {
        imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        process(irp);
        imageReader.setInput(imageInputStream, true);
        return imageReader.read(0, irp);
      } else {
        throw new HttpMessageNotReadableException(
            "Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
      }
    } finally {
      if (imageReader != null) {
        imageReader.dispose();
      }
      if (imageInputStream != null) {
        try {
          imageInputStream.close();
        } catch (IOException ex) {
          // ignore
        }
      }
    }
  }
 private boolean isReadable(MediaType mediaType) {
   if (mediaType == null) {
     return true;
   }
   Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(mediaType.toString());
   return imageReaders.hasNext();
 }
Ejemplo n.º 3
0
  private BufferedImage readImage() {
    Iterator<?> readersIterator = ImageIO.getImageReadersByMIMEType(fieldImageMineType);

    if (readersIterator.hasNext()) {
      ImageReader reader = (ImageReader) readersIterator.next();

      reader.setInput(
          new MemoryCacheImageInputStream(new ByteArrayInputStream(fieldOriginal)), true, true);

      try {
        BufferedImage image = reader.read(0);
        return image;
      } catch (Throwable th) {
        new JLogMessage(JLogLevel.ERROR, "Fiction book image {0} cannot be decoded")
            .log(th, getImageFileName());
      }
    }
    return null;
  }