Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
smthing committed Sep 16, 2024
1 parent 5e88b9c commit 925ab41
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand Down

0 comments on commit 925ab41

Please sign in to comment.