Trying to simplify the code was a mistake ...

private static final int WRITE_BUFFER_SIZE = 8 * 1024;  <-- with 32K fails on 
this client
 
 private final AsyncContext ac;
 private final ServletResponse sr;
 private ServletOutputStream os;
 private boolean firstTime = true;
 private byte[] response;
 private int responseSize, startIdx, endIdx;
 
 // Constructor
 public WriteListenerImpl(AsyncContext ac, byte[] response) {
  this.ac = ac;
  this.sr = this.ac.getResponse();
  this.response = response;
 }
 
 @Override
 public void onWritePossible() throws IOException
 {
   // If this is the first time we call 'onWritePossible' ...
   if (this.firstTime) {
    this.firstTime = false;
    this.os = this.sr.getOutputStream();

    this.responseSize = this.response.length;
    this.startIdx = 0;
    this.endIdx = Math.min(this.responseSize, WRITE_BUFFER_SIZE);
   }
   
   // And write the 'response'.
   while (this.startIdx < this.endIdx && this.os.isReady()) {
     this.os.write(this.response, this.startIdx, this.endIdx - this.startIdx);

     this.startIdx = this.endIdx;
     this.endIdx += WRITE_BUFFER_SIZE;
     if (this.endIdx > this.responseSize) this.endIdx = this.responseSize;
   }

   // Just perform the 'postProcess' when response writing is finished (start 
== end)
   if (this.startIdx == this.endIdx) {
    this.postProcess(null);
   }
 }
 
  @Override
 public void onError(Throwable t) {
  this.postProcess(t);
 }
 
 private void postProcess(Throwable t) {
  this.ac.complete();
  
  if (t != null) {
   t.printStackeTrace();
  }
 }

-----Original Message-----
From: Chuck Caldarale <n82...@gmail.com> 
Sent: Thursday, May 30, 2024 8:01 PM
To: Tomcat Users List <users@tomcat.apache.org>
Subject: Re: Write listener question


> On May 30, 2024, at 12:53, <joan.balagu...@ventusproxy.com> 
> <joan.balagu...@ventusproxy.com> wrote:
> 
> isFirst is initialized to 'true' when the class is instantiated, so 
> that piece of code is just executed the first time the execution enters the '
> onWritePossible' method. Later, within the same 'if', 'isFirst' is set 
> to false, (not shown in the code, sorry)


If you don’t show us all of the code, then it’s rather difficult to answer your 
question about the implementation being correct.


> Perhaps this one client has a slow network, so isReady() returns false 
> in that environment, but not in other ones.
> --> And how can this be solved?


The slow network possibility was based on the assumption that isFirst was never 
cleared, since that’s what you presented.

  - Chuck


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to