Skip to content

Commit 35af9ad

Browse files
committed
EntityInputStream#isEmpty refactoring
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
1 parent 2bd5ba2 commit 35af9ad

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.glassfish.jersey.servlet;
1818

1919
import java.io.IOException;
20-
import java.io.InputStream;
2120
import java.io.UncheckedIOException;
2221
import java.lang.reflect.Type;
2322
import java.net.URI;
@@ -56,7 +55,6 @@
5655
import jakarta.servlet.http.HttpServletRequest;
5756
import jakarta.servlet.http.HttpServletResponse;
5857

59-
import org.glassfish.jersey.innate.io.InputStreamWrapper;
6058
import org.glassfish.jersey.internal.ServiceFinderBinder;
6159
import org.glassfish.jersey.internal.inject.AbstractBinder;
6260
import org.glassfish.jersey.internal.inject.InjectionManager;
@@ -424,16 +422,7 @@ private void initContainerRequest(
424422
final ResponseWriter responseWriter) throws IOException {
425423

426424
try {
427-
requestContext.setEntityStream(new InputStreamWrapper() {
428-
@Override
429-
protected InputStream getWrapped() {
430-
try {
431-
return servletRequest.getInputStream();
432-
} catch (IOException e) {
433-
throw new UncheckedIOException(e);
434-
}
435-
}
436-
});
425+
requestContext.setEntityStream(servletRequest.getInputStream());
437426
} catch (UncheckedIOException e) {
438427
throw e.getCause();
439428
}

containers/jersey-servlet-core/src/test/java/org/glassfish/jersey/servlet/internal/RequestInputStreamTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
4747
switch (method.getName()) {
4848
case "getHeaderNames":
4949
return Collections.emptyEnumeration();
50-
case "getInputStream":
51-
throw new IllegalStateException("ServletRequest#getInputStream clashes with ServletRequest#getReader");
50+
// case "getInputStream":
51+
// throw new IllegalStateException("ServletRequest#getInputStream clashes with ServletRequest#getReader");
5252
}
5353
return null;
5454
}

core-common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStream.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21-
import java.io.PushbackInputStream;
2221

2322
import jakarta.ws.rs.ProcessingException;
2423

@@ -93,13 +92,12 @@ public void reset() {
9392
*/
9493
@Override
9594
public void close() throws ProcessingException {
96-
final InputStream in = input;
97-
if (in == null) {
95+
if (input == null) {
9896
return;
9997
}
10098
if (!closed) {
10199
try {
102-
in.close();
100+
input.close();
103101
} catch (IOException ex) {
104102
// This e.g. means that the underlying socket stream got closed by other thread somehow...
105103
throw new ProcessingException(LocalizationMessages.MESSAGE_CONTENT_INPUT_STREAM_CLOSE_FAILED(), ex);
@@ -119,43 +117,39 @@ public void close() throws ProcessingException {
119117
*/
120118
public boolean isEmpty() {
121119
ensureNotClosed();
122-
123-
final InputStream in = input;
124-
if (in == null) {
120+
if (input == null) {
125121
return true;
126122
}
127123

128124
try {
129125
// Try #markSupported first - #available on WLS waits until socked timeout is reached when chunked encoding is used.
130-
if (in.markSupported()) {
131-
in.mark(1);
132-
int i = in.read();
133-
in.reset();
126+
if (input.markSupported()) {
127+
input.mark(1);
128+
int i = input.read();
129+
input.reset();
134130
return i == -1;
135131
} else {
132+
int availableBytes = 0;
133+
int exceedCount = 50;
136134
try {
137-
if (in.available() > 0) {
138-
return false;
135+
136+
while (availableBytes == 0 && exceedCount > 0) {
137+
availableBytes = input.available();
138+
exceedCount--;
139139
}
140+
140141
} catch (IOException ioe) {
141142
// NOOP. Try other approaches as this can fail on WLS.
142143
}
143144

144-
int b = in.read();
145-
if (b == -1) {
146-
return true;
145+
if (availableBytes > 0) {
146+
return false;
147147
}
148-
149-
PushbackInputStream pbis;
150-
if (in instanceof PushbackInputStream) {
151-
pbis = (PushbackInputStream) in;
152-
} else {
153-
pbis = new PushbackInputStream(in, 1);
154-
input = pbis;
155-
}
156-
pbis.unread(b);
157-
158-
return false;
148+
//This situation should never happen, but due to some circumstances it can occur - stream comes very
149+
//late, stream's implementation does not override default available() or something like that.
150+
//It's impossible to read from the underlying stream and properly return the read byte into it.
151+
//So we just return true not to corrupt the stream. This marks the whole stream as empty.
152+
return true;
159153
}
160154
} catch (IOException ex) {
161155
throw new ProcessingException(ex);

0 commit comments

Comments
 (0)