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;
 
 
This class is used to read WebSocket frames from the underlying socket and makes the payload available for reading as an java.io.InputStream. It only makes the number of bytes declared in the payload length available for reading even if more bytes are available from the socket.
 
 public class WsInputStream extends InputStream {
 
     private final HttpEvent event;
     private final WsOutbound outbound;
 
     private WsFrame frame;
     private long remaining;
     private long readThisFragment;
 
     private String error = null;
 
 
     public WsInputStream(HttpEvent eventWsOutbound outbound) {
         this. = event;
         this. = outbound;
     }


    
Process the next WebSocket frame.

Parameters:
block Should this method block until a frame is presented if no data is currently available to process. Note that if a single byte is available, this method will block until the complete frame (excluding payload for non-control frames) is available.
Returns:
The next frame to be processed or null if block is false and there is no data to be processed.
Throws:
java.io.IOException If a problem occurs reading the data for the frame.
 
     public WsFrame nextFrame(boolean blockthrows IOException {
          = WsFrame.nextFrame(block);
         if ( != null) {
              = 0;
              = .getPayLoadLength();
         }
         return ;
     }
 
 
     // ----------------------------------------------------- InputStream methods
 
     @Override
     public int read() throws IOException {
 
         makePayloadDataAvailable();
 
         if ( == 0) {
             return -1;
         }
 
         --;
         ++;
 
         int masked = .getHttpServletRequest().getInputStream().read();
         if(masked == -1) {
             return -1;
         }
         return masked ^
                 (.getMask()[(int) (( - 1) % 4)] & 0xFF);
     }
 
 
     @Override
     public int read(byte b[], int offint lenthrows IOException {
 
        makePayloadDataAvailable();
        if ( == 0) {
            return -1;
        }
        if (len > ) {
            len = (int;
        }
        int result = .getHttpServletRequest().getInputStream().read(bofflen);
        if(result == -1) {
            return -1;
        }
        for (int i = offi < off + resulti++) {
            b[i] = (byte) (b[i] ^
                    .getMask()[(int) (( + i - off) % 4)]);
        }
         -= result;
         += result;
        return result;
    }
    /*
     * Ensures that there is payload data ready to read.
     */
    private void makePayloadDataAvailable() throws IOException {
        if ( != null) {
            throw new IOException();
        }
        while ( == 0 && !.getFin()) {
            // Need more data - process next frame
            nextFrame(true);
            while (.isControl()) {
                if (.getOpCode() == .) {
                    .pong(.getPayLoad());
                } else if (.getOpCode() == .) {
                    // NO-OP. Swallow it.
                } else if (.getOpCode() == .) {
                    .close();
                } else{
                    throw new IOException(.frameUnknownOpcode(Byte.valueOf(.getOpCode())));
                }
                nextFrame(true);
            }
            if (.getOpCode() != .) {
                 = .frameNotContinuation(Byte.valueOf(.getOpCode()));
                throw new IOException();
            }
        }
    }
New to GrepCode? Check out our FAQ X