tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cos...@locus.apache.org
Subject cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util MimeHeaders.java
Date Tue, 29 Aug 2000 03:44:32 GMT
costin      00/08/28 20:44:31

  Modified:    src/facade22/org/apache/tomcat/facade
                        HttpServletResponseFacade.java
               src/facade23/org/apache/tomcat/facade23
                        HttpServletResponseFacade.java
               src/share/org/apache/tomcat/core Request.java Response.java
               src/share/org/apache/tomcat/request StaticInterceptor.java
               src/share/org/apache/tomcat/service/connector
                        Ajp12ConnectionHandler.java
                        Ajp13ConnectorRequest.java
                        JNIConnectionHandler.java
               src/share/org/apache/tomcat/service/http
                        HttpRequestAdapter.java HttpResponseAdapter.java
               src/share/org/apache/tomcat/util MimeHeaders.java
  Log:
  Step 2 in MimeHeaders refactoring.
  
  MessageBytes is now exposed and used instead of Strings. Most methods
  returning string are removed - if we do the conversion too early (before
  charset is detected ) we loose all the benefits and optimizations of
  MessageBytes.
  
  As proposed earlier, MessageBytes will gradually replace all Strings
  - toString() and setString() can be used to provide the old functionality.
  
  Also, rewrote the code to enumerate the header names and multi-values.
  The new code doesn't create GC ( Hashtable, Vector ) and it's probably
  (more) correct.
  
  I think the new code is manageable now, looking forward to review and
  feedback.
  
  ( BTW, the new methods are now getName() and getValue() instead of
  getHeaderName, etc. The reason is that the same object can be used
  to store the parameters in an efficient way)
  
  This shoud give a significant performance improvment when completed, but the
  main reasons for doing those changes are:
  -support for non-ascii charsets
  -refactoring - better, readable code
  
  Revision  Changes    Path
  1.3       +5 -5      jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletResponseFacade.java
  
  Index: HttpServletResponseFacade.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletResponseFacade.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- HttpServletResponseFacade.java	2000/08/28 06:08:15	1.2
  +++ HttpServletResponseFacade.java	2000/08/29 03:44:19	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletResponseFacade.java,v
1.2 2000/08/28 06:08:15 costin Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/08/28 06:08:15 $
  + * $Header: /home/cvs/jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletResponseFacade.java,v
