Skip to content

Commit

Permalink
EntityInputStream#isEmpty refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Nov 20, 2024
1 parent 2bd5ba2 commit 35af9ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.glassfish.jersey.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Type;
import java.net.URI;
Expand Down Expand Up @@ -56,7 +55,6 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.glassfish.jersey.innate.io.InputStreamWrapper;
import org.glassfish.jersey.internal.ServiceFinderBinder;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.internal.inject.InjectionManager;
Expand Down Expand Up @@ -424,16 +422,7 @@ private void initContainerRequest(
final ResponseWriter responseWriter) throws IOException {

try {
requestContext.setEntityStream(new InputStreamWrapper() {
@Override
protected InputStream getWrapped() {
try {
return servletRequest.getInputStream();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
});
requestContext.setEntityStream(servletRequest.getInputStream());
} catch (UncheckedIOException e) {
throw e.getCause();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
switch (method.getName()) {
case "getHeaderNames":
return Collections.emptyEnumeration();
case "getInputStream":
throw new IllegalStateException("ServletRequest#getInputStream clashes with ServletRequest#getReader");
// case "getInputStream":
// throw new IllegalStateException("ServletRequest#getInputStream clashes with ServletRequest#getReader");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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

import jakarta.ws.rs.ProcessingException;

Expand Down Expand Up @@ -93,13 +92,12 @@ public void reset() {
*/
@Override
public void close() throws ProcessingException {
final InputStream in = input;
if (in == null) {
if (input == null) {
return;
}
if (!closed) {
try {
in.close();
input.close();
} catch (IOException ex) {
// This e.g. means that the underlying socket stream got closed by other thread somehow...
throw new ProcessingException(LocalizationMessages.MESSAGE_CONTENT_INPUT_STREAM_CLOSE_FAILED(), ex);
Expand All @@ -119,43 +117,39 @@ public void close() throws ProcessingException {
*/
public boolean isEmpty() {
ensureNotClosed();

final InputStream in = input;
if (in == null) {
if (input == null) {
return true;
}

try {
// Try #markSupported first - #available on WLS waits until socked timeout is reached when chunked encoding is used.
if (in.markSupported()) {
in.mark(1);
int i = in.read();
in.reset();
if (input.markSupported()) {
input.mark(1);
int i = input.read();
input.reset();
return i == -1;
} else {
int availableBytes = 0;
int exceedCount = 50;
try {
if (in.available() > 0) {
return false;

while (availableBytes == 0 && exceedCount > 0) {
availableBytes = input.available();
exceedCount--;
}

} catch (IOException ioe) {
// NOOP. Try other approaches as this can fail on WLS.
}

int b = in.read();
if (b == -1) {
return true;
if (availableBytes > 0) {
return false;
}

PushbackInputStream pbis;
if (in instanceof PushbackInputStream) {
pbis = (PushbackInputStream) in;
} else {
pbis = new PushbackInputStream(in, 1);
input = pbis;
}
pbis.unread(b);

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;
}
} catch (IOException ex) {
throw new ProcessingException(ex);
Expand Down

0 comments on commit 35af9ad

Please sign in to comment.