Announcement Announcement Module
Collapse
No announcement yet.
ResourceHttpMessageConverter.writeInternal doesn't close InputStream Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ResourceHttpMessageConverter.writeInternal doesn't close InputStream

    In Spring 3.2.2, ResourceHttpMessageConverter.writeInternal doesn't close the InputStream acquired by resource.getInputStream() on line 88 and is causing me an issue. Is this intentional? I have a JSF page which displays an image via a REST controller and when I update my record, I want to move the underlying file from temp -> perm storage an am not able to do so because the file is locked. I can think of a few possible solutions, but would prefer the one that is most elegant without wasting too much time.

    Code:
    84	@Override
    85	protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
    86			throws IOException, HttpMessageNotWritableException {
    87
    88		StreamUtils.copy(resource.getInputStream(), outputMessage.getBody());
    89		outputMessage.getBody().flush();
    90	}

  • #2
    After a little searching, I devised a simple solution...

    Code:
    import org.apache.commons.io.input.AutoCloseInputStream;
    
    class AutoClosingFileSystemResource extends FileSystemResource {
    
      public AutoClosingFileSystemResource(File file) {
        super(file);
      }
    
      @Override
      public InputStream getInputStream() throws IOException {
        return new AutoCloseInputStream(super.getInputStream());
      }
    }

    Comment

    Working...
    X