Author: markt Date: Mon Feb 27 11:04:12 2012 New Revision: 1294104 URL: http://svn.apache.org/viewvc?rev=1294104&view=rev Log: Refactor to reduce duplication
Modified: tomcat/trunk/java/org/apache/catalina/websocket/WsInputStream.java Modified: tomcat/trunk/java/org/apache/catalina/websocket/WsInputStream.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/WsInputStream.java?rev=1294104&r1=1294103&r2=1294104&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/websocket/WsInputStream.java (original) +++ tomcat/trunk/java/org/apache/catalina/websocket/WsInputStream.java Mon Feb 27 11:04:12 2012 @@ -67,31 +67,8 @@ public class WsInputStream extends java. @Override public int read() throws IOException { - if (error != null) { - throw new IOException(error); - } - while (remaining == 0 && !getFrame().getFin()) { - // Need more data - process next frame - processFrame(); - while (frame.isControl()) { - if (getFrame().getOpCode() == Constants.OPCODE_PING) { - outbound.pong(frame.getPayLoad()); - } else if (getFrame().getOpCode() == Constants.OPCODE_PONG) { - // NO-OP. Swallow it. - } else if (getFrame().getOpCode() == Constants.OPCODE_CLOSE) { - outbound.close(frame); - } else{ - throw new IOException(sm.getString("is.unknownOpCode", - Byte.valueOf(getFrame().getOpCode()))); - } - processFrame(); - } - if (getFrame().getOpCode() != Constants.OPCODE_CONTINUATION) { - error = sm.getString("is.notContinutation", - Byte.valueOf(getFrame().getOpCode())); - throw new IOException(error); - } - } + + makePayloadDataAvailable(); if (remaining == 0) { return -1; @@ -111,6 +88,35 @@ public class WsInputStream extends java. @Override public int read(byte b[], int off, int len) throws IOException { + + makePayloadDataAvailable(); + + if (remaining == 0) { + return -1; + } + + if (len > remaining) { + len = (int) remaining; + } + int result = processor.read(b, off, len); + if(result == -1) { + return -1; + } + + for (int i = off; i < off + result; i++) { + b[i] = (byte) (b[i] ^ + frame.getMask()[(int) ((readThisFragment + i - off) % 4)]); + } + remaining -= result; + readThisFragment += result; + return result; + } + + + /* + * Ensures that there is payload data ready to read. + */ + private void makePayloadDataAvailable() throws IOException { if (error != null) { throw new IOException(error); } @@ -136,26 +142,5 @@ public class WsInputStream extends java. throw new IOException(error); } } - - if (remaining == 0) { - return -1; - } - - if (len > remaining) { - len = (int) remaining; - } - int result = processor.read(b, off, len); - if(result == -1) { - return -1; - } - - for (int i = off; i < off + result; i++) { - b[i] = (byte) (b[i] ^ - frame.getMask()[(int) ((readThisFragment + i - off) % 4)]); - } - remaining -= result; - readThisFragment += result; - return result; } - } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org