title: How do I set Content-Length when returning large objects in Jersey JAX-RS server
body: <p>I sometimes want to return a large (several MB) binary object as a response from a JAX-RS resource method. I know the size of the object, and I want the Content-Length header to be set on the response, and I don't want chunked transfer encoding to be used.</p>
  
  <p>In Jersey 1.x, I solved this with a custom MessageBodyWriter:</p>
  
  <pre><code>public class Blob {
    public InputStream stream;
    public long length;    
  }
  
  @Provider
  public class BlobWriter extends MessageBodyWriter&lt;Blob&gt; {
  
    public boolean isWriteable(Class&lt;?&gt; type, Type genericType,
                                 Annotation[] annotations, MediaType mediaType) {
      return Blob.class.isAssignableFrom(type);
    }
  
    public long getSize(T t, Class&lt;?&gt; type, Type genericType, Annotation[] annotations,
                          MediaType mediaType) {
      return t.length;
    }
  
    public void writeTo(T t, Class&lt;?&gt; type, Type genericType, Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap&lt;String, Object&gt; httpHeaders,
                        OutputStream entityStream)
              throws java.io.IOException {
      org.glassfish.jersey.message.internal.ReaderWriter.writeTo(t.stream, entityStream);
    }
  }
  </code></pre>
  
  <p>But this stopped working when I upgraded to Jersey 2.x, since JAX-RS/Jersey 2 does not care about MessageBodyWriter.getSize() anymore. How can I accomplish this with Jersey 2?</p>