Package org.springframework.core.io

# Class InputStreamResource

java.lang.Object
org.springframework.core.io.AbstractResource
org.springframework.core.io.InputStreamResource

All Implemented Interfaces:
  `InputStreamSource`, `Resource`

public class InputStreamResource extends AbstractResource
`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.

NOTE: This class does not provide an independent `AbstractResource.contentLength()` implementation: Any such call will consume the given `InputStream`! Consider overriding `#contentLength()` with a custom implementation if possible. For any other purpose, it is not recommended to extend from this class; this is particularly true when used with Spring's web resource rendering which specifically skips `#contentLength()` for this exact class only.

Since:
  28.12.2003
Author:
  Juergen Hoeller, Sam Brannen
See Also:
  - `ByteArrayResource`
  - `ClassPathResource`
  - `FileSystemResource`
  - `UrlResource`

## Constructor Summary

Constructors
Constructor
Description
`InputStreamResource(InputStream inputStream)`
Create a new `InputStreamResource` for an existing `InputStream`.
`InputStreamResource(InputStream inputStream, String description)`
Create a new `InputStreamResource` for an existing `InputStream`.
`InputStreamResource(InputStreamSource inputStreamSource)`
Create a new `InputStreamResource` with a lazy `InputStream` for single use.
`InputStreamResource(InputStreamSource inputStreamSource, String description)`
Create a new `InputStreamResource` with a lazy `InputStream` for single use.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`equals(Object other)`
This implementation compares the underlying InputStream.
`boolean`
`exists()`
This implementation always returns `true`.
`String`
`getDescription()`
This implementation returns a description that includes the passed-in description, if any.
`InputStream`
`getInputStream()`
This implementation throws IllegalStateException if attempting to read the underlying stream multiple times.
`int`
`hashCode()`
This implementation returns the hash code of the underlying InputStream.
`boolean`
`isOpen()`
This implementation always returns `true`.

### Methods inherited from class org.springframework.core.io.AbstractResource

`contentLength, createRelative, getFile, getFileForLastModifiedCheck, getFilename, getURI, getURL, isFile, isReadable, lastModified, readableChannel, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.springframework.core.io.Resource

`getContentAsByteArray, getContentAsString`

## Constructor Details

### InputStreamResource

public InputStreamResource(InputStreamSource inputStreamSource)
Create a new `InputStreamResource` with a lazy `InputStream` for single use.

Parameters:
  `inputStreamSource` - an on-demand source for a single-use InputStream
Since:
  6.1.7

### InputStreamResource

public InputStreamResource(InputStreamSource inputStreamSource, @Nullable String description)
Create a new `InputStreamResource` with a lazy `InputStream` for single use.

Parameters:
  `inputStreamSource` - an on-demand source for a single-use InputStream
  `description` - where the InputStream comes from
Since:
  6.1.7

### InputStreamResource

public InputStreamResource(InputStream inputStream)
Create a new `InputStreamResource` for an existing `InputStream`.

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(InputStreamSource)`

### InputStreamResource

public InputStreamResource(InputStream inputStream, @Nullable String description)
Create a new `InputStreamResource` for an existing `InputStream`.

Parameters:
  `inputStream` - the InputStream to use
  `description` - where the InputStream comes from
See Also:
  - `InputStreamResource(InputStreamSource, String)`

## Method Details

### exists

public boolean exists()
This implementation always returns `true`.

Specified by:
  `exists` in interface `Resource`
Overrides:
  `exists` in class `AbstractResource`

### isOpen

public boolean isOpen()
This implementation always returns `true`.

Specified by:
  `isOpen` in interface `Resource`
Overrides:
  `isOpen` in class `AbstractResource`

### getInputStream

public InputStream getInputStream() throws IOException, IllegalStateException
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 exist
  `IOException` - if the content stream could not be opened
  `IllegalStateException`
See Also:
  - `Resource.isReadable()`
  - `Resource.isOpen()`

### getDescription

public String getDescription()
This implementation returns a description that includes the passed-in description, if any.

See Also:
  - `Object.toString()`

### equals

public boolean equals(@Nullable Object other)
This implementation compares the underlying InputStream.

Overrides:
  `equals` in class `AbstractResource`
See Also:
  - `Resource.getDescription()`

### hashCode

public int hashCode()
This implementation returns the hash code of the underlying InputStream.

Overrides:
  `hashCode` in class `AbstractResource`
See Also:
  - `Resource.getDescription()`