/**
   * This implementation sets the default headers by calling {@link #addDefaultHeaders}, and then
   * calls {@link #writeInternal}.
   */
  @Override
  public final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage)
      throws IOException, HttpMessageNotWritableException {

    final HttpHeaders headers = outputMessage.getHeaders();
    addDefaultHeaders(headers, t, contentType);

    if (outputMessage instanceof StreamingHttpOutputMessage) {
      StreamingHttpOutputMessage streamingOutputMessage =
          (StreamingHttpOutputMessage) outputMessage;
      streamingOutputMessage.setBody(
          new StreamingHttpOutputMessage.Body() {
            @Override
            public void writeTo(final OutputStream outputStream) throws IOException {
              writeInternal(
                  t,
                  new HttpOutputMessage() {
                    @Override
                    public OutputStream getBody() throws IOException {
                      return outputStream;
                    }

                    @Override
                    public HttpHeaders getHeaders() {
                      return headers;
                    }
                  });
            }
          });
    } else {
      writeInternal(t, outputMessage);
      outputMessage.getBody().flush();
    }
  }
  @Override
  public void write(
      final BufferedImage image, final MediaType contentType, final HttpOutputMessage outputMessage)
      throws IOException, HttpMessageNotWritableException {

    if (outputMessage instanceof StreamingHttpOutputMessage) {
      StreamingHttpOutputMessage streamingOutputMessage =
          (StreamingHttpOutputMessage) outputMessage;
      streamingOutputMessage.setBody(
          new StreamingHttpOutputMessage.Body() {
            @Override
            public void writeTo(OutputStream outputStream) throws IOException {
              writeInternal(image, contentType, outputMessage.getHeaders(), outputStream);
            }
          });
    } else {
      writeInternal(image, contentType, outputMessage.getHeaders(), outputMessage.getBody());
    }
  }