org.springframework.http.converter
Class BufferedImageHttpMessageConverter

java.lang.Object
  extended by org.springframework.http.converter.BufferedImageHttpMessageConverter
All Implemented Interfaces:
HttpMessageConverter<java.awt.image.BufferedImage>

public class BufferedImageHttpMessageConverter
extends java.lang.Object
implements HttpMessageConverter<java.awt.image.BufferedImage>

Implementation of HttpMessageConverter that can read and write BufferedImages.

By default, this converter can read all media types that are supported by the registered image readers, and writes using the media type of the first available registered image writer. This behavior can be overriden by setting the #setContentType(org.springframework.http.MediaType) contentType} properties.

If the cacheDir property is set to an existing directory, this converter will cache image data.

The process(ImageReadParam) and process(ImageWriteParam) template methods allow subclasses to override Image I/O parameters.

Since:
3.0
Author:
Arjen Poutsma

Field Summary
private  java.io.File cacheDir
           
private  MediaType defaultContentType
           
private  java.util.List<MediaType> readableMediaTypes
           
 
Constructor Summary
BufferedImageHttpMessageConverter()
           
 
Method Summary
 boolean canRead(java.lang.Class<?> clazz, MediaType mediaType)
          Indicates whether the given class can be read by this converter.
 boolean canWrite(java.lang.Class<?> clazz, MediaType mediaType)
          Indicates whether the given class can be written by this converter.
private  javax.imageio.stream.ImageInputStream createImageInputStream(java.io.InputStream is)
           
private  javax.imageio.stream.ImageOutputStream createImageOutputStream(java.io.OutputStream os)
           
 MediaType getDefaultContentType()
          Returns the default Content-Type to be used for writing.
 java.util.List<MediaType> getSupportedMediaTypes()
          Return the list of MediaType objects supported by this converter.
private  boolean isReadable(MediaType mediaType)
           
private  boolean isWritable(MediaType mediaType)
           
protected  void process(javax.imageio.ImageReadParam irp)
          Template method that allows for manipulating the ImageReadParam before it is used to read an image.
protected  void process(javax.imageio.ImageWriteParam iwp)
          Template method that allows for manipulating the ImageWriteParam before it is used to write an image.
 java.awt.image.BufferedImage read(java.lang.Class<? extends java.awt.image.BufferedImage> clazz, HttpInputMessage inputMessage)
          Read an object of the given type form the given input message, and returns it.
 void setCacheDir(java.io.File cacheDir)
          Sets the cache directory.
 void setDefaultContentType(MediaType defaultContentType)
          Sets the default Content-Type to be used for writing.
 void write(java.awt.image.BufferedImage image, MediaType contentType, HttpOutputMessage outputMessage)
          Write an given object to the given output message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

readableMediaTypes

private final java.util.List<MediaType> readableMediaTypes

defaultContentType

private MediaType defaultContentType

cacheDir

private java.io.File cacheDir
Constructor Detail

BufferedImageHttpMessageConverter

public BufferedImageHttpMessageConverter()
Method Detail

setDefaultContentType

public void setDefaultContentType(MediaType defaultContentType)
Sets the default Content-Type to be used for writing.

Throws:
java.lang.IllegalArgumentException - if the given content type is not supported by the Java Image I/O API

getDefaultContentType

public MediaType getDefaultContentType()
Returns the default Content-Type to be used for writing. Called when write(java.awt.image.BufferedImage, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage) is invoked without a specified content type parameter.


setCacheDir

public void setCacheDir(java.io.File cacheDir)
Sets the cache directory. If this property is set to an existing directory, this converter will cache image data.


canRead

public boolean canRead(java.lang.Class<?> clazz,
                       MediaType mediaType)
Description copied from interface: HttpMessageConverter
Indicates whether the given class can be read by this converter.

Specified by:
canRead in interface HttpMessageConverter<java.awt.image.BufferedImage>
Parameters:
clazz - the class to test for readability
mediaType - the media type to read, can be null if not specified. Typically the value of a Content-Type header.
Returns:
true if readable; false otherwise

isReadable

private boolean isReadable(MediaType mediaType)

canWrite

public boolean canWrite(java.lang.Class<?> clazz,
                        MediaType mediaType)
Description copied from interface: HttpMessageConverter
Indicates whether the given class can be written by this converter.

Specified by:
canWrite in interface HttpMessageConverter<java.awt.image.BufferedImage>
Parameters:
clazz - the class to test for writability
mediaType - the media type to write, can be null if not specified. Typically the value of an Accept header.
Returns:
true if writable; false otherwise

isWritable

private boolean isWritable(MediaType mediaType)

getSupportedMediaTypes

public java.util.List<MediaType> getSupportedMediaTypes()
Description copied from interface: HttpMessageConverter
Return the list of MediaType objects supported by this converter.

Specified by:
getSupportedMediaTypes in interface HttpMessageConverter<java.awt.image.BufferedImage>
Returns:
the list of supported media types

read

public java.awt.image.BufferedImage read(java.lang.Class<? extends java.awt.image.BufferedImage> clazz,
                                         HttpInputMessage inputMessage)
                                  throws java.io.IOException,
                                         HttpMessageNotReadableException
Description copied from interface: HttpMessageConverter
Read an object of the given type form the given input message, and returns it.

Specified by:
read in interface HttpMessageConverter<java.awt.image.BufferedImage>
Parameters:
clazz - the type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.
inputMessage - the HTTP input message to read from
Returns:
the converted object
Throws:
java.io.IOException - in case of I/O errors
HttpMessageNotReadableException - in case of conversion errors

createImageInputStream

private javax.imageio.stream.ImageInputStream createImageInputStream(java.io.InputStream is)
                                                              throws java.io.IOException
Throws:
java.io.IOException

write

public void write(java.awt.image.BufferedImage image,
                  MediaType contentType,
                  HttpOutputMessage outputMessage)
           throws java.io.IOException,
                  HttpMessageNotWritableException
Description copied from interface: HttpMessageConverter
Write an given object to the given output message.

Specified by:
write in interface HttpMessageConverter<java.awt.image.BufferedImage>
Parameters:
image - the object to write to the output message. The type of this object must have previously been passed to the canWrite method of this interface, which must have returned true.
contentType - the content type to use when writing. May be null to indicate that the default content type of the converter must be used. If not null, this media type must have previously been passed to the canWrite method of this interface, which must have returned true.
outputMessage - the message to write to
Throws:
java.io.IOException - in case of I/O errors
HttpMessageNotWritableException - in case of conversion errors

createImageOutputStream

private javax.imageio.stream.ImageOutputStream createImageOutputStream(java.io.OutputStream os)
                                                                throws java.io.IOException
Throws:
java.io.IOException

process

protected void process(javax.imageio.ImageReadParam irp)
Template method that allows for manipulating the ImageReadParam before it is used to read an image.

Default implementation is empty.


process

protected void process(javax.imageio.ImageWriteParam iwp)
Template method that allows for manipulating the ImageWriteParam before it is used to write an image.

Default implementation is empty.