quarkusio/quarkus · error · java.lang.IllegalStateException

Entity stream has already been read and is not buffered: cal

Error message

Entity stream has already been read and is not buffered: call Response.bufferEntity()

What it means

The response entity stream was already fully consumed by an earlier readEntity() call, and because bufferEntity() was never called the stream cannot be rewound. ClientRestResponseImpl.readEntity() tries entityStream.reset() only when `buffered` is true; when `consumed` is true it throws this IllegalStateException telling you to call bufferEntity() first.

Source

Thrown at independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/ClientRestResponseImpl.java:45

            return (OtherT) entity;
        }

        checkClosed();

        // apparently we're trying to re-read it here, even if we already have an entity, as long as it's not the right
        // type
        // Note that this will get us the entity if it's an InputStream because setEntity checks that
        InputStream entityStream = getEntityStream();
        if (entityStream == null) {
            entityStream = new EmptyInputStream();
        }

        // it's possible we already read it for a different type, so try to reset it
        try {
            if (buffered) {
                entityStream.reset();
            } else if (consumed) {
                throw new IllegalStateException(
                        "Entity stream has already been read and is not buffered: call Response.bufferEntity()");
            }
        } catch (IOException e) {
            throw new ProcessingException(e);
        }

        // Spec says to return the input stream as-is, without closing it, if that's what we want
        if (InputStream.class.isAssignableFrom(entityType)) {
            return (OtherT) entityStream;
        }

        MediaType mediaType = getMediaType();
        try {
            entity = (T) ClientSerialisers.invokeClientReader(annotations, entityType, genericType, mediaType,
                    restClientRequestContext.properties, restClientRequestContext, getStringHeaders(),
                    restClientRequestContext.getRestClient().getClientContext().getSerialisers(),
                    entityStream, restClientRequestContext.getReaderInterceptors(), restClientRequestContext.configuration);
            consumed = true;

View on GitHub (pinned to e1c734241f)

Solutions

  1. Call response.bufferEntity() once before the first readEntity(); subsequent readEntity calls will then succeed with any compatible type
  2. If you only need the value once, read it a single time into the final type and reuse that object
  3. Read the entity as InputStream only if you handle buffering/rewinding yourself
  4. Restructure interceptors so they do not consume the entity stream, or have them buffer it

Example fix

// before
String raw = res.readEntity(String.class);
MyDto dto = res.readEntity(MyDto.class); // IllegalStateException

// after
res.bufferEntity();
String raw = res.readEntity(String.class);
MyDto dto = res.readEntity(MyDto.class); // OK: stream was buffered and reset
Defensive patterns

Strategy: try-catch

Validate before calling

// buffer before any repeated read
if (!response.bufferEntity()) { /* entity absent or already streaming-only */ }
MyDto dto = response.readEntity(MyDto.class);

Try / catch

try {
    return response.readEntity(MyDto.class);
} catch (IllegalStateException e) {
    if (e.getMessage().contains("Entity stream has already been read")) {
        throw new IllegalStateException("Call response.bufferEntity() before the first readEntity()", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling readEntity() (or readEntity with a different type) twice on the same Response without calling bufferEntity() before the first read.

Common situations: Logging the entity as String then parsing it as a DTO; interceptor/filter reads the body and application code reads it again; retry/deserialization logic re-reading the entity on error paths; generic wrapper code reading the entity for type checks then delegating.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/4c7aed48114fafbd. Report an issue: GitHub.