0

I want to implement a java interface HttpServletResponseWrapper and I got an answer in StackOverflow (Looking for an example for inserting content into the response using a servlet filter) in this answer we create a ByteArrayPrintWriter, but in this code why we don't close the ByteArrayPrintWriter stream, is there any problem in this way? thank you for any help.

xiaoxu
  • 11
  • 6

1 Answers1

0

In this particular case it's not necessary to close the ByteArrayOutputStream since we know the internal implementation has no buffers or anything that would require a close() call. The bytes are written to the backing array directly, and closing is a NO-OP.

/**
 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
 * this class can be called after the stream has been closed without
 * generating an <tt>IOException</tt>.
 */
public void close() throws IOException {
}

It might be a good idea to still call close() for consistency's sake, but that's your choice. Either way is fine.

Kayaman
  • 72,141
  • 5
  • 83
  • 121