1.3 2000/08/29 03:44:19 costin Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/08/29 03:44:19 $
    *
    * ====================================================================
    *
  @@ -249,12 +249,12 @@
   
       public void setDateHeader(String name, long date) {
   	MimeHeaders headers=response.getMimeHeaders();
  -	headers.setDateHeader( name, date );
  +	headers.setValue( name ).setTime( date );
       }
   
       public void addDateHeader(String name, long value) {
   	MimeHeaders headers=response.getMimeHeaders();
  -	headers.addDateHeader( name, value );
  +	headers.addValue( name ).setTime( value );
       }
   
       public void setHeader(String name, String value) {
  
  
  
  1.3       +5 -5      jakarta-tomcat/src/facade23/org/apache/tomcat/facade23/HttpServletResponseFacade.java
  
  Index: HttpServletResponseFacade.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/facade23/org/apache/tomcat/facade23/HttpServletResponseFacade.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- HttpServletResponseFacade.java	2000/08/28 06:08:15	1.2
  +++ HttpServletResponseFacade.java	2000/08/29 03:44:20	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/facade23/org/apache/tomcat/facade23/HttpServletResponseFacade.java,v
1.2 2000/08/28 06:08:15 costin Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/08/28 06:08:15 $
  + * $Header: /home/cvs/jakarta-tomcat/src/facade23/org/apache/tomcat/facade23/HttpServletResponseFacade.java,v
1.3 2000/08/29 03:44:20 costin Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/08/29 03:44:20 $
    *
    * ====================================================================
    *
  @@ -247,12 +247,12 @@
   
       public void setDateHeader(String name, long date) {
   	MimeHeaders headers=response.getMimeHeaders();
  -	headers.setDateHeader( name, date);
  +	headers.setValue( name ).setTime( date);
       }
   
       public void addDateHeader(String name, long value) {
   	MimeHeaders headers=response.getMimeHeaders();
  -	headers.addDateHeader( name, value );
  +	headers.addValue( name ).setTime( value );
       }
   
       public void setHeader(String name, String value) {
  
  
  
  1.57      +3 -6      jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java
  
  Index: Request.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java,v
  retrieving revision 1.56
  retrieving revision 1.57
  diff -u -r1.56 -r1.57
  --- Request.java	2000/08/27 20:37:29	1.56
  +++ Request.java	2000/08/29 03:44:20	1.57
  @@ -299,10 +299,9 @@
       public int getContentLength() {
           if( contentLength > -1 ) return contentLength;
   
  -	String value=getHeader( "content-length" );
  -	if( value==null) return -1;
  +	MessageBytes clB=headers.getValue("content-length");
  +        contentLength = (clB==null) ? -1 : clB.getInt();
   
  -	contentLength = Integer.parseInt(value);
   	return contentLength;
       }
   
  @@ -670,9 +669,7 @@
   
       // -------------------- Facade for MimeHeaders
       public Enumeration getHeaders(String name) {
  -	//	Vector v = reqA.getMimeHeaders().getHeadersVector(name);
  -	Vector v = getMimeHeaders().getHeadersVector(name);
  -	return v.elements();
  +	return getMimeHeaders().values(name);
       }
   
       // -------------------- Utils - facade for RequestUtil
  
  
  
  1.33      +11 -10    jakarta-tomcat/src/share/org/apache/tomcat/core/Response.java
  
  Index: Response.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Response.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- Response.java	2000/08/28 06:08:15	1.32
  +++ Response.java	2000/08/29 03:44:21	1.33
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Response.java,v 1.32
2000/08/28 06:08:15 costin Exp $
  - * $Revision: 1.32 $
  - * $Date: 2000/08/28 06:08:15 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Response.java,v 1.33
2000/08/29 03:44:21 costin Exp $
  + * $Revision: 1.33 $
  + * $Date: 2000/08/29 03:44:21 $
    *
    * ====================================================================
    *
  @@ -209,7 +209,7 @@
       }
   
       public boolean containsHeader(String name) {
  -	return headers.containsHeader(name);
  +	return headers.getHeader(name) != null;
       }
   
       public boolean isUsingStream() {
  @@ -245,7 +245,7 @@
   	    if( checkSpecialHeader(name, value) )
   		return;
   	}
  -	headers.setHeader(name, value);
  +	headers.setValue(name).setString( value);
       }
   
       public void addHeader(String name, String value) {
  @@ -255,7 +255,7 @@
   	    if( checkSpecialHeader(name, value) )
   		return;
   	}
  -	headers.addHeader(name, value);
  +	headers.addValue(name).setString( value );
       }
   
       
  @@ -426,11 +426,12 @@
   
           // Set the contentType for header output
           // Use the setContentType() method so encoding is set properly
  -        String newType = RequestUtil.constructLocalizedContentType(contentType, locale);
  +        String newType = RequestUtil.constructLocalizedContentType(contentType,
  +								   locale);
           setContentType(newType);
   
   	// only one header !
  -	headers.setHeader("Content-Language", contentLanguage);
  +	headers.setValue("Content-Language").setString( contentLanguage);
       }
   
       public String getCharacterEncoding() {
  @@ -444,7 +445,7 @@
           if (encoding != null) {
   	    characterEncoding = encoding;
           }
  -	headers.setHeader("Content-Type", contentType);
  +	headers.setValue("Content-Type").setString( contentType);
       }
   
       public String getContentType() {
  @@ -454,7 +455,7 @@
       public void setContentLength(int contentLength) {
           if( included ) return;
   	this.contentLength = contentLength;
  -	headers.setIntHeader("Content-Length", contentLength);
  +	headers.setValue("Content-Length").setInt(contentLength);
       }
   
       public int getContentLength() {
  
  
  
  1.16      +1 -1      jakarta-tomcat/src/share/org/apache/tomcat/request/StaticInterceptor.java
  
  Index: StaticInterceptor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/request/StaticInterceptor.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- StaticInterceptor.java	2000/08/28 06:08:16	1.15
  +++ StaticInterceptor.java	2000/08/29 03:44:28	1.16
  @@ -290,7 +290,7 @@
   
       static void setDateHeader( Response res, String name, long value ) {
   	MimeHeaders headers=res.getMimeHeaders();
  -	headers.setDateHeader( name, value );
  +	headers.setValue( name ).setTime( value );
       }
   
       /** All path checks that were part of DefaultServlet
  
  
  
  1.36      +1 -3      jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp12ConnectionHandler.java
  
  Index: Ajp12ConnectionHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp12ConnectionHandler.java,v
  retrieving revision 1.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- Ajp12ConnectionHandler.java	2000/08/28 06:08:17	1.35
  +++ Ajp12ConnectionHandler.java	2000/08/29 03:44:29	1.36
  @@ -365,7 +365,7 @@
   		case 3: // Header
   		    token1 = readString(ajpin, null);
   		    token2 = readString(ajpin, "");
  -		    headers.addHeader(token1.toLowerCase(), token2);
  +		    headers.addValue(token1).setString(token2);
   		    break;
   
   		case 254: // Signal
  @@ -447,8 +447,6 @@
   
   	//processCookies();
   
  -	contentLength = headers.getIntHeader("content-length");
  -	contentType = headers.getHeader("content-type");
   
   	// XXX
   	// detect for real whether or not we have more requests
  
  
  
  1.11      +6 -7      jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp13ConnectorRequest.java
  
  Index: Ajp13ConnectorRequest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp13ConnectorRequest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Ajp13ConnectorRequest.java	2000/08/28 06:08:17	1.10
  +++ Ajp13ConnectorRequest.java	2000/08/29 03:44:29	1.11
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp13ConnectorRequest.java,v
1.10 2000/08/28 06:08:17 costin Exp $
  - * $Revision: 1.10 $
  - * $Date: 2000/08/28 06:08:17 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp13ConnectorRequest.java,v
1.11 2000/08/29 03:44:29 costin Exp $
  + * $Revision: 1.11 $
  + * $Date: 2000/08/29 03:44:29 $
    *
    * ====================================================================
    *
  @@ -166,7 +166,7 @@
               }
   
               String hValue = msg.getString();
  -            headers.addHeader( hName , hValue );
  +            headers.addValue( hName ).setString( hValue );
               //log( "Head: " + hName + "=" + hValue);
           }
   
  @@ -230,9 +230,8 @@
               setScheme("https");
           }
   
  -        contentLength = headers.getIntHeader("content-length");
  -        contentType = headers.getHeader("content-type");
  -	//    	((BufferedServletInputStream)this.in).setLimit(contentLength);
  +	MessageBytes clB=headers.getValue("content-length");
  +        contentLength = (clB==null) ? -1 : clB.getInt();
       	if(contentLength > 0) {
       		/* Read present data */
       		int err = con.receive(msg);
  
  
  
  1.17      +4 -15     jakarta-tomcat/src/share/org/apache/tomcat/service/connector/JNIConnectionHandler.java
  
  Index: JNIConnectionHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/JNIConnectionHandler.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- JNIConnectionHandler.java	2000/08/28 06:08:17	1.16
  +++ JNIConnectionHandler.java	2000/08/29 03:44:29	1.17
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/JNIConnectionHandler.java,v
1.16 2000/08/28 06:08:17 costin Exp $
  - * $Revision: 1.16 $
  - * $Date: 2000/08/28 06:08:17 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/JNIConnectionHandler.java,v
1.17 2000/08/29 03:44:29 costin Exp $
  + * $Revision: 1.17 $
  + * $Date: 2000/08/29 03:44:29 $
    *
    * ====================================================================
    *
  @@ -300,7 +300,7 @@
               String []values = new String[nheaders];
               if(h.readHeaders(s, l, names, values) > 0) {
                   for(i = 0 ; i < nheaders ; i++) {
  -                    headers.addHeader(names[i].toLowerCase(), values[i]);
  +                    headers.addValue(names[i]).setString(values[i]);
                   }
               } else {
                   throw new IOException("Error: JNI implementation error");
  @@ -313,18 +313,7 @@
       	    requestURI = requestURI.substring(0, idQ);
           }
   
  -	    contentLength = headers.getIntHeader("content-length");
  -	    contentType = headers.getHeader("content-type");
       }
  -
  -//     public ServletInputStream getInputStream() throws IOException {
  -//         if(contentLength <= 0) {
  -//             throw new IOException("Empty input stream");
  -//         }
  -
  -//         in = new BufferedServletInputStream(this);
  -//     	return in;
  -//     }
   }
   
   
  
  
  
  1.28      +7 -6      jakarta-tomcat/src/share/org/apache/tomcat/service/http/HttpRequestAdapter.java
  
  Index: HttpRequestAdapter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/http/HttpRequestAdapter.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- HttpRequestAdapter.java	2000/08/28 06:08:17	1.27
  +++ HttpRequestAdapter.java	2000/08/29 03:44:29	1.28
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/http/HttpRequestAdapter.java,v
1.27 2000/08/28 06:08:17 costin Exp $
  - * $Revision: 1.27 $
  - * $Date: 2000/08/28 06:08:17 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/http/HttpRequestAdapter.java,v
