Resteasy docs does not explain who is responsible for closing streams passed to MultipartFormDataOutput
. Let's consider the following example:
WebTarget target = ClientBuilder.newClient().target("url");
MultipartFormDataOutput formData = new MultipartFormDataOutput();
FileInputStream fis1 = new FileInputStream(new File("/path/to/image1"));
FileInputStream fis2 = new FileInputStream(new File("/path/to/image2"));
formData.addFormData("image", fis1, MediaType.APPLICATION_OCTET_STREAM_TYPE);
formData.addFormData("image", fis2, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Entity<MultipartFormDataOutput> entity = Entity.entity(formData, MediaType.MULTIPART_FORM_DATA);
Response response = target.request().post(entity);
Will the fis1
and fis2
be closed by the resteasy or the user should take care of closing these streams?
So I can answer my question myself, hope someone will benefit from this.
Resteasy will close the passed stream. In my case, the InputStreamProvider
will take care of closing the FileInputStream
.
public void writeTo(InputStream inputStream, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException
{
LogMessages.LOGGER.debugf("Provider : %s, Method : writeTo", getClass().getName());
try
{
int c = inputStream.read();
if (c == -1)
{
httpHeaders.putSingle(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(0));
entityStream.write(new byte[0]); // fix RESTEASY-204
return;
}
else
entityStream.write(c);
ProviderHelper.writeTo(inputStream, entityStream);
}
finally
{
inputStream.close();
}
}