You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2009/01/06 00:57:58 UTC

svn commit: r731778 - in /webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter: HttpRequestFilter.java StreamUtil.java

Author: veithen
Date: Mon Jan  5 15:57:57 2009
New Revision: 731778

URL: http://svn.apache.org/viewvc?rev=731778&view=rev
Log:
Moved some reusable stuff to a utility class.

Added:
    webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java   (with props)
Modified:
    webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java

Modified: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java?rev=731778&r1=731777&r2=731778&view=diff
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java (original)
+++ webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/HttpRequestFilter.java Mon Jan  5 15:57:57 2009
@@ -32,12 +32,12 @@
         while (stream.available() > 0) {
             switch (state) {
                 case STATE_REQUEST: {
-                    int eol = searchEndOfLine(stream);
+                    int eol = StreamUtil.searchEndOfLine(stream);
                     if (eol == -1) {
                         // EOL not yet available; maybe next time...
                         return;
                     } else {
-                        String orgRequest = getString(stream, 0, eol);
+                        String orgRequest = StreamUtil.getAsciiString(stream, 0, eol);
                         String request = processRequest(orgRequest);
                         if (request == orgRequest) {
                             stream.skip(eol+2);
@@ -51,7 +51,7 @@
                     break;
                 }
                 case STATE_HEADER: {
-                    int eol = searchEndOfLine(stream);
+                    int eol = StreamUtil.searchEndOfLine(stream);
                     if (eol == -1) {
                         return;
                     }
@@ -66,12 +66,12 @@
                             break;
                         }
                     }
-                    String name = getString(stream, 0, colon);
+                    String name = StreamUtil.getAsciiString(stream, 0, colon);
                     int valueStart = colon+1;
                     while (stream.get(valueStart) == ' ') {
                         valueStart++;
                     }
-                    String orgValue = getString(stream, valueStart, eol);
+                    String orgValue = StreamUtil.getAsciiString(stream, valueStart, eol);
                     String value = processHeader(name, orgValue);
                     if (value == null) {
                         stream.discard(eol+2);
@@ -91,23 +91,6 @@
         }
     }
     
-    private static int searchEndOfLine(Stream stream) {
-        for (int i=0; i<stream.available()-1; i++) {
-            if (stream.get(i) == '\r' && stream.get(i+1) == '\n') {
-                return i;
-            }
-        }
-        return -1;
-    }
-    
-    private static String getString(Stream stream, int begin, int end) {
-        StringBuffer buffer = new StringBuffer(end-begin);
-        for (int i=begin; i<end; i++) {
-            buffer.append((char)stream.get(i));
-        }
-        return buffer.toString();
-    }
-    
     private static void insert(Stream stream, String s) throws IOException {
         byte[] b = s.getBytes("ascii");
         stream.insert(b, 0, b.length);

Added: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java?rev=731778&view=auto
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java (added)
+++ webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java Mon Jan  5 15:57:57 2009
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * 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 org.apache.ws.commons.tcpmon.core.filter;
+
+/**
+ * Class containing utility methods to work with streams.
+ */
+public class StreamUtil {
+    private StreamUtil() {}
+    
+    /**
+     * Search the available data in the stream for the first occurrence of
+     * the end of line sequence (\r\n).
+     * 
+     * @param stream the stream to search in
+     * @return the offset relative to the current position in the stream
+     *         where the first occurrence of the EOL sequence has been found,
+     *         or -1 if there is no EOL sequence in the available part of the
+     *         stream
+     */
+    public static int searchEndOfLine(Stream stream) {
+        for (int i=0; i<stream.available()-1; i++) {
+            if (stream.get(i) == '\r' && stream.get(i+1) == '\n') {
+                return i;
+            }
+        }
+        return -1;
+    }
+    
+    /**
+     * Convert a part of a stream to a string, using the ASCII charset encoding.
+     */
+    public static String getAsciiString(Stream stream, int begin, int end) {
+        StringBuffer buffer = new StringBuffer(end-begin);
+        for (int i=begin; i<end; i++) {
+            buffer.append((char)stream.get(i));
+        }
+        return buffer.toString();
+    }
+}

Propchange: webservices/commons/trunk/modules/tcpmon/src/org/apache/ws/commons/tcpmon/core/filter/StreamUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native