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.
Constructors
InputStreamContent(String type, InputStream inputStream)
public InputStreamContent(String type, InputStream inputStream)
Name | Description |
type | String Content type or |
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.
Type | Description |
InputStream |
getLength()
public long getLength()
Type | Description |
long |
retrySupported()
public boolean retrySupported()
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.
Name | Description |
closeInputStream | boolean |
Type | Description |
InputStreamContent |
setLength(long length)
public InputStreamContent setLength(long length)
Sets the content length or less than zero if not known.
Defaults to -1
.
Name | Description |
length | long |
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.
Name | Description |
retrySupported | boolean |
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.
Name | Description |
type | String |
Type | Description |
InputStreamContent |