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;
 
 
Represents a complete WebSocket frame with the exception of the payload for non-control frames.
 
 public class WsFrame {
 
     private final boolean fin;
     private final int rsv;
     private final byte opCode;
     private final byte[] mask = new byte[4];
     private long payloadLength;
     private final ByteBuffer payload;

    
Create the new WebSocket frame, reading data from the processor as necessary.

Parameters:
first First byte of data for this frame
processor Processor associated with the WebSocket connection on which the frame has been sent
Throws:
java.io.IOException If a problem occurs processing the frame. Any exception will trigger the closing of the WebSocket connection.
 
     private WsFrame(byte firstHttpEvent eventthrows IOException {
 
         int b = first & 0xFF;
          = (b & 0x80) > 0;
          = (b & 0x70) >>> 4;
          = (byte) (b & 0x0F);
 
         b = blockingRead(event);
         // Client data must be masked
         if ((b & 0x80) == 0) {
             throw new IOException(.frameNotMasked());
         }
 
          = b & 0x7F;
         if ( == 126) {
             byte[] extended = new byte[2];
             blockingRead(eventextended);
              = byteArrayToLong(extended);
         } else if ( == 127) {
             byte[] extended = new byte[8];
             blockingRead(eventextended);
              = byteArrayToLong(extended);
         }
 
         if (isControl()) {
             if ( > 125) {
                 throw new IOException();
             }
             if (!) {
                 throw new IOException();
             }
         }
 
         blockingRead(event);
 
         if (isControl()) {
             // Note: Payload limited to <= 125 bytes by test above
              = ByteBuffer.allocate((int);
             blockingRead(event);
 
             if ( == . &&  > 2) {
                 // Check close payload - if present - is valid UTF-8
                 CharBuffer cb = CharBuffer.allocate((int);
                 Utf8Decoder decoder = new Utf8Decoder();
                 .position(2);
                CoderResult cr = decoder.decode(cbtrue);
                .position(0);
                if (cr.isError()) {
                    throw new IOException(.frameInvalidUtf8());
                }
            }
        } else {
             = null;
        }
    }
    public static long byteArrayToLong(byte[] inputthrows IOException {
        if (input.length > 8) {
            // TODO: Better message
            throw new IOException();
        }
        int shift = 0;
        long result = 0;
        for (int i = input.length - 1; i >= 0; i--) {
            result = result + ((input[i] & 0xFF) << shift);
            shift += 8;
        }
        return result;
    }
    public boolean getFin() {
        return ;
    }
    public int getRsv() {
        return ;
    }
    public byte getOpCode() {
        return ;
    }
    public boolean isControl() {
        return ( & 0x08) > 0;
    }
    public byte[] getMask() {
        return ;
    }
    public long getPayLoadLength() {
        return ;
    }
    public ByteBuffer getPayLoad() {
        return ;
    }
    /*
     * Blocks until a aingle byte has been read
     */
    private int blockingRead(HttpEvent event)
            throws IOException {
        int result = event.getHttpServletRequest().getInputStream().read();
        if (result == -1) {
            throw new IOException(.frameEos());
        }
        return result;
    }
    /*
     * Blocks until the byte array has been filled.
     */
    private void blockingRead(HttpEvent eventbyte[] bytes)
            throws IOException {
        int read = 0;
        int last = 0;
        while (read < bytes.length) {
            last = event.getHttpServletRequest().getInputStream().read(bytesreadbytes.length - read);
            if (last == -1) {
                throw new IOException(.frameEos());
            }
            read += last;
        }
    }
    /*
     * Intended to read whole payload and blocks until it has. Therefore able to
     * unmask the payload data.
     */
    private void blockingRead(HttpEvent eventByteBuffer bb)
            throws IOException {
        int last = 0;
        while (bb.hasRemaining()) {
            last = event.getHttpServletRequest().getInputStream().read();
            if (last == -1) {
                throw new IOException(.frameEos());
            }
            bb.put((byte) (last ^ [bb.position() % 4]));
        }
        bb.flip();
    }


    
Read the next WebSocket frame, reading data from the processor as necessary.

Parameters:
processor Processor associated with the WebSocket connection on which the frame has been sent
block Should this method block until a frame is presented if no data is currently available to process. Note that is a single byte is available, this method will block until the complete frame (excluding payload for non-control frames) is available.
Throws:
java.io.IOException If a problem occurs processing the frame. Any exception will trigger the closing of the WebSocket connection.
    public static WsFrame nextFrame(HttpEvent event,
            boolean blockthrows IOException {
        byte[] first = new byte[1];
        int read = 0;
        if (block) {
            read = event.getHttpServletRequest().getInputStream().read(first, 0, 1);
        } else {
            if (event.isReadReady()) {
                read = event.getHttpServletRequest().getInputStream().read(first, 0, 1);
            }
        }
        if (read == 1) {
            return new WsFrame(first[0], event);
        } else if (read == 0) {
            return null;
        } else if (read == -1) {
            throw new EOFException(.frameEos());
        } else {
            throw new IOException(.frameFailedRead(Integer.valueOf(read)));
        }
    }
New to GrepCode? Check out our FAQ X