You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Update advice on contentLength() in subclasses of InputStreamResource #20990
It seems the bug was introduced in #16633
org.springframework.http.converter.ResourceHttpMessageConverter#getContentLength
// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!if (InputStreamResource.class == resource.getClass()) {
returnnull;
}
In fact default behavior of InputStreamResource is not the same as for its children.
What specifically are you asking for here? I'm aware that such special handling is unusual but unfortunately there is no sensible way to determine the content length for a plain InputStreamResource. And for custom subclasses thereof, we expect contentLength() to be overridden.
Are you trying to implement such a custom InputStreamResource subclass and expose it to ResourceHttpMessageConverter? In most scenarios, it is preferable to implement a regular Resource with a fresh stream returned from getInputStream(), avoiding the InputStreamResource flaw of dealing with a pre-opened stream.
Actually I have to subclass InputStreamResource to extend metadata.
I was confused a lot when my application change its behavior after I switch InputStreamResource to MyInputStreamResource.
I got an exceptional cituation because InputStreamResource was read twice on contentLength and on getting inputStream.
I'm thinking about elimination this kind of InputStreamResource behavior if it is possible (I'm aware of legacy background).
Before #16633, InputStreamResource itself didn't work with ResourceHttpMessageConverter at all. From that perspective, we at least made that case work. Making it work for InputStreamResource subclasses as well is tough since we don't know whether contentLength() has been overridden there.
So for a custom InputStreamResource subclass, we strongly recommend a custom implementation of contentLength(): possibly simply returning -1, leading to the same behavior as for a standard InputStreamResource in ResourceHttpMessageConverter. I suppose we should at least document that part.
There is indeed a legacy problem here: specifically, that InputStreamResource may be used for content length determination, with contentLength() getting called but not getInputStream() itself. If it wasn't for that specific scenario, we could also let InputStreamResource itself implement contentLength() with a -1 return value.
changed the title [-]Consistent treatment of InputStreamResource subclasses [SPR-16445][/-][+]Update advice on contentLength() in subclasses of InputStreamResource [/+]on Mar 25, 2020
Activity
spring-projects-issues commentedon Jan 31, 2018
Juergen Hoeller commented
What specifically are you asking for here? I'm aware that such special handling is unusual but unfortunately there is no sensible way to determine the content length for a plain
InputStreamResource. And for custom subclasses thereof, we expectcontentLength()to be overridden.Are you trying to implement such a custom
InputStreamResourcesubclass and expose it toResourceHttpMessageConverter? In most scenarios, it is preferable to implement a regularResourcewith a fresh stream returned fromgetInputStream(), avoiding theInputStreamResourceflaw of dealing with a pre-opened stream.spring-projects-issues commentedon Jan 31, 2018
bademus commented
Thanks for quick answer,
Actually I have to subclass InputStreamResource to extend metadata.
I was confused a lot when my application change its behavior after I switch InputStreamResource to MyInputStreamResource.
I got an exceptional cituation because InputStreamResource was read twice on contentLength and on getting inputStream.
I'm thinking about elimination this kind of InputStreamResource behavior if it is possible (I'm aware of legacy background).
spring-projects-issues commentedon Jan 31, 2018
Juergen Hoeller commented
Before #16633,
InputStreamResourceitself didn't work withResourceHttpMessageConverterat all. From that perspective, we at least made that case work. Making it work forInputStreamResourcesubclasses as well is tough since we don't know whethercontentLength()has been overridden there.So for a custom
InputStreamResourcesubclass, we strongly recommend a custom implementation ofcontentLength(): possibly simply returning -1, leading to the same behavior as for a standardInputStreamResourceinResourceHttpMessageConverter. I suppose we should at least document that part.There is indeed a legacy problem here: specifically, that
InputStreamResourcemay be used for content length determination, withcontentLength()getting called but notgetInputStream()itself. If it wasn't for that specific scenario, we could also letInputStreamResourceitself implementcontentLength()with a -1 return value.[-]Consistent treatment of InputStreamResource subclasses [SPR-16445][/-][+]Update advice on contentLength() in subclasses of InputStreamResource [/+]dat4c0ded commentedon Apr 16, 2020
I encountered the same problem but this code below works for me.
Update Javadoc of InputStreamResource#contentLength