@ -38,9 +38,11 @@ import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageInputStream ;
import javax.imageio.stream.MemoryCacheImageInputStream ;
import javax.imageio.stream.MemoryCacheImageOutputStream ;
import javax.imageio.stream.MemoryCacheImageOutputStream ;
import org.springframework.http.HttpHeaders ;
import org.springframework.http.HttpInputMessage ;
import org.springframework.http.HttpInputMessage ;
import org.springframework.http.HttpOutputMessage ;
import org.springframework.http.HttpOutputMessage ;
import org.springframework.http.MediaType ;
import org.springframework.http.MediaType ;
import org.springframework.http.StreamingHttpOutputMessage ;
import org.springframework.util.Assert ;
import org.springframework.util.Assert ;
import org.springframework.util.StringUtils ;
import org.springframework.util.StringUtils ;
@ -203,7 +205,30 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
}
}
@Override
@Override
public void write ( BufferedImage image , MediaType contentType , HttpOutputMessage outputMessage )
public void write ( final BufferedImage image , final MediaType contentType ,
final HttpOutputMessage outputMessage )
throws IOException , HttpMessageNotWritableException {
if ( outputMessage instanceof StreamingHttpOutputMessage ) {
StreamingHttpOutputMessage streamingOutputMessage =
( StreamingHttpOutputMessage ) outputMessage ;
streamingOutputMessage . setBody ( new StreamingHttpOutputMessage . Body ( ) {
@Override
public void writeTo ( OutputStream outputStream ) throws IOException {
writeInternal ( image , contentType , outputMessage . getHeaders ( ) ,
outputStream ) ;
}
} ) ;
}
else {
writeInternal ( image , contentType , outputMessage . getHeaders ( ) ,
outputMessage . getBody ( ) ) ;
}
}
private void writeInternal ( BufferedImage image , MediaType contentType ,
HttpHeaders headers , OutputStream body )
throws IOException , HttpMessageNotWritableException {
throws IOException , HttpMessageNotWritableException {
if ( contentType = = null | | contentType . isWildcardType ( ) | | contentType . isWildcardSubtype ( ) ) {
if ( contentType = = null | | contentType . isWildcardType ( ) | | contentType . isWildcardSubtype ( ) ) {
@ -211,16 +236,16 @@ public class BufferedImageHttpMessageConverter implements HttpMessageConverter<B
}
}
Assert . notNull ( contentType ,
Assert . notNull ( contentType ,
"Count not determine Content-Type, set one using the 'defaultContentType' property" ) ;
"Count not determine Content-Type, set one using the 'defaultContentType' property" ) ;
outputMessage . getHeaders ( ) . setContentType ( contentType ) ;
headers . setContentType ( contentType ) ;
ImageOutputStream imageOutputStream = null ;
ImageOutputStream imageOutputStream = null ;
ImageWriter imageWriter = null ;
ImageWriter imageWriter = null ;
try {
try {
imageOutputStream = createImageOutputStream ( outputMessage . getBody ( ) ) ;
Iterator < ImageWriter > imageWriters = ImageIO . getImageWritersByMIMEType ( contentType . toString ( ) ) ;
Iterator < ImageWriter > imageWriters = ImageIO . getImageWritersByMIMEType ( contentType . toString ( ) ) ;
if ( imageWriters . hasNext ( ) ) {
if ( imageWriters . hasNext ( ) ) {
imageWriter = imageWriters . next ( ) ;
imageWriter = imageWriters . next ( ) ;
ImageWriteParam iwp = imageWriter . getDefaultWriteParam ( ) ;
ImageWriteParam iwp = imageWriter . getDefaultWriteParam ( ) ;
process ( iwp ) ;
process ( iwp ) ;
imageOutputStream = createImageOutputStream ( body ) ;
imageWriter . setOutput ( imageOutputStream ) ;
imageWriter . setOutput ( imageOutputStream ) ;
imageWriter . write ( null , new IIOImage ( image , null , null ) , iwp ) ;
imageWriter . write ( null , new IIOImage ( image , null , null ) , iwp ) ;
}
}