diff --git a/spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java b/spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java index eeb307cfcdf..4e9552a650a 100644 --- a/spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java +++ b/spring-core/src/main/java/org/springframework/core/codec/ResourceDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -83,6 +83,10 @@ public class ResourceDecoder extends AbstractDataBufferDecoder { public String getFilename() { return filename; } + @Override + public long contentLength() { + return bytes.length; + } }; } else if (Resource.class.isAssignableFrom(clazz)) { diff --git a/spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java b/spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java index 1c3a531f7a3..f060ce4f359 100644 --- a/spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java +++ b/spring-core/src/test/java/org/springframework/core/codec/ResourceDecoderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -82,10 +82,7 @@ class ResourceDecoderTests extends AbstractDecoderTests { @Override @Test public void decodeToMono() { - Flux input = Flux.concat( - dataBuffer(this.fooBytes), - dataBuffer(this.barBytes)); - + Flux input = Flux.concat(dataBuffer(this.fooBytes), dataBuffer(this.barBytes)); testDecodeToMonoAll(input, ResolvableType.forClass(Resource.class), step -> step .consumeNextWith(value -> { @@ -105,4 +102,22 @@ class ResourceDecoderTests extends AbstractDecoderTests { Collections.singletonMap(ResourceDecoder.FILENAME_HINT, "testFile")); } + @Test + public void decodeInputStreamResource() { + Flux input = Flux.concat(dataBuffer(this.fooBytes), dataBuffer(this.barBytes)); + testDecodeAll(input, InputStreamResource.class, step -> step + .consumeNextWith(resource -> { + try { + byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream()); + assertThat(new String(bytes)).isEqualTo("foobar"); + assertThat(resource.contentLength()).isEqualTo(fooBytes.length + barBytes.length); + } + catch (IOException ex) { + throw new AssertionError(ex.getMessage(), ex); + } + }) + .expectComplete() + .verify()); + } + } 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 582ab859de1..f587aecf7ff 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -84,6 +84,11 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter