Home | History | Annotate | Download | only in message
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BufferedHeader.java $
      3  * $Revision: 604625 $
      4  * $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.message;
     33 
     34 import org.apache.http.FormattedHeader;
     35 import org.apache.http.HeaderElement;
     36 import org.apache.http.ParseException;
     37 import org.apache.http.util.CharArrayBuffer;
     38 
     39 /**
     40  * This class represents a raw HTTP header whose content is parsed 'on demand'
     41  * only when the header value needs to be consumed.
     42  *
     43  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     44  *
     45  *
     46  * <!-- empty lines above to avoid 'svn diff' context problems -->
     47  * @version $Revision: 604625 $ $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $
     48  */
     49 public class BufferedHeader implements FormattedHeader, Cloneable {
     50 
     51     /**
     52      * Header name.
     53      */
     54     private final String name;
     55 
     56     /**
     57      * The buffer containing the entire header line.
     58      */
     59     private final CharArrayBuffer buffer;
     60 
     61     /**
     62      * The beginning of the header value in the buffer
     63      */
     64     private final int valuePos;
     65 
     66 
     67     /**
     68      * Creates a new header from a buffer.
     69      * The name of the header will be parsed immediately,
     70      * the value only if it is accessed.
     71      *
     72      * @param buffer    the buffer containing the header to represent
     73      *
     74      * @throws ParseException   in case of a parse error
     75      */
     76     public BufferedHeader(final CharArrayBuffer buffer)
     77         throws ParseException {
     78 
     79         super();
     80         if (buffer == null) {
     81             throw new IllegalArgumentException
     82                 ("Char array buffer may not be null");
     83         }
     84         int colon = buffer.indexOf(':');
     85         if (colon == -1) {
     86             throw new ParseException
     87                 ("Invalid header: " + buffer.toString());
     88         }
     89         String s = buffer.substringTrimmed(0, colon);
     90         if (s.length() == 0) {
     91             throw new ParseException
     92                 ("Invalid header: " + buffer.toString());
     93         }
     94         this.buffer = buffer;
     95         this.name = s;
     96         this.valuePos = colon + 1;
     97     }
     98 
     99 
    100     public String getName() {
    101         return this.name;
    102     }
    103 
    104     public String getValue() {
    105         return this.buffer.substringTrimmed(this.valuePos, this.buffer.length());
    106     }
    107 
    108     public HeaderElement[] getElements() throws ParseException {
    109         ParserCursor cursor = new ParserCursor(0, this.buffer.length());
    110         cursor.updatePos(this.valuePos);
    111         return BasicHeaderValueParser.DEFAULT
    112             .parseElements(this.buffer, cursor);
    113     }
    114 
    115     public int getValuePos() {
    116         return this.valuePos;
    117     }
    118 
    119     public CharArrayBuffer getBuffer() {
    120         return this.buffer;
    121     }
    122 
    123     public String toString() {
    124         return this.buffer.toString();
    125     }
    126 
    127     public Object clone() throws CloneNotSupportedException {
    128         // buffer is considered immutable
    129         // no need to make a copy of it
    130         return super.clone();
    131     }
    132 
    133 }
    134