{"record":{"id":"d8b54bc7d693dd8d","repo":"quarkusio/quarkus","slug":"getcontent-has-already-been-invoked","errorCode":null,"errorMessage":"getContent() has already been invoked","messagePattern":"getContent\\(\\) has already been invoked","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/jaxrs/EntityPartImpl.java","lineNumber":56,"sourceCode":"        this.mediaType = mediaType;\n        this.content = content;\n        this.serialisers = serialisers;\n    }\n\n    @Override\n    public String getName() {\n        return name;\n    }\n\n    @Override\n    public Optional<String> getFileName() {\n        return Optional.ofNullable(fileName);\n    }\n\n    @Override\n    public InputStream getContent() {\n        if (!contentConsumed.compareAndSet(false, true)) {\n            throw new IllegalStateException(\"getContent() has already been invoked\");\n        }\n        return content;\n    }\n\n    @Override\n    public <T> T getContent(Class<T> type)\n            throws IllegalArgumentException, IllegalStateException, IOException, WebApplicationException {\n        if (!contentConsumed.compareAndSet(false, true)) {\n            throw new IllegalStateException(\"getContent() has already been invoked\");\n        }\n        return readContent(type, type);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    @Override\n    public <T> T getContent(GenericType<T> type)\n            throws IllegalArgumentException, IllegalStateException, IOException, WebApplicationException {\n        if (!contentConsumed.compareAndSet(false, true)) {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/jaxrs/EntityPartImpl.java#L38-L74","documentation":"EntityPartImpl.getContent() (the InputStream overload) throws IllegalStateException if called more than once. An EntityPart's body is a one-shot stream; once consumed it cannot be rewound, so the implementation guards with a compareAndSet flag and fails fast on the second call. This mirrors the JAX-RS 3.1 spec's single-consumption semantics.","triggerScenarios":"Calling part.getContent() twice on the same EntityPart obtained from a MultipartFormDataOutput response or client parse — e.g. reading the raw stream and then, in error handling or logging, reading it again.","commonSituations":"Logging part content then passing it downstream; retry/error paths that re-read the same part; interceptors or filters that consume the content before the business code does.","solutions":["Call getContent() exactly once and buffer the bytes (read all into byte[]) if you need multiple uses.","Use one of the typed overloads getContent(Class) / getContent(GenericType) instead of multiple raw reads.","Cache the converted value (e.g. String or byte[]) in a local variable and reuse it."],"exampleFix":"// before\nString logged = new String(part.getContent().readAllBytes());\nprocess(part.getContent()); // IllegalStateException: already invoked\n// after\nbyte[] data = part.getContent().readAllBytes();\nString logged = new String(data);\nprocess(new ByteArrayInputStream(data));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    InputStream in = part.getContent();\n    byte[] data = in.readAllBytes(); // consume once, reuse everywhere\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"already been invoked\")) {\n        throw new IllegalStateException(\"part content read twice; buffer instead\", e);\n    }\n    throw e;\n}","preventionTips":["Read multipart part content exactly once and buffer bytes for reuse.","Never pass the raw EntityPart to multiple consumers; pass the buffered data.","Avoid reading content in interceptors/logging before business code."],"tags":["resteasy-reactive","multipart","illegal-state","stream","one-shot"],"backgroundTag":"stream-already-consumed","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}