0

I know I can stream my output using a StreamingOutput. But can I do it also with a MessageBodyWriter? If I implement it like this:

@Override
public void writeTo(HelloWorldRepresentation t, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    "Hello world".chars().forEach(i -> {
        try {
            entityStream.write(i);
            entityStream.write('\n');
            entityStream.flush();
            Thread.sleep(1000);
        } catch (Exception e) {
            throw new WebApplicationException(e);
        }
    });
}

All output seems to arrive at the same time (i.e. not streaming). Any clues?

Friso
  • 1,080
  • 6
  • 37

1 Answers1

0

For the curious that stumble upon this question, it was not streaming since there was not enough data for it to stream. The standard is

/**
 * The default buffer size ({@value}) for I/O operations on byte and character
 * streams.
 */
public static final int IO_DEFAULT_BUFFER_SIZE = 8192;

(from https://github.com/jersey/jersey/blob/master/core-common/src/main/java/org/glassfish/jersey/message/MessageProperties.java) which I obviously did not hit.

Friso
  • 1,080
  • 6
  • 37