snichol     2002/11/29 13:42:39

  Modified:    java/src/org/apache/soap/util StringUtils.java
               java/src/org/apache/soap/util/net HTTPUtils.java
  Log:
  Submitted by: Pavel Ausianik <[EMAIL PROTECTED]>
  
  Please find a patch proposed for converting HTTP headers using own
  conversion instead of  using JVM byte to char converter, as it happens not
  to be efficient.
  
  Also I have concatenated several const Strings in StringBuffer.append.
  
  Modification by Scott Nichol: put getStringISO8859_1 into StringUtils to
  make it generally available.
  
  Revision  Changes    Path
  1.8       +25 -1     xml-soap/java/src/org/apache/soap/util/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/util/StringUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- StringUtils.java  6 Sep 2002 17:50:26 -0000       1.7
  +++ StringUtils.java  29 Nov 2002 21:42:38 -0000      1.8
  @@ -67,9 +67,11 @@
    * Deals with strings (probably need to elaborate some more).
    *
    * @author Matthew J. Duftler
  + * @author Pavel Ausianik &lt;[EMAIL PROTECTED]&gt;
    */
   public class StringUtils
   {
  +  public static final String ISO_8859_1 = "8859_1";
     public static final String lineSeparator =
       System.getProperty("line.separator", "\n");
     public static String URI_SEPARATION_CHAR = "@";
  @@ -280,5 +282,27 @@
        
     }
    
  -
  +  /**
  +   * Creates a String from byte array using direct byte to char conversion
  +   * (the same as ISO8859_1), using preallocated char array
  +   * main purpose is to workaround slow JDK conversion
  +   *
  +   * @param      value    array that is the source of characters.
  +   * @param      offset   the initial offset.
  +   * @param      count    the length.
  +   * @param      cbuf     preallocated char array
  +   *
  +   * @return <code>true</code> when the string matches against the pattern,
  +   *         <code>false</code> otherwise.
  +   */
  +  public static String getStringISO8859_1(byte[] value, int offset, int count, 
char[] cbuf)
  +      throws UnsupportedEncodingException  {
  +    if (count > cbuf.length)
  +        return new String(value, offset, count, ISO_8859_1);
  +
  +    // make own transformation
  +    for (int i = 0; i < count; i++)
  +      cbuf[i] = (char) value[i + offset];
  +    return new String(cbuf, 0, count);
  +  }
   }
  
  
  
  1.40      +18 -18    xml-soap/java/src/org/apache/soap/util/net/HTTPUtils.java
  
  Index: HTTPUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/util/net/HTTPUtils.java,v
  retrieving revision 1.39
  retrieving revision 1.40
  diff -u -r1.39 -r1.40
  --- HTTPUtils.java    20 Nov 2002 06:33:57 -0000      1.39
  +++ HTTPUtils.java    29 Nov 2002 21:42:39 -0000      1.40
  @@ -81,6 +81,7 @@
   import org.apache.soap.rpc.SOAPContext;
   import org.apache.soap.transport.TransportMessage;
   import org.apache.soap.util.MutableBoolean;
  +import org.apache.soap.util.StringUtils;
   
   /**
    * A bunch of utility stuff for doing HTTP things.
  @@ -100,7 +101,6 @@
     private static final String HTTP_VERSION = "1.0";
     private static final int    HTTP_DEFAULT_PORT = 80;
     private static final int    HTTPS_DEFAULT_PORT = 443;
  -  private static final String ISO_8859_1 = "8859_1";
   
     public  static final int    DEFAULT_OUTPUT_BUFFER_SIZE = 8 * 1024;
   
  @@ -110,7 +110,7 @@
     public static String encodeAuth(String userName, String password)
         throws SOAPException {
       try {
  -      return Base64.encode((userName + ":" + password).getBytes(ISO_8859_1));
  +      return Base64.encode((userName + ":" + 
password).getBytes(StringUtils.ISO_8859_1));
       } catch (UnsupportedEncodingException e) {
         throw new SOAPException (Constants.FAULT_CODE_CLIENT, e.getMessage(), e);
       }
  @@ -461,14 +461,13 @@
   
         /* Construct the HTTP header. */
         StringBuffer headerbuf = new StringBuffer(512);
  -      headerbuf.append(Constants.HEADER_POST).append(' ').append(URI)
  -          .append(" HTTP/").append(HTTP_VERSION).append("\r\n")
  -          .append(Constants.HEADER_HOST).append(": ").append(host)
  -          .append(':').append(port)
  -          .append("\r\n")
  -          .append(Constants.HEADER_CONTENT_TYPE).append(": ")
  -          .append(request.getContentType()).append("\r\n")
  -          .append(Constants.HEADER_CONTENT_LENGTH).append(": ")
  +      headerbuf.append(Constants.HEADER_POST + ' ').append(URI)
  +          .append(" HTTP/"+ HTTP_VERSION + "\r\n" +
  +                  Constants.HEADER_HOST + ": ").append(host)
  +          .append(':').append(port).append("\r\n" +
  +                  Constants.HEADER_CONTENT_TYPE + ": ")
  +          .append(request.getContentType()).append("\r\n" +
  +                  Constants.HEADER_CONTENT_LENGTH + ": ")
             .append(request.getContentLength()).append("\r\n");
         for (Enumeration e = request.getHeaderNames(); e.hasMoreElements(); ) {
             Object key = e.nextElement();
  @@ -493,6 +492,7 @@
   
         BufferedInputStream bInStream = new BufferedInputStream(inStream);
         byte[] linebuf = new byte[1024];
  +      char[] cbuf = new char[256];
         int count = 0;
         int b;
   
  @@ -506,7 +506,7 @@
             int codeStart = -1;
             int codeEnd = -1;
             int stringStart = -1;
  -    
  +
             for (count = 0, b = bInStream.read(); b != '\n' && b != -1; b = 
bInStream.read()) {
                 if (b != '\r') {
                          if (b == ' ') {
  @@ -534,17 +534,17 @@
                 throw new Exception("Reached end of stream while reading HTTP 
response status");
             if (codeStart == -1)
                 throw new Exception("HTTP response status not present");
  -          versionString = new String(linebuf, 0, versionEnd, ISO_8859_1);
  +          versionString = StringUtils.getStringISO8859_1(linebuf, 0, versionEnd, 
cbuf);
             if (codeEnd == -1)
                 codeEnd = count + 1;
  -          statusCode = Integer.parseInt(new String(linebuf, codeStart, codeEnd - 
codeStart, ISO_8859_1));
  +          statusCode = Integer.parseInt(StringUtils.getStringISO8859_1(linebuf, 
codeStart, codeEnd - codeStart, cbuf));
             if (stringStart != -1)
  -              statusString = new String(linebuf, stringStart, 1 + count - 
stringStart, ISO_8859_1);
  +              statusString = StringUtils.getStringISO8859_1(linebuf, stringStart, 1 
+ count - stringStart, cbuf);
             else
                 statusString = "";
         } catch (Exception e) {
             throw new SOAPException(Constants.FAULT_CODE_CLIENT,
  -              "Error parsing HTTP status line \"" + new String(linebuf, 0, count, 
ISO_8859_1) + "\": " + e, e);
  +              "Error parsing HTTP status line \"" + 
StringUtils.getStringISO8859_1(linebuf, 0, count, cbuf) + "\": " + e, e);
         }
   
         /* Read the HTTP headers. */
  @@ -580,13 +580,13 @@
                 if (nameEnd == -1)
                     throw new Exception("Incorrectly formed HTTP response header");
   
  -              String name = new String(linebuf, 0, nameEnd, ISO_8859_1);
  +              String name = StringUtils.getStringISO8859_1(linebuf, 0, nameEnd, 
cbuf);
                 String value = null;
                 if (valStart != -1) {
                      // Remove trailing ; to prevent ContentType from throwing 
exception
                      if (linebuf[count - 1] == ';')
                          --count;
  -                   value = new String(linebuf, valStart, count - valStart, 
ISO_8859_1);
  +                   value = StringUtils.getStringISO8859_1(linebuf, valStart, count 
- valStart, cbuf);
                  } else
                      value = "";
   
  @@ -608,7 +608,7 @@
             }
         } catch (Exception e) {
             throw new SOAPException(Constants.FAULT_CODE_CLIENT,
  -              "Error parsing HTTP header line \"" + new String(linebuf, 0, count, 
ISO_8859_1) + "\": " + e, e);
  +              "Error parsing HTTP header line \"" + 
StringUtils.getStringISO8859_1(linebuf, 0, count, cbuf) + "\": " + e, e);
         }
   
         boolean isOneWay = false;
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to