private void dealWithFullBuffer(StreamSourceFrameChannel channel) {
   if (!current.getResource().hasRemaining()) {
     current.getResource().flip();
     data.add(current);
     current = channel.getWebSocketChannel().getBufferPool().allocate();
   }
 }
 private void checkMaxSize(StreamSourceFrameChannel channel, int res) throws IOException {
   currentSize += res;
   if (maxMessageSize > 0 && currentSize > maxMessageSize) {
     WebSockets.sendClose(
         new CloseMessage(
                 CloseMessage.MSG_TOO_BIG, WebSocketMessages.MESSAGES.messageToBig(maxMessageSize))
             .toByteBuffer(),
         channel.getWebSocketChannel(),
         null);
     throw new IOException(WebSocketMessages.MESSAGES.messageToBig(maxMessageSize));
   }
 }
 private void handleNewFrame(
     StreamSourceFrameChannel channel, final WebSocketCallback<BufferedBinaryMessage> callback) {
   // TODO: remove this crap
   // basically some bogus web sockets TCK tests assume that messages will be broken up into frames
   // even if we have the full message available.
   if (!bufferFullMessage) {
     if (channel.getWebSocketFrameCount() != frameCount
         && current != null
         && !channel.isFinalFragment()) {
       frameCount = channel.getWebSocketFrameCount();
       callback.complete(channel.getWebSocketChannel(), this);
     }
   }
 }
 public void readBlocking(StreamSourceFrameChannel channel) throws IOException {
   if (current == null) {
     current = channel.getWebSocketChannel().getBufferPool().allocate();
   }
   for (; ; ) {
     int res = channel.read(current.getResource());
     if (res == -1) {
       complete = true;
       return;
     } else if (res == 0) {
       channel.awaitReadable();
     }
     checkMaxSize(channel, res);
     if (bufferFullMessage) {
       dealWithFullBuffer(channel);
     } else if (!current.getResource().hasRemaining()) {
       return;
     }
   }
 }
  public void read(
      final StreamSourceFrameChannel channel,
      final WebSocketCallback<BufferedBinaryMessage> callback) {
    try {
      for (; ; ) {
        if (current == null) {
          current = channel.getWebSocketChannel().getBufferPool().allocate();
        }
        int res = channel.read(current.getResource());
        if (res == -1) {
          this.complete = true;
          callback.complete(channel.getWebSocketChannel(), this);
          return;
        } else if (res == 0) {
          channel
              .getReadSetter()
              .set(
                  new ChannelListener<StreamSourceFrameChannel>() {
                    @Override
                    public void handleEvent(StreamSourceFrameChannel channel) {
                      try {
                        for (; ; ) {
                          if (current == null) {
                            current = channel.getWebSocketChannel().getBufferPool().allocate();
                          }
                          int res = channel.read(current.getResource());
                          if (res == -1) {
                            complete = true;
                            channel.suspendReads();
                            callback.complete(
                                channel.getWebSocketChannel(), BufferedBinaryMessage.this);
                            return;
                          } else if (res == 0) {
                            return;
                          }

                          checkMaxSize(channel, res);
                          if (bufferFullMessage) {
                            dealWithFullBuffer(channel);
                          } else if (!current.getResource().hasRemaining()) {
                            callback.complete(
                                channel.getWebSocketChannel(), BufferedBinaryMessage.this);
                          } else {
                            handleNewFrame(channel, callback);
                          }
                        }
                      } catch (IOException e) {
                        channel.suspendReads();
                        callback.onError(
                            channel.getWebSocketChannel(), BufferedBinaryMessage.this, e);
                      }
                    }
                  });
          channel.resumeReads();
          return;
        }

        checkMaxSize(channel, res);
        if (bufferFullMessage) {
          dealWithFullBuffer(channel);
        } else if (!current.getResource().hasRemaining()) {
          callback.complete(channel.getWebSocketChannel(), BufferedBinaryMessage.this);
        } else {
          handleNewFrame(channel, callback);
        }
      }
    } catch (IOException e) {
      callback.onError(channel.getWebSocketChannel(), this, e);
    }
  }