From 318e35584eb390d7b276b3fab776978e8c79591d Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 20 Nov 2024 15:00:17 +0100 Subject: [PATCH] EntityInputStream#isEmpty refactoring Signed-off-by: Maxim Nesen --- .../message/internal/EntityInputStream.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStream.java b/core-common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStream.java index eaa41e1982..6e71fd5b42 100644 --- a/core-common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStream.java +++ b/core-common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStream.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.InputStream; +import java.io.PushbackInputStream; import jakarta.ws.rs.ProcessingException; @@ -145,11 +146,15 @@ public boolean isEmpty() { if (availableBytes > 0) { return false; } - //This situation should never happen, but due to some circumstances it can occur - stream comes very - //late, stream's implementation does not override default available() or something like that. - //It's impossible to read from the underlying stream and properly return the read byte into it. - //So we just return true not to corrupt the stream. This marks the whole stream as empty. - return true; + + final PushbackInputStream in = (input instanceof PushbackInputStream) ? (PushbackInputStream) input + : new PushbackInputStream(input); + //This situation occurs in rare cases - stream comes very late, stream's implementation does not + // override default available() or something like that. + int i = in.read(); + in.unread(i); + input = in; + return i == -1; } } catch (IOException ex) { throw new ProcessingException(ex);