Top Description Fields Constructors Methods
org.springframework.http.converter

public Class BufferedImageHttpMessageConverter

extends Object
implements HttpMessageConverter<BufferedImage>
Class Inheritance
All Implemented Interfaces
org.springframework.http.converter.HttpMessageConverter
Imports
java.awt.image.BufferedImage, java.io.File, .IOException, .InputStream, .OutputStream, java.util.ArrayList, .Collections, .Iterator, .List, javax.imageio.IIOImage, .ImageIO, .ImageReadParam, .ImageReader, .ImageWriteParam, .ImageWriter, javax.imageio.stream.FileCacheImageInputStream, .FileCacheImageOutputStream, .ImageInputStream, .ImageOutputStream, .MemoryCacheImageInputStream, .MemoryCacheImageOutputStream, org.springframework.http.HttpInputMessage, .HttpOutputMessage, .MediaType, .StreamingHttpOutputMessage, org.springframework.lang.Nullable, org.springframework.util.Assert, .StreamUtils, .StringUtils

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.

Author
Arjen Poutsma
Since
3.0

Field Summary

Modifier and TypeField and Description
private File
private MediaType
private final List<MediaType>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
canRead(Class<?>
the class to test for readability
clazz
,
MediaType
the media type to read (can be null if not specified); typically the value of a Content-Type header.
mediaType
)

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

Indicates whether the given class can be read by this converter.
public boolean
canWrite(Class<?>
the class to test for writability
clazz
,
MediaType
the media type to write (can be null if not specified); typically the value of an Accept header.
mediaType
)

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

Indicates whether the given class can be written by this converter.
private ImageInputStream
private ImageOutputStream
private MediaType
public MediaType
getDefaultContentType()

Returns the default Content-Type to be used for writing.

public List<MediaType>
getSupportedMediaTypes()

Implements org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

Return the list of media types supported by this converter.
private boolean
isReadable(MediaType mediaType)

private boolean
isWritable(MediaType mediaType)

protected void
process(ImageReadParam irp)

Template method that allows for manipulating the ImageReadParam before it is used to read an image.

protected void
process(ImageWriteParam iwp)

Template method that allows for manipulating the ImageWriteParam before it is used to write an image.

public BufferedImage
read(Class<? extends BufferedImage>
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.
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Implements org.springframework.http.converter.HttpMessageConverter.read.

Read an object of the given type from the given input message, and returns it.
public void
setCacheDir(File cacheDir)

Sets the cache directory.

public void
setDefaultContentType(MediaType defaultContentType)

Sets the default Content-Type to be used for writing.

public void
write(final BufferedImage
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.
image
,
final MediaType
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.
contentType
,
final HttpOutputMessage
the message to write to
outputMessage
)

Implements org.springframework.http.converter.HttpMessageConverter.write.

Write a given object to the given output message.
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheDirback to summary
private File cacheDir
Annotations
@Nullable
defaultContentTypeback to summary
private MediaType defaultContentType
Annotations
@Nullable
readableMediaTypesback to summary
private final List<MediaType> readableMediaTypes

Constructor Detail

BufferedImageHttpMessageConverterback to summary
public BufferedImageHttpMessageConverter()

Method Detail

canReadback to summary
public boolean canRead(Class<?> clazz, MediaType mediaType)

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

Indicates whether the given class can be read by this converter.

Parameters
clazz:Class<?>

the class to test for readability

mediaType:MediaType

the media type to read (can be null if not specified); typically the value of a Content-Type header.

Returns:boolean

true if readable; false otherwise

Annotations
@Override
canWriteback to summary
public boolean canWrite(Class<?> clazz, MediaType mediaType)

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

Indicates whether the given class can be written by this converter.

Parameters
clazz:Class<?>

the class to test for writability

mediaType:MediaType

the media type to write (can be null if not specified); typically the value of an Accept header.

Returns:boolean

true if writable; false otherwise

Annotations
@Override
createImageInputStreamback to summary
private ImageInputStream createImageInputStream(InputStream is) throws IOException
createImageOutputStreamback to summary
private ImageOutputStream createImageOutputStream(OutputStream os) throws IOException
getContentTypeback to summary
private MediaType getContentType(MediaType contentType)
getDefaultContentTypeback to summary
public MediaType getDefaultContentType()

Returns the default Content-Type to be used for writing. Called when write is invoked without a specified content type parameter.

Annotations
@Nullable
getSupportedMediaTypesback to summary
public List<MediaType> getSupportedMediaTypes()

Implements org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

Doc from org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

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 via canWrite(clazz, null. The list may also exclude MIME types supported only for a specific class. Alternatively, use getSupportedMediaTypes(Class) for a more precise list.

Returns:List<MediaType>

the list of supported media types

Annotations
@Override
isReadableback to summary
private boolean isReadable(MediaType mediaType)
isWritableback to summary
private boolean isWritable(MediaType mediaType)
processback to summary
protected void process(ImageReadParam irp)

Template method that allows for manipulating the ImageReadParam before it is used to read an image.

The default implementation is empty.

processback to summary
protected void process(ImageWriteParam iwp)

Template method that allows for manipulating the ImageWriteParam before it is used to write an image.

The default implementation is empty.

readback to summary
public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

Implements org.springframework.http.converter.HttpMessageConverter.read.

Doc from org.springframework.http.converter.HttpMessageConverter.read.

Read an object of the given type from the given input message, and returns it.

Parameters
clazz:Class<? extends BufferedImage>

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:HttpInputMessage

the HTTP input message to read from

Returns:BufferedImage

the converted object

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotReadableException:
in case of conversion errors
setCacheDirback to summary
public void setCacheDir(File cacheDir)

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

setDefaultContentTypeback to summary
public void setDefaultContentType(MediaType defaultContentType)

Sets the default Content-Type to be used for writing.

Exceptions
IllegalArgumentException:
if the given content type is not supported by the Java Image I/O API
writeback to summary
public void write(final BufferedImage image, final MediaType contentType, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException

Implements org.springframework.http.converter.HttpMessageConverter.write.

Doc from org.springframework.http.converter.HttpMessageConverter.write.

Write a given object to the given output message.

Parameters
image:BufferedImage

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:MediaType

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:HttpOutputMessage

the message to write to

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotWritableException:
in case of conversion errors
writeInternalback to summary
private void writeInternal(BufferedImage image, MediaType contentType, OutputStream body) throws IOException, HttpMessageNotWritableException