Class InputStreamResource
- All Implemented Interfaces:
InputStreamSource,Resource
Resource implementation for a given InputStream or a given
InputStreamSource (which can be supplied as a lambda expression)
for a lazy InputStream on demand.
Should only be used if no other specific Resource implementation
is applicable. In particular, prefer ByteArrayResource or any of the
file-based Resource implementations if possible. If you need to obtain
a custom stream multiple times, use a custom AbstractResource subclass
with a corresponding getInputStream() implementation.
In contrast to other Resource implementations, this is a descriptor
for an already opened resource - therefore returning true from
isOpen(). Do not use an InputStreamResource if you need to keep
the resource descriptor somewhere, or if you need to read from a stream multiple
times. This also applies when constructed with an InputStreamSource
which lazily obtains the stream but only allows for single access as well.
- Since:
- 28.12.2003
- Author:
- Juergen Hoeller, Sam Brannen
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionInputStreamResource(InputStream inputStream) Create a newInputStreamResourcefor an existingInputStream.InputStreamResource(InputStream inputStream, String description) Create a newInputStreamResourcefor an existingInputStream.InputStreamResource(InputStreamSource inputStreamSource) Create a newInputStreamResourcewith a lazyInputStreamfor single use.InputStreamResource(InputStreamSource inputStreamSource, String description) Create a newInputStreamResourcewith a lazyInputStreamfor single use. -
Method Summary
Modifier and TypeMethodDescriptionbooleanThis implementation compares the underlying InputStream.booleanexists()This implementation always returnstrue.This implementation returns a description that includes the passed-in description, if any.This implementation throws IllegalStateException if attempting to read the underlying stream multiple times.inthashCode()This implementation returns the hash code of the underlying InputStream.booleanisOpen()This implementation always returnstrue.Methods inherited from class org.springframework.core.io.AbstractResource
contentLength, createRelative, getFile, getFileForLastModifiedCheck, getFilename, getURI, getURL, isFile, isReadable, lastModified, readableChannel, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.core.io.Resource
getContentAsByteArray, getContentAsString
-
Constructor Details
-
InputStreamResource
Create a newInputStreamResourcewith a lazyInputStreamfor single use.- Parameters:
inputStreamSource- an on-demand source for a single-use InputStream- Since:
- 6.1.7
-
InputStreamResource
Create a newInputStreamResourcewith a lazyInputStreamfor single use.- Parameters:
inputStreamSource- an on-demand source for a single-use InputStreamdescription- where the InputStream comes from- Since:
- 6.1.7
-
InputStreamResource
Create a newInputStreamResourcefor an existingInputStream.Consider retrieving the InputStream on demand if possible, reducing its lifetime and reliably opening it and closing it through regular
InputStreamSource.getInputStream()usage.- Parameters:
inputStream- the InputStream to use- See Also:
-
InputStreamResource
Create a newInputStreamResourcefor an existingInputStream.- Parameters:
inputStream- the InputStream to usedescription- where the InputStream comes from- See Also:
-
-
Method Details
-
exists
public boolean exists()This implementation always returnstrue.- Specified by:
existsin interfaceResource- Overrides:
existsin classAbstractResource
-
isOpen
public boolean isOpen()This implementation always returnstrue.- Specified by:
isOpenin interfaceResource- Overrides:
isOpenin classAbstractResource
-
getInputStream
This implementation throws IllegalStateException if attempting to read the underlying stream multiple times.- Returns:
- the input stream for the underlying resource (must not be
null) - Throws:
FileNotFoundException- if the underlying resource does not existIOException- if the content stream could not be openedIllegalStateException- See Also:
-
getDescription
This implementation returns a description that includes the passed-in description, if any.- See Also:
-
equals
This implementation compares the underlying InputStream.- Overrides:
equalsin classAbstractResource- See Also:
-
hashCode
public int hashCode()This implementation returns the hash code of the underlying InputStream.- Overrides:
hashCodein classAbstractResource- See Also:
-