Skip to content

Commit

Permalink
Merge pull request #43213 from qa-andreas-stangl/fix-quarkus-rest-jax…
Browse files Browse the repository at this point in the history
…b-stream-emptyness-check

More robust Stream emptiness check for ServerJaxbMessageBodyReader
  • Loading branch information
geoand authored Sep 12, 2024
2 parents 8f586aa + f420b25 commit f9d20f8
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

Expand All @@ -20,7 +21,6 @@
import jakarta.xml.bind.UnmarshalException;
import jakarta.xml.bind.Unmarshaller;

import org.jboss.resteasy.reactive.common.util.StreamUtil;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;
Expand Down Expand Up @@ -93,14 +93,16 @@ private Unmarshaller getUnmarshall(Class<Object> type) throws JAXBException {
}

private Object doReadFrom(Class<Object> type, Type genericType, InputStream entityStream) throws IOException {
if (isInputStreamEmpty(entityStream)) {
PushbackInputStream pushbackEntityStream = new PushbackInputStream(entityStream);
if (isStreamEmpty(pushbackEntityStream)) {
return null;
}

return unmarshal(entityStream, type);
return unmarshal(pushbackEntityStream, type);
}

private boolean isInputStreamEmpty(InputStream entityStream) throws IOException {
return StreamUtil.isEmpty(entityStream) || entityStream.available() == 0;
private boolean isStreamEmpty(PushbackInputStream pushbackStream) throws IOException {
int firstByte = pushbackStream.read();
pushbackStream.unread(firstByte);
return firstByte == -1;
}
}

0 comments on commit f9d20f8

Please sign in to comment.