Skip to content

Commit

Permalink
EntityInputStream#isEmpty refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
  • Loading branch information
senivam committed Nov 20, 2024
1 parent 35af9ad commit 318e355
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

import jakarta.ws.rs.ProcessingException;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 318e355

Please sign in to comment.