Class InputStreamContent (1.42.3)

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
NameDescription
typeString

Content type or null for none

inputStreamInputStream

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
TypeDescription
InputStream
Overrides

getLength()

public long getLength()
Returns
TypeDescription
long

retrySupported()

public boolean retrySupported()
Returns
TypeDescription
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
NameDescription
closeInputStreamboolean
Returns
TypeDescription
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
NameDescription
lengthlong
Returns
TypeDescription
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
NameDescription
retrySupportedboolean
Returns
TypeDescription
InputStreamContent

setType(String type)

public InputStreamContent setType(String type)

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

Parameter
NameDescription
typeString
Returns
TypeDescription
InputStreamContent
Overrides