Class InputStreamContent (1.45.2)

public final class InputStreamContent extends AbstractInputStreamContent

Concrete implementation of AbstractInputStreamContent that simply handles the transfer of data from an input stream to an output stream. This should only be used for streams that can not be re-opened and retried. If you have a stream that it is possible to recreate please create a new subclass of AbstractInputStreamContent.

The input stream is guaranteed to be closed at the end of #writeTo(OutputStream).

Sample use with a URL:

 
 private static void setRequestJpegContent(HttpRequest request, URL jpegUrl) throws IOException {
 request.setContent(new InputStreamContent("image/jpeg", jpegUrl.openStream()));
 }
 
 

Implementation is not thread-safe.

Inheritance

java.lang.Object > AbstractInputStreamContent > InputStreamContent

Constructors

InputStreamContent(String type, InputStream inputStream)

public InputStreamContent(String type, InputStream inputStream)
Parameters
Name Description
type String

Content type or null for none

inputStream InputStream

Input stream to read from

Methods

getInputStream()

public InputStream getInputStream()

Return an input stream for the specific implementation type of AbstractInputStreamContent. If the specific implementation will return true for #retrySupported() this should be a factory function which will create a new InputStream from the source data whenever invoked.

Returns
Type Description
InputStream
Overrides

getLength()

public long getLength()
Returns
Type Description
long

retrySupported()

public boolean retrySupported()
Returns
Type Description
boolean

setCloseInputStream(boolean closeInputStream)

public InputStreamContent setCloseInputStream(boolean closeInputStream)

Sets whether the input stream should be closed at the end of #writeTo. Default is true. Subclasses should override by calling super.

Parameter
Name Description
closeInputStream boolean
Returns
Type Description
InputStreamContent
Overrides

setLength(long length)

public InputStreamContent setLength(long length)

Sets the content length or less than zero if not known.

Defaults to -1.

Parameter
Name Description
length long
Returns
Type Description
InputStreamContent

setRetrySupported(boolean retrySupported)

public InputStreamContent setRetrySupported(boolean retrySupported)

Sets whether or not retry is supported. Defaults to false.

Should be set to true if #getInputStream is called to reset to the original position of the input stream.

Parameter
Name Description
retrySupported boolean
Returns
Type Description
InputStreamContent

setType(String type)

public InputStreamContent setType(String type)

Sets the content type or null for none. Subclasses should override by calling super.

Parameter
Name Description
type String
Returns
Type Description
InputStreamContent
Overrides