From a2680f4c4a48c493c5fcc26bef9463ff886dabb0 Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Tue, 8 Sep 2015 02:23:56 +0900 Subject: [PATCH] Read large data using InputStreamResource at ResourceMessageConverter Issue: SPR-13443 --- .../ResourceHttpMessageConverter.java | 10 ++++++--- .../ResourceHttpMessageConverterTests.java | 22 +++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java index 9eebbbf322f2..d06b66475e60 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java @@ -40,6 +40,7 @@ * If JAF is not available, {@code application/octet-stream} is used. * * @author Arjen Poutsma + * @author Kazuki Shimizu * @since 3.0.2 */ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter { @@ -61,9 +62,12 @@ protected boolean supports(Class clazz) { @Override protected Resource readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - - byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody()); - return new ByteArrayResource(body); + if (InputStreamResource.class == clazz){ + return new InputStreamResource(inputMessage.getBody()); + } else { + byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody()); + return new ByteArrayResource(body); + } } @Override diff --git a/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java index 6e57854f2e35..0a4a5571079c 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/ResourceHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.http.converter; import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; import org.junit.Before; @@ -24,6 +25,7 @@ import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.http.MockHttpInputMessage; @@ -31,9 +33,12 @@ import org.springframework.util.FileCopyUtils; import static org.junit.Assert.*; +import static org.hamcrest.core.Is.*; +import static org.hamcrest.core.IsInstanceOf.*; /** * @author Arjen Poutsma + * @author Kazuki Shimizu */ public class ResourceHttpMessageConverterTests { @@ -60,7 +65,20 @@ public void read() throws IOException { byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg")); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); - converter.read(Resource.class, inputMessage); + Resource actualResource = converter.read(Resource.class, inputMessage); + assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream()), is(body)); + } + + // SPR-13443 + @Test + public void readWithInputStreamResource() throws IOException { + try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) { + MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); + inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); + Resource actualResource = converter.read(InputStreamResource.class, inputMessage); + assertThat(actualResource, instanceOf(InputStreamResource.class)); + assertThat(actualResource.getInputStream(), is(body)); + } } @Test