1.28 2000/08/29 03:44:29 costin Exp $
  + * $Revision: 1.28 $
  + * $Date: 2000/08/29 03:44:29 $
    *
    * ====================================================================
    *
  @@ -243,7 +243,8 @@
   
   	while ((c = b[off++]) != ':' && c != ' ') {
   	    if (c == '\n') {
  -		loghelper.log("Parse error, empty line: " + new String( b, off, len ), Logger.ERROR);
  +		loghelper.log("Parse error, empty line: " +
  +			      new String( b, off, len ), Logger.ERROR);
   		return false;
   	    }
   	}
  @@ -265,8 +266,8 @@
   
   	while ((c = b[off++]) == ' ');
   
  -	headers.addBytesHeader( b, nS, nE,
  -				off-1, len - (off - start - 1));
  +	headers.addValue( b, nS, nE).
  +	    setBytes(b, off-1, len - (off - start - 1));
   	return true;
       }
   
  
  
  
  1.19      +4 -4      jakarta-tomcat/src/share/org/apache/tomcat/service/http/HttpResponseAdapter.java
  
  Index: HttpResponseAdapter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/http/HttpResponseAdapter.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- HttpResponseAdapter.java	2000/08/28 06:08:18	1.18
  +++ HttpResponseAdapter.java	2000/08/29 03:44:30	1.19
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/http/HttpResponseAdapter.java,v
1.18 2000/08/28 06:08:18 costin Exp $
  - * $Revision: 1.18 $
  - * $Date: 2000/08/28 06:08:18 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/http/HttpResponseAdapter.java,v
