contentLength support for Resource decoding
Expose the known content length, if known, in the InputStreamResource returned by ResourceHttpMessageConverter or ResourceDecoder. See gh-24292
This commit is contained in:
committed by
Rossen Stoyanchev
parent
e3e7d90415
commit
e96b71acf3
@@ -83,6 +83,11 @@ public class ResourceDecoder extends AbstractDataBufferDecoder<Resource> {
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() {
|
||||
return bytes.length;
|
||||
}
|
||||
};
|
||||
}
|
||||
else if (Resource.class.isAssignableFrom(clazz)) {
|
||||
|
||||
@@ -105,4 +105,22 @@ class ResourceDecoderTests extends AbstractDecoderTests<ResourceDecoder> {
|
||||
Collections.singletonMap(ResourceDecoder.FILENAME_HINT, "testFile"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeInputStreamResource() {
|
||||
Flux<DataBuffer> 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user