Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 package io.undertow.websockets.core;
 
 
 import java.util.List;

A buffered binary message.

Author(s):
Stuart Douglas
 
 public class BufferedBinaryMessage {
 
     private final boolean bufferFullMessage;
     private List<Pooled<ByteBuffer>> data = new ArrayList<>(1);
     private Pooled<ByteBuffercurrent;
     private final long maxMessageSize;
     private long currentSize;
     private boolean complete;
     private int frameCount;
 
 
     public BufferedBinaryMessage(long maxMessageSizeboolean bufferFullMessage) {
         this. = bufferFullMessage;
         this. = maxMessageSize;
     }
 
     public BufferedBinaryMessage(boolean bufferFullMessage) {
         this(-1, bufferFullMessage);
     }
 
     public void readBlocking(StreamSourceFrameChannel channelthrows IOException {
         if ( == null) {
              = channel.getWebSocketChannel().getBufferPool().allocate();
         }
         for (; ; ) {
             int res = channel.read(.getResource());
             if (res == -1) {
                  = true;
                 return;
             } else if (res == 0) {
                 channel.awaitReadable();
             }
             checkMaxSize(channelres);
             if () {
                 dealWithFullBuffer(channel);
             } else if (!.getResource().hasRemaining()) {
                 return;
             }
         }
     }
 
     private void dealWithFullBuffer(StreamSourceFrameChannel channel) {
         if (!.getResource().hasRemaining()) {
             .getResource().flip();
             .add();
              = channel.getWebSocketChannel().getBufferPool().allocate();
         }
     }
 
     public void read(final StreamSourceFrameChannel channelfinal WebSocketCallback<BufferedBinaryMessagecallback) {
         try {
             for (; ; ) {
                 if ( == null) {
                      = channel.getWebSocketChannel().getBufferPool().allocate();
                 }
                 int res = channel.read(.getResource());
                 if (res == -1) {
                     this. = true;
                     callback.complete(channel.getWebSocketChannel(), this);
                     return;
                 } else if (res == 0) {
                     channel.getReadSetter().set(new ChannelListener<StreamSourceFrameChannel>() {
                         @Override
                         public void handleEvent(StreamSourceFrameChannel channel) {
                            if( ) {
                                return;
                            }
                            try {
                                for (; ; ) {
                                    if ( == null) {
                                         = channel.getWebSocketChannel().getBufferPool().allocate();
                                    }
                                    int res = channel.read(.getResource());
                                    if (res == -1) {
                                         = true;
                                        channel.suspendReads();
                                        callback.complete(channel.getWebSocketChannel(), BufferedBinaryMessage.this);
                                        return;
                                    } else if (res == 0) {
                                        return;
                                    }
                                    checkMaxSize(channelres);
                                    if () {
                                        dealWithFullBuffer(channel);
                                    } else if (!.getResource().hasRemaining()) {
                                        callback.complete(channel.getWebSocketChannel(), BufferedBinaryMessage.this);
                                    } else {
                                        handleNewFrame(channelcallback);
                                    }
                                }
                            } catch (IOException e) {
                                channel.suspendReads();
                                callback.onError(channel.getWebSocketChannel(), BufferedBinaryMessage.thise);
                            }
                        }
                    });
                    channel.resumeReads();
                    return;
                }
                checkMaxSize(channelres);
                if () {
                    dealWithFullBuffer(channel);
                } else if (!.getResource().hasRemaining()) {
                    callback.complete(channel.getWebSocketChannel(), BufferedBinaryMessage.this);
                } else {
                    handleNewFrame(channelcallback);
                }
            }
        } catch (IOException e) {
            callback.onError(channel.getWebSocketChannel(), thise);
        }
    }
    private void handleNewFrame(StreamSourceFrameChannel channelfinal WebSocketCallback<BufferedBinaryMessagecallback) {
        //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(!) {
            if(channel.getWebSocketFrameCount() !=  &&  != null && !channel.isFinalFragment()) {
                 = channel.getWebSocketFrameCount();
                callback.complete(channel.getWebSocketChannel(), this);
            }
        }
    }
    private void checkMaxSize(StreamSourceFrameChannel channelint resthrows IOException {
         += res;
        if ( > 0 &&  > ) {
        }
    }
    public Pooled<ByteBuffer[]> getData() {
        if ( == null) {
            return new ImmediatePooled<>(new ByteBuffer[0]);
        }
        if (.isEmpty()) {
            final Pooled<ByteBuffercurrent = this.;
            current.getResource().flip();
            this. = null;
            final ByteBuffer[] data = new ByteBuffer[]{current.getResource()};
            return new PooledByteBufferArray(Collections.<Pooled<ByteBuffer>>singletonList(current), data);
        }
        .getResource().flip();
        .add();
         = null;
        ByteBuffer[] ret = new ByteBuffer[.size()];
        for (int i = 0; i < .size(); ++i) {
            ret[i] = .get(i).getResource();
        }
        List<Pooled<ByteBuffer>> data = this.;
        this. = new ArrayList<>();
        return new PooledByteBufferArray(dataret);
    }
    public boolean isComplete() {
        return ;
    }
    private static final class PooledByteBufferArray implements Pooled<ByteBuffer[]> {
        private final List<Pooled<ByteBuffer>> pooled;
        private final ByteBuffer[] data;
        private PooledByteBufferArray(List<Pooled<ByteBuffer>> pooledByteBuffer[] data) {
            this. = pooled;
            this. = data;
        }
        @Override
        public void discard() {
            for (Pooled<ByteBufferitem : ) {
                item.discard();
            }
        }
        @Override
        public void free() {
            for (Pooled<ByteBufferitem : ) {
                item.free();
            }
        }
        @Override
        public ByteBuffer[] getResource() throws IllegalStateException {
            return ;
        }
        @Override
        public void close() {
            free();
        }
    }
New to GrepCode? Check out our FAQ X