HttpServletResponseImpl implements HttpServletResponse { public void flushHeaders() throws IllegalStateException, IOException { if (isCommitted) { throw new IllegalStateException("Headers should not be committed more than once."); } isCommitted = true; for (Cookie cookie : cookies) { headers.setHeader(Headers.HEADER_SET_COOKIE, cookieHeaderSerializer.serialize(cookie)); } byte[] head = (getStatus() + NEW_LINE + headersSerializer.serialize(headers)) .getBytes(StandardCharsets.UTF_8); InputStream inputStream = new ByteArrayInputStream(head); serveStream(inputStream); IOUtilities.closeSilently(inputStream); } HttpServletResponseImpl(final Serializer<Headers> headersSerializer,
                                   final Serializer<Cookie> cookieHeaderSerializer,
                                   final StreamHelper streamHelper,
                                   final OutputStream outputStream); @Override void addCookie(final Cookie cookie); @Override List<Cookie> getCookies(); @Override boolean isCommitted(); @Override void reset(); @Override void resetBuffer(); @Override void setBufferSize(final int bufferSize); @Override void setCharacterEncoding(final String charset); @Override void sendRedirect(final String location); @Override void setContentType(final String contentType); @Override void setLocale(final Locale loc); @Override void flushBuffer(); @Override int getBufferSize(); @Override String getCharacterEncoding(); @Override String getContentType(); @Override Locale getLocale(); @Override void setKeepAlive(final boolean keepAlive); @Override void setContentLength(final int length); @Override void setContentLength(final long length); @Override Headers getHeaders(); @Override void setStatus(final String status); @Override void setHeader(final String name, final String value); @Override void setIntHeader(final String name, final int value); @Override PrintWriter getWriter(); @Override ServletOutputStream getOutputStream(); void flushHeaders(); void serveStream(final InputStream inputStream); void serveStream(final InputStream inputStream, final Range range); void serveStream(final InputStream inputStream,
                            final List<Range> rangeList,
                            final String boundary,
                            final String contentType,
                            final long totalLength); String getStatus(); void flush();  }
@Test(expected = IllegalStateException.class) public void shouldNotAllowHeadersToBeFlushedTwice() throws IOException { try { httpServletResponseImpl.flushHeaders(); } catch (IllegalStateException e) { fail("Should not throw exception on the first call"); } httpServletResponseImpl.flushHeaders(); }