Class BufferedImageHttpMessageConverter
java.lang.Object
org.springframework.http.converter.BufferedImageHttpMessageConverter
- All Implemented Interfaces:
HttpMessageConverter<BufferedImage>
public class BufferedImageHttpMessageConverter
extends Object
implements HttpMessageConverter<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.
The latter can be overridden by setting the
defaultContentType
property.
If the cacheDir
property is set, 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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Indicates whether the given class can be read by this converter.boolean
Indicates whether the given class can be written by this converter.Returns the defaultContent-Type
to be used for writing.Return the list of media types supported by this converter.protected void
process
(ImageReadParam irp) Template method that allows for manipulating theImageReadParam
before it is used to read an image.protected void
process
(ImageWriteParam iwp) Template method that allows for manipulating theImageWriteParam
before it is used to write an image.read
(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) Read an object of the given type from the given input message, and returns it.void
setCacheDir
(File cacheDir) Sets the cache directory.void
setDefaultContentType
(MediaType defaultContentType) Sets the defaultContent-Type
to be used for writing.void
write
(BufferedImage image, MediaType contentType, HttpOutputMessage outputMessage) Write a 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
Methods inherited from interface org.springframework.http.converter.HttpMessageConverter
getSupportedMediaTypes
-
Constructor Details
-
BufferedImageHttpMessageConverter
public BufferedImageHttpMessageConverter()
-
-
Method Details
-
setDefaultContentType
Sets the defaultContent-Type
to be used for writing.- Throws:
IllegalArgumentException
- if the given content type is not supported by the Java Image I/O API
-
getDefaultContentType
Returns the defaultContent-Type
to 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
Sets the cache directory. If this property is set to an existing directory, this converter will cache image data. -
canRead
Description copied from interface:HttpMessageConverter
Indicates whether the given class can be read by this converter.- Specified by:
canRead
in interfaceHttpMessageConverter<BufferedImage>
- Parameters:
clazz
- the class to test for readabilitymediaType
- the media type to read (can benull
if not specified); typically the value of aContent-Type
header.- Returns:
true
if readable;false
otherwise
-
canWrite
Description copied from interface:HttpMessageConverter
Indicates whether the given class can be written by this converter.- Specified by:
canWrite
in interfaceHttpMessageConverter<BufferedImage>
- Parameters:
clazz
- the class to test for writabilitymediaType
- the media type to write (can benull
if not specified); typically the value of anAccept
header.- Returns:
true
if writable;false
otherwise
-
getSupportedMediaTypes
Description copied from interface:HttpMessageConverter
Return the list of media types supported by this converter. The list may not apply to every possible target element type and calls to this method should typically be guarded viacanWrite(clazz, null
. The list may also exclude MIME types supported only for a specific class. Alternatively, useHttpMessageConverter.getSupportedMediaTypes(Class)
for a more precise list.- Specified by:
getSupportedMediaTypes
in interfaceHttpMessageConverter<BufferedImage>
- Returns:
- the list of supported media types
-
read
public BufferedImage read(@Nullable Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from interface:HttpMessageConverter
Read an object of the given type from the given input message, and returns it.- Specified by:
read
in interfaceHttpMessageConverter<BufferedImage>
- Parameters:
clazz
- the type of object to return. This type must have previously been passed to thecanRead
method 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, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException Description copied from interface:HttpMessageConverter
Write a given object to the given output message.- Specified by:
write
in interfaceHttpMessageConverter<BufferedImage>
- Parameters:
image
- the object to write to the output message. The type of this object must have previously been passed to thecanWrite
method of this interface, which must have returnedtrue
.contentType
- the content type to use when writing. May benull
to indicate that the default content type of the converter must be used. If notnull
, this media type must have previously been passed to thecanWrite
method 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
Template method that allows for manipulating theImageReadParam
before it is used to read an image.The default implementation is empty.
-
process
Template method that allows for manipulating theImageWriteParam
before it is used to write an image.The default implementation is empty.
-