diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/impl/HttpServletRequestImpl.java b/servlet-core/src/main/java/tech/smartboot/servlet/impl/HttpServletRequestImpl.java index a9117f0..5a9c6d5 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/impl/HttpServletRequestImpl.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/impl/HttpServletRequestImpl.java @@ -629,7 +629,7 @@ public ServletInputStream getInputStream() throws IOException { throw new IllegalStateException("getReader method has already been called for this request"); } if (servletInputStream == null) { - servletInputStream = new ServletInputStreamImpl(request.getInputStream()); + servletInputStream = new ServletInputStreamImpl(this,request.getInputStream()); } return servletInputStream; } diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletInputStreamImpl.java b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletInputStreamImpl.java index 4ccaf22..d25b568 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletInputStreamImpl.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletInputStreamImpl.java @@ -12,17 +12,21 @@ import jakarta.servlet.ReadListener; import jakarta.servlet.ServletInputStream; +import org.smartboot.http.common.io.BodyInputStream; + import java.io.IOException; -import java.io.InputStream; /** * @author 三刀(zhengjunweimail@163.com) * @version V1.0 , 2020/12/12 */ public class ServletInputStreamImpl extends ServletInputStream { - private final InputStream inputStream; + private final BodyInputStream inputStream; + private ReadListener readListener; + private final HttpServletRequestImpl request; - public ServletInputStreamImpl(InputStream inputStream) { + public ServletInputStreamImpl(HttpServletRequestImpl request, BodyInputStream inputStream) { + this.request = request; this.inputStream = inputStream; } @@ -33,12 +37,40 @@ public boolean isFinished() { @Override public boolean isReady() { - throw new UnsupportedOperationException(); + if (request.isAsyncStarted()) { + return inputStream.isReady(); + } else { + return false; + } } @Override public void setReadListener(ReadListener readListener) { - throw new UnsupportedOperationException(); + if (readListener == null) { + throw new NullPointerException(); + } + if (this.readListener != null) { + throw new IllegalStateException(); + } + if (!request.isAsyncStarted()) { + throw new IllegalStateException(); + } + inputStream.setReadListener(new org.smartboot.http.common.io.ReadListener() { + @Override + public void onDataAvailable() throws IOException { + readListener.onDataAvailable(); + } + + @Override + public void onAllDataRead() throws IOException { + readListener.onAllDataRead(); + } + + @Override + public void onError(Throwable t) { + readListener.onError(t); + } + }); } @Override