Java Code Examples for javax.servlet.ServletOutputStream#isReady()

The following examples show how to use javax.servlet.ServletOutputStream#isReady() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServletServerHttpResponse.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void flush() throws IOException {
	ServletOutputStream outputStream = this.outputStream;
	if (outputStream.isReady()) {
		try {
			outputStream.flush();
			this.flushOnNext = false;
		}
		catch (IOException ex) {
			this.flushOnNext = true;
			throw ex;
		}
	}
	else {
		this.flushOnNext = true;
	}
}
 
Example 2
Source File: ServletServerHttpResponse.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void flush() throws IOException {
	ServletOutputStream outputStream = this.outputStream;
	if (outputStream.isReady()) {
		try {
			outputStream.flush();
			this.flushOnNext = false;
		}
		catch (IOException ex) {
			this.flushOnNext = true;
			throw ex;
		}
	}
	else {
		this.flushOnNext = true;
	}
}
 
Example 3
Source File: AccessLogFilter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public ServletOutputStream getOutputStream() throws IOException {

  final ServletOutputStream outputStream = d.getOutputStream();
  return new ServletOutputStream() {

    @Override
    public void write(int b) throws IOException {
      respBody.write(b);
      outputStream.write(b);
    }

    @Override
    public void setWriteListener(WriteListener writeListener) {
      outputStream.setWriteListener(writeListener);
    }

    @Override
    public boolean isReady() {
      return outputStream.isReady();
    }
  };
}
 
Example 4
Source File: TestCoyoteOutputStream.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void doAsyncWrite(AsyncContext asyncCtxt,
        ServletOutputStream sos) throws IOException {
    while (sos.isReady()) {
        int next = asyncWriteCount.getAndIncrement();
        if (next < asyncWriteTarget) {
            sos.write(
                    ("OK - " + next + System.lineSeparator()).getBytes(
                            StandardCharsets.UTF_8));
            sos.flush();
        } else {
            asyncCtxt.dispatch("/write");
            break;
        }
    }
}
 
Example 5
Source File: ServletServerHttpResponse.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Write the DataBuffer to the response body OutputStream.
 * Invoked only when {@link ServletOutputStream#isReady()} returns "true"
 * and the readable bytes in the DataBuffer is greater than 0.
 * @return the number of bytes written
 */
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
	ServletOutputStream outputStream = this.outputStream;
	InputStream input = dataBuffer.asInputStream();
	int bytesWritten = 0;
	byte[] buffer = new byte[this.bufferSize];
	int bytesRead;
	while (outputStream.isReady() && (bytesRead = input.read(buffer)) != -1) {
		outputStream.write(buffer, 0, bytesRead);
		bytesWritten += bytesRead;
	}
	return bytesWritten;
}
 
Example 6
Source File: ServletServerHttpResponse.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Write the DataBuffer to the response body OutputStream.
 * Invoked only when {@link ServletOutputStream#isReady()} returns "true"
 * and the readable bytes in the DataBuffer is greater than 0.
 * @return the number of bytes written
 */
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
	ServletOutputStream outputStream = this.outputStream;
	InputStream input = dataBuffer.asInputStream();
	int bytesWritten = 0;
	byte[] buffer = new byte[this.bufferSize];
	int bytesRead;
	while (outputStream.isReady() && (bytesRead = input.read(buffer)) != -1) {
		outputStream.write(buffer, 0, bytesRead);
		bytesWritten += bytesRead;
	}
	return bytesWritten;
}
 
Example 7
Source File: AsyncOutputStreamServlet.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final boolean flush = req.getParameter("flush") != null;
    final boolean close = req.getParameter("close") != null;
    final boolean preable = req.getParameter("preamble") != null;
    final boolean offIoThread = req.getParameter("offIoThread") != null;
    final int reps = Integer.parseInt(req.getParameter("reps"));

    final AtomicInteger count = new AtomicInteger();

    final AsyncContext context = req.startAsync();
    final ServletOutputStream outputStream = resp.getOutputStream();
    if(preable) {
        for(int i = 0; i < reps; ++i) {
            outputStream.write(ServletOutputStreamTestCase.message.getBytes());
        }
    }
    WriteListener listener = new WriteListener() {
        @Override
        public synchronized void onWritePossible() throws IOException {
            while (outputStream.isReady() && count.get() < reps) {
                count.incrementAndGet();
                outputStream.write(ServletOutputStreamTestCase.message.getBytes());
            }
            if (count.get() == reps) {
                if (flush) {
                    outputStream.flush();
                }
                if (close) {
                    outputStream.close();
                }
                context.complete();
            }
        }

        @Override
        public void onError(final Throwable t) {

        }
    };
    outputStream.setWriteListener(offIoThread ? new WriteListener() {
        @Override
        public void onWritePossible() throws IOException {
            context.start(new Runnable() {
                @Override
                public void run() {
                    try {
                        listener.onWritePossible();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }

        @Override
        public void onError(Throwable throwable) {

        }
    } : listener);
}