Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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 org.apache.catalina.websocket;
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 import java.io.Reader;
Base implementation of the class used to process WebSocket connections based on messages. Applications should extend this class to provide application specific functionality. Applications that wish to operate on a stream basis rather than a message basis should use StreamInbound.
 
 public abstract class MessageInbound extends StreamInbound {
 
     // 2MB - like maxPostSize
     private int byteBufferMaxSize = 2097152;
     private int charBufferMaxSize = 2097152;
 
     private ByteBuffer bb = ByteBuffer.allocate(8192);
     private CharBuffer cb = CharBuffer.allocate(8192);
 
 
     @Override
     protected final void onBinaryData(InputStream isthrows IOException {
         int read = 0;
         while (read > -1) {
             .position(.position() + read);
             if (.remaining() == 0) {
                 resizeByteBuffer();
             }
             read = is.read(.array(), .position(), .remaining());
         }
         .flip();
         onBinaryMessage();
         .clear();
     }
 
 
     @Override
     protected final void onTextData(Reader rthrows IOException {
         int read = 0;
         while (read > -1) {
             .position(.position() + read);
             if (.remaining() == 0) {
                 resizeCharBuffer();
             }
             read = r.read(.array(), .position(), .remaining());
         }
         .flip();
         onTextMessage();
         .clear();
     }
 
 
     private void resizeByteBuffer() throws IOException {
         int maxSize = getByteBufferMaxSize();
         if (.limit() >= maxSize) {
             throw new IOException(.bufferTooSmall());
         }
 
         long newSize = .limit() * 2;
         if (newSize > maxSize) {
             newSize = maxSize;
         }
 
         // Cast is safe. newSize < maxSize and maxSize is an int
         ByteBuffer newBuffer = ByteBuffer.allocate((intnewSize);
         .rewind();
         newBuffer.put();
          = newBuffer;
     }
 
 
     private void resizeCharBuffer() throws IOException {
         int maxSize = getCharBufferMaxSize();
         if (.limit() >= maxSize) {
             throw new IOException(.bufferTooSmall());
         }
 
        long newSize = .limit() * 2;
        if (newSize > maxSize) {
            newSize = maxSize;
        }
        // Cast is safe. newSize < maxSize and maxSize is an int
        CharBuffer newBuffer = CharBuffer.allocate((intnewSize);
        .rewind();
        newBuffer.put();
         = newBuffer;
    }


    
Obtain the current maximum size (in bytes) of the buffer used for binary messages.
    public final int getByteBufferMaxSize() {
        return ;
    }


    
Set the maximum size (in bytes) of the buffer used for binary messages.
    public final void setByteBufferMaxSize(int byteBufferMaxSize) {
        this. = byteBufferMaxSize;
    }


    
Obtain the current maximum size (in characters) of the buffer used for binary messages.
    public final int getCharBufferMaxSize() {
        return ;
    }


    
Set the maximum size (in characters) of the buffer used for textual messages.
    public final void setCharBufferMaxSize(int charBufferMaxSize) {
        this. = charBufferMaxSize;
    }


    
This method is called when there is a binary WebSocket message available to process. The message is presented via a ByteBuffer and may have been formed from one or more frames. The number of frames used to transmit the message is not made visible to the application.

Parameters:
message The WebSocket message
Throws:
java.io.IOException If a problem occurs processing the message. Any exception will trigger the closing of the WebSocket connection.
    protected abstract void onBinaryMessage(ByteBuffer message)
            throws IOException;


    
This method is called when there is a textual WebSocket message available to process. The message is presented via a CharBuffer and may have been formed from one or more frames. The number of frames used to transmit the message is not made visible to the application.

Parameters:
message The WebSocket message
Throws:
java.io.IOException If a problem occurs processing the message. Any exception will trigger the closing of the WebSocket connection.
    protected abstract void onTextMessage(CharBuffer message)
            throws IOException;
New to GrepCode? Check out our FAQ X