@Override
 public int read(final byte[] b, final int off, final int len) throws IOException {
   if (anyAreSet(state, FLAG_CLOSED)) {
     throw UndertowServletMessages.MESSAGES.streamIsClosed();
   }
   if (anyAreSet(state, FLAG_FINISHED)) {
     return -1;
   }
   ByteBuffer buffer = ByteBuffer.wrap(b, off, len);
   if (listener == null) {
     int res = Channels.readBlocking(channel, buffer);
     if (res == -1) {
       state |= FLAG_FINISHED;
     }
     return res;
   } else {
     if (anyAreClear(state, FLAG_READY)) {
       throw UndertowServletMessages.MESSAGES.streamNotReady();
     }
     int res = channel.read(buffer);
     if (res == -1) {
       state |= FLAG_FINISHED;
     } else if (res == 0) {
       state &= ~FLAG_READY;
       channel.resumeReads();
     }
     return res;
   }
 }
  public void renegotiateBufferRequest(HttpServerExchange exchange, SslClientAuthMode newAuthMode)
      throws IOException {
    int maxSize =
        exchange
            .getConnection()
            .getUndertowOptions()
            .get(UndertowOptions.MAX_BUFFERED_REQUEST_SIZE, 16384);
    if (maxSize <= 0) {
      throw new SSLPeerUnverifiedException("");
    }

    // first we need to read the request
    boolean requestResetRequired = false;
    StreamSourceChannel requestChannel = Connectors.getExistingRequestChannel(exchange);
    if (requestChannel == null) {
      requestChannel = exchange.getRequestChannel();
      requestResetRequired = true;
    }

    Pooled<ByteBuffer> pooled = exchange.getConnection().getBufferPool().allocate();
    boolean free = true; // if the pooled buffer should be freed
    int usedBuffers = 0;
    Pooled<ByteBuffer>[] poolArray = null;
    final int bufferSize = pooled.getResource().remaining();
    int allowedBuffers = ((maxSize + bufferSize - 1) / bufferSize);
    poolArray = new Pooled[allowedBuffers];
    poolArray[usedBuffers++] = pooled;
    try {
      int res;
      do {
        final ByteBuffer buf = pooled.getResource();
        res = Channels.readBlocking(requestChannel, buf);
        if (!buf.hasRemaining()) {
          if (usedBuffers == allowedBuffers) {
            throw new SSLPeerUnverifiedException("");
          } else {
            buf.flip();
            pooled = exchange.getConnection().getBufferPool().allocate();
            poolArray[usedBuffers++] = pooled;
          }
        }
      } while (res != -1);
      free = false;
      pooled.getResource().flip();
      Connectors.ungetRequestBytes(exchange, poolArray);
      renegotiateNoRequest(exchange, newAuthMode);
    } finally {
      if (free) {
        for (Pooled<ByteBuffer> buf : poolArray) {
          if (buf != null) {
            buf.free();
          }
        }
      }
      if (requestResetRequired) {
        exchange.requestChannel = null;
      }
    }
  }