Class BufferedImageHttpMessageConverter
- java.lang.Object
- org.springframework.http.converter.BufferedImageHttpMessageConverter
- All Implemented Interfaces:
HttpMessageConverter<BufferedImage>
public class BufferedImageHttpMessageConverter extends Object implements HttpMessageConverter<BufferedImage>
Implementation ofHttpMessageConverterthat can read and writeBufferedImages.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. The latter can be overridden by setting the
defaultContentTypeproperty.If the
cacheDirproperty is set, this converter will cache image data.The
process(ImageReadParam)andprocess(ImageWriteParam)template methods allow subclasses to override Image I/O parameters.- Since:
- 3.0
- Author:
- Arjen Poutsma
Constructor Summary
Constructors Constructor Description BufferedImageHttpMessageConverter()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanRead(Class<?> clazz, MediaType mediaType)Indicates whether the given class can be read by this converter.booleancanWrite(Class<?> clazz, MediaType mediaType)Indicates whether the given class can be written by this converter.MediaTypegetDefaultContentType()Returns the defaultContent-Typeto be used for writing.List<MediaType>getSupportedMediaTypes()Return the list ofMediaTypeobjects supported by this converter.protected voidprocess(ImageReadParam irp)Template method that allows for manipulating theImageReadParambefore it is used to read an image.protected voidprocess(ImageWriteParam iwp)Template method that allows for manipulating theImageWriteParambefore it is used to write an image.BufferedImageread(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage)Read an object of the given type from the given input message, and returns it.voidsetCacheDir(File cacheDir)Sets the cache directory.voidsetDefaultContentType(MediaType defaultContentType)Sets the defaultContent-Typeto be used for writing.voidwrite(BufferedImage image, MediaType contentType, HttpOutputMessage outputMessage)Write an given object to the given output message.
Constructor Detail
BufferedImageHttpMessageConverter
public BufferedImageHttpMessageConverter()
Method Detail
setDefaultContentType
public void setDefaultContentType(MediaType defaultContentType)
Sets the defaultContent-Typeto be used for writing.- Throws:
IllegalArgumentException- if the given content type is not supported by the Java Image I/O API
getDefaultContentType
public MediaType getDefaultContentType()
Returns the defaultContent-Typeto be used for writing. Called whenwrite(java.awt.image.BufferedImage, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)is invoked without a specified content type parameter.
setCacheDir
public void setCacheDir(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(Class<?> clazz, MediaType mediaType)
Description copied from interface:HttpMessageConverterIndicates whether the given class can be read by this converter.- Specified by:
canReadin interfaceHttpMessageConverter<BufferedImage>- Parameters:
clazz- the class to test for readabilitymediaType- the media type to read (can benullif not specified); typically the value of aContent-Typeheader.- Returns:
trueif readable;falseotherwise
canWrite
public boolean canWrite(Class<?> clazz, MediaType mediaType)
Description copied from interface:HttpMessageConverterIndicates whether the given class can be written by this converter.- Specified by:
canWritein interfaceHttpMessageConverter<BufferedImage>- Parameters:
clazz- the class to test for writabilitymediaType- the media type to write (can benullif not specified); typically the value of anAcceptheader.- Returns:
trueif writable;falseotherwise
getSupportedMediaTypes
public List<MediaType> getSupportedMediaTypes()
Description copied from interface:HttpMessageConverterReturn the list ofMediaTypeobjects supported by this converter.- Specified by:
getSupportedMediaTypesin interfaceHttpMessageConverter<BufferedImage>- Returns:
- the list of supported media types, potentially an immutable copy
read
public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
Description copied from interface:HttpMessageConverterRead an object of the given type from the given input message, and returns it.- Specified by:
readin interfaceHttpMessageConverter<BufferedImage>- Parameters:
clazz- the type of object to return. This type must have previously been passed to thecanReadmethod of this interface, which must have returnedtrue.inputMessage- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException- in case of I/O errorsHttpMessageNotReadableException- in case of conversion errors
write
public void write(BufferedImage image, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
Description copied from interface:HttpMessageConverterWrite an given object to the given output message.- Specified by:
writein interfaceHttpMessageConverter<BufferedImage>- Parameters:
image- the object to write to the output message. The type of this object must have previously been passed to thecanWritemethod of this interface, which must have returnedtrue.contentType- the content type to use when writing. May benullto indicate that the default content type of the converter must be used. If notnull, this media type must have previously been passed to thecanWritemethod of this interface, which must have returnedtrue.outputMessage- the message to write to- Throws:
IOException- in case of I/O errorsHttpMessageNotWritableException- in case of conversion errors
process
protected void process(ImageReadParam irp)
Template method that allows for manipulating theImageReadParambefore it is used to read an image.The default implementation is empty.
process
protected void process(ImageWriteParam iwp)
Template method that allows for manipulating theImageWriteParambefore it is used to write an image.The default implementation is empty.