1.19 2000/08/29 03:44:30 costin Exp $
  + * $Revision: 1.19 $
  + * $Date: 2000/08/29 03:44:30 $
    *
    * ====================================================================
    *
  @@ -154,7 +154,7 @@
   	// This avoids redundant setting of date ( very expensive ).
   	// XXX XXX Check if IIS, NES do generate the date
   	if( false ) {
  -	    headers.setDateHeader(  "Date", System.currentTimeMillis());
  +	    headers.setValue(  "Date" ).setTime( System.currentTimeMillis());
   	}
   	
   	// Servlet Engine header will be set per/adapter - smarter adapters will
  
  
  
  1.13      +141 -228  jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaders.java
  
  Index: MimeHeaders.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaders.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- MimeHeaders.java	2000/08/29 00:58:26	1.12
  +++ MimeHeaders.java	2000/08/29 03:44:30	1.13
  @@ -1,8 +1,4 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaders.java,v
1.12 2000/08/29 00:58:26 nacho Exp $
  - * $Revision: 1.12 $
  - * $Date: 2000/08/29 00:58:26 $
  - *
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
  @@ -125,7 +121,8 @@
       /**
        * The header fields.
        */
  -    private MimeHeaderField[] headers = new MimeHeaderField[DEFAULT_HEADER_SIZE];
  +    private MimeHeaderField[] headers = new
  +	MimeHeaderField[DEFAULT_HEADER_SIZE];
   
       /**
        * The current number of header fields.
  @@ -148,6 +145,8 @@
   	count = 0;
       }
   
  +    // -------------------- Idx access to headers ----------
  +    
       /**
        * Returns the current number of header fields.
        */
  @@ -171,20 +170,24 @@
   	return n >= 0 && n < count ? headers[n].getValue() : null;
       }
   
  +    // -------------------- --------------------
  +
       /**
  -     * Finds and returns a header field with the given name.  If no such
  -     * field exists, null is returned.  If more than one such field is
  -     * in the header, an arbitrary one is returned.
  +     * Returns an enumeration of strings representing the header field names.
  +     * Field names may appear multiple times in this enumeration, indicating
  +     * that multiple fields with that name exist in this header.
        */
  -    private MimeHeaderField find(String name) {
  -        for (int i = 0; i < count; i++) {
  -	    if (headers[i].getName().equalsIgnoreCase(name)) {
  -                return headers[i];
  -            }
  -        }
  -        return null;
  +    public Enumeration names() {
  +	return new NamesEnumerator(this);
       }
   
  +    public Enumeration values(String name) {
  +	return new ValuesEnumerator(this, name);
  +    }
  +
  +    // -------------------- Adding headers --------------------
  +    
  +
       /**
        * Adds a partially constructed field to the header.  This
        * field has not had its name or value initialized.
  @@ -205,184 +208,64 @@
   	return mh;
       }
   
  -    /**
  -     * Returns an enumeration of strings representing the header field names.
  -     * Field names may appear multiple times in this enumeration, indicating
  -     * that multiple fields with that name exist in this header.
  -     */
  -    public Enumeration names() {
  -	return new MimeHeadersEnumerator(this);
  +    /** Create a new named header , return the MessageBytes
  +	container for the new value
  +    */
  +    public MessageBytes addValue( String name ) {
  + 	MimeHeaderField mh = createHeader();
  +	mh.getName().setString(name);
  +	return mh.getValue();
       }
   
  -    // NOTE:  All of these put/get "Header" calls should
  -    // be renamed to put/get "field" !!!  This object is
  -    // the header, and its components are called fields.
  -
  -    public void addBytesHeader(byte b[], int startN, int endN,
  -			       int startV, int endV)
  +    /** Create a new named header using un-translated byte[].
  +	The conversion to chars can be delayed until
  +	encoding is known.
  +     */
  +    public MessageBytes addValue(byte b[], int startN, int endN)
       {
   	MimeHeaderField mhf=createHeader();
   	mhf.getName().setBytes(b, startN, endN);
  -	mhf.getValue().setBytes(b, startV, endV);
  -    }
  -
  -    /**
  -     * Creates a new header field whose value is the specified string.
  -     * @param name the header name
  -     * @param s the header field string value
  -     */
  -    public void setHeader(String name, String s) {
  -	MimeHeaderField headerF= find( name );
  -	if( headerF == null )
  -	    headerF=addHeader( name );
  -	headerF.getValue().setString(s);
  -    }
  -
  -    public void addHeader(String name, String s) {
  -        addHeader(name).getValue().setString(s);
  -    }
  -
  -    /**
  -     * Creates a new header field whose value is the specified integer.
  -     * @param name the header name
  -     * @param i the header field integer value
  -     */
  -    public void setIntHeader(String name, int i) {
  -	MimeHeaderField headerF= find( name );
  -	if( headerF == null )
  -	    headerF=addHeader( name );
  -	headerF.getValue().setInt(i);
  -    }
  -
  -    public void addIntHeader(String name, int i) {
  -        addHeader(name).getValue().setInt(i);
  -    }
  -
  -    /**
  -     * Creates a new header field whose value is the specified time.
  -     * The encoding uses RFC 822 date format, as updated by RFC 1123.
  -     * @param name the header name
  -     * @param t the time in number of milliseconds since the epoch
  -     */
  -    public void setDateHeader(String name, long t) {
  -	MimeHeaderField headerF= find( name );
  -	if( headerF == null )
  -	    headerF=addHeader( name );
  -	headerF.getValue().setTime(t);
  -    }
  -
  -    public void addDateHeader(String name, long t) {
  -        addHeader(name).getValue().setTime(t);
  -    }
  -
  -    /**
  -     * Returns the string value of one of the headers with the
  -     * specified name.
  -     * @see getHeaders
  -     * @param name the header field name
  -     * @return the string value of the field, or null if none found
  -     */
  -    public String getHeader(String name) {
  -	MimeHeaderField mh = find(name);
  -
  -	return mh != null ? mh.getValue().toString() : null;
  -    }
  -
  -    /**
  -     * Returns the string value of all of the headers with the
  -     * specified name.
  -     * @see getHeader
  -     * @param name the header field name
  -     * @return array values of the fields, or null if none found
  -     */
  -    public String[] getHeaders(String name) {
  -	// XXX XXX XXX XXX XXX XXX
  -	Vector values = getHeadersVector(name);
  -
  -	if (values.size() > 0) {
  -	    String retval[] = new String[values.size()];
  -
  -	    for (int i = 0; i < retval.length; i++)
  -		retval[i] = (String)values.elementAt(i);
  -	    return retval;
  -	}
  -	return null;
  +	return mhf.getValue();
       }
   
  -    // XXX XXX XXX XXX XXX XXX 
  -    /** Same as getHeaders, return a Vector - avoid Vector-[]-Vector conversion
  -     */
  -    public Vector getHeadersVector(String name) {
  -	Vector values = new Vector();
  -
  -	for (int i = 0; i < count; i++) {
  -	    if (headers[i].getName().equalsIgnoreCase(name))
  -		values.addElement(headers[i].getValue().toString());
  +    /** Allow "set" operations - 
  +        return a MessageBytes container for the
  +	header value ( existing header or new
  +	if this .
  +    */
  +    public MessageBytes setValue( String name ) {
  + 	MessageBytes value=getValue(name);
  +	if( value == null ) {
  +	    MimeHeaderField mh = createHeader();
  +	    mh.getName().setString(name);
  +	    value=mh.getValue();
   	}
  -
  -	return values;
  -    }
  -
  -    /**
  -     * Returns the integer value of a header with the specified name.
  -     * @param name the header field name
  -     * @return the integer value of the header field, or -1 if the header
  -     *	       was not found
  -     * @exception NumberFormatException if the integer format was invalid
  -     */
  -
  -    public int getIntHeader(String name) throws NumberFormatException {
  -	MimeHeaderField mh = find(name);
  -
  -	return mh != null ? mh.getValue().getInt() : -1;
  -    }
  -
  -    /**
  -     * Returns the date value of a header with the specified name.
  -     * @param name the header field name
  -     * @return the date value of the header field in number of milliseconds
  -     *	       since the epoch, or -1 if the header was not found
  -     * @exception IllegalArgumentException if the date format was invalid
  -     */
  -    public long getDateHeader(String name) throws IllegalArgumentException {
  -	MimeHeaderField mh = find(name);
  -
  -	return mh != null ? mh.getValue().getTime() : -1;
  +	return value;
       }
   
  +    //-------------------- Getting headers --------------------
       /**
  -     * Returns the name of the nth header field where n >= 0. Returns null
  -     * if there were fewer than (n + 1) fields. This can be used to iterate
  -     * through all the fields in the header.
  -     */
  -    public String getHeaderName(int n) {
  -	return n >= 0 && n < count ? headers[n].getName().toString()
  -	    : null;
  -    }
  -
  -    /**
  -     * Returns the body of the nth header field where n >= 0. Returns null
  -     * if there were fewer than (n + 1) fields. This can be used along
  -     * with getHeaderName to iterate through all the fields in the header.
  +     * Finds and returns a header field with the given name.  If no such
  +     * field exists, null is returned.  If more than one such field is
  +     * in the header, an arbitrary one is returned.
        */
  -    public String getHeader(int n) {
  -	return n >= 0 && n < count ? headers[n].getValue().toString()
  -	    : null;
  +    public MessageBytes getValue(String name) {
  +        for (int i = 0; i < count; i++) {
  +	    if (headers[i].getName().equalsIgnoreCase(name)) {
  +                return headers[i].getValue();
  +            }
  +        }
  +        return null;
       }
  -
  -    /**
  -     * Returns the number of fields using a given field name.
  -     */
  -    public int getFieldCount (String name) {
  -	int retval = 0;
   
  -	for (int i = 0; i < count; i++)
  -	    if (headers [i].getName().equalsIgnoreCase(name))
  -		retval++;
  -
  -	return retval;
  +    // bad shortcut - it'll convert to string ( too early probably,
  +    // encoding is guessed very late )
  +    public String getHeader(String name) {
  +	MessageBytes mh = getValue(name);
  +	return mh != null ? mh.toString() : null;
       }
   
  +    // -------------------- Removing --------------------
       /**
        * Removes a header field with the specified name.  Does nothing
        * if such a field could not be found.
  @@ -405,69 +288,98 @@
   		i--;
   	    }
   	}
  -    }
  -
  -    /**
  -     * Returns true if the specified field is contained in the header,
  -     * otherwise returns false.
  -     * @param name the field name
  -     */
  -    public boolean containsHeader(String name) {
  -	return find(name) != null;
       }
  -
  -
  -    protected MimeHeaderField addHeader(String name) {
  - 	MimeHeaderField mh = createHeader();
  -
  -	mh.getName().setString(name);
  -
  -	return mh;
  -    }
  -    
  -    /**
  -     * Returns a lengthly string representation of the current header fields.
  -     */
  -    public String toString() {
  -	StringBuffer sb = new StringBuffer();
  -	sb.append("{");
  -	for (int i = 0; i < count; i++) {
  -	    sb.append("{");
  -	    sb.append(headers[i].toString());
  -	    sb.append("}");
  +}
   
  -	    if (i < count - 1) {
  -		sb.append(",");
  +/** Enumerate the distinct header names.
  +    Each nextElement() is O(n) ( a comparation is
  +    done with all previous elements ).
  +
  +    This is less frequesnt than add() -
  +    we want to keep add O(1).
  +*/
  +class NamesEnumerator implements Enumeration {
  +    int pos;
  +    int size;
  +    String next;
  +    MimeHeaders headers;
  +
  +    NamesEnumerator(MimeHeaders headers) {
  +	this.headers=headers;
  +	pos=0;
  +	size = headers.size();
  +	findNext();
  +    }
  +
  +    private void findNext() {
  +	next=null;
  +	for(  ; pos< size; pos++ ) {
  +	    next=headers.getName( pos ).toString();
  +	    for( int j=0; j<pos ; j++ ) {
  +		if( headers.getName( j ).equalsIgnoreCase( next )) {
  +		    // duplicate.
  +		    next=null;
  +		    break;
  +		}
   	    }
  +	    if( next!=null ) {
  +		// it's not a duplicate
  +		break;
  +	    }
   	}
  -	sb.append("}");
  +	// next time findNext is called it will try the
  +	// next element
  +	pos++;
  +    }
  +    
  +    public boolean hasMoreElements() {
  +	return next!=null;
  +    }
   
  -	return sb.toString();
  +    public Object nextElement() {
  +	String current=next;
  +	findNext();
  +	return current;
       }
   }
   
  -// XXX XXX XXX XXX Must be rewritten !!!
  -class MimeHeadersEnumerator implements Enumeration {
  -    private Hashtable hash;
  -    private Enumeration delegate;
  -
  -    MimeHeadersEnumerator(MimeHeaders headers) {
  -        // Store header names in a Hashtable to guarantee uniqueness
  -        // This has the side benefit of letting us use Hashtable's enumerator
  -        hash = new Hashtable();
  -        int size = headers.size();
  -        for (int i = 0; i < size; i++) {
  -            hash.put(headers.getHeaderName(i), "");
  -        }
  -        delegate = hash.keys();
  +/** Enumerate the values for a (possibly ) multiple
  +    value element.
  +*/
  +class ValuesEnumerator implements Enumeration {
  +    int pos;
  +    int size;
  +    MessageBytes next;
  +    MimeHeaders headers;
  +    String name;
  +
  +    ValuesEnumerator(MimeHeaders headers, String name) {
  +	this.headers=headers;
  +	pos=0;
  +	size = headers.size();
  +	findNext();
  +    }
  +
  +    private void findNext() {
  +	next=null;
  +	for( ; pos< size; pos++ ) {
  +	    MessageBytes n1=headers.getName( pos );
  +	    if( n1.equalsIgnoreCase( name )) {
  +		next=headers.getValue( pos );
  +		break;
  +	    }
  +	}
  +	pos++;
       }
  -
  +    
       public boolean hasMoreElements() {
  -	return delegate.hasMoreElements();
  +	return next!=null;
       }
   
       public Object nextElement() {
  -	return delegate.nextElement();
  +	MessageBytes current=next;
  +	findNext();
  +	return current.toString();
       }
   }
   
  @@ -475,7 +387,8 @@
       // multiple headers with same name - a linked list will
       // speed up name enumerations and search ( both cpu and
       // GC)
  -    MimeHeaderField next; 
  +    MimeHeaderField next;
  +    MimeHeaderField prev; 
       
       protected final MessageBytes nameB = new MessageBytes();
       protected final MessageBytes valueB = new MessageBytes();
  
  
  

Mime
View raw message