As a feature request, would it make sense to extend Resource interface to allow a getBytes[] method so one does not have to deal with inputstreams, closing them etc... I mean from conceptual point of view, a resource is an abstraction of a byte content (even if a particular implementation is not handling physically that byte content), and as so it could make sense to de-encapsulate it easily. If someone needs the content it just autowires it in a fancy semantical way like @Value Resource, and then can parse it easily with a new convenient String(resource.getBytes()), for instance, instead of having to stream it
Comment From: sbrannen
Thanks for the proposal.
The Resource
abstraction is intentionally a low-level API with minimal surface area in terms of the number of methods defined in the interface, and we would like to keep it that way.
If you are interested in retrieving the contents of a resource as a String
, you likely would not want to concern yourself with an intermediate byte[]
anyway. For such use cases, we recommend the use of a utility class that can convert from an InputStream
into a String
-- for example Spring's FileCopyUtils
or preferably a third-party utility such as Apache Commons and similar libraries.
Another option would be to invoke Resource#readableChannel()
to read()
into a CharBuffer
.
In light of the above I am closing this issue.