001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/message/BufferedHeader.java $
003: * $Revision: 604625 $
004: * $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import org.apache.http.FormattedHeader;
035: import org.apache.http.HeaderElement;
036: import org.apache.http.ParseException;
037: import org.apache.http.util.CharArrayBuffer;
038:
039: /**
040: * This class represents a raw HTTP header whose content is parsed 'on demand'
041: * only when the header value needs to be consumed.
042: *
043: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
044: *
045: *
046: * <!-- empty lines above to avoid 'svn diff' context problems -->
047: * @version $Revision: 604625 $ $Date: 2007-12-16 15:11:11 +0100 (Sun, 16 Dec 2007) $
048: */
049: public class BufferedHeader implements FormattedHeader, Cloneable {
050:
051: /**
052: * Header name.
053: */
054: private final String name;
055:
056: /**
057: * The buffer containing the entire header line.
058: */
059: private final CharArrayBuffer buffer;
060:
061: /**
062: * The beginning of the header value in the buffer
063: */
064: private final int valuePos;
065:
066: /**
067: * Creates a new header from a buffer.
068: * The name of the header will be parsed immediately,
069: * the value only if it is accessed.
070: *
071: * @param buffer the buffer containing the header to represent
072: *
073: * @throws ParseException in case of a parse error
074: */
075: public BufferedHeader(final CharArrayBuffer buffer)
076: throws ParseException {
077:
078: super ();
079: if (buffer == null) {
080: throw new IllegalArgumentException(
081: "Char array buffer may not be null");
082: }
083: int colon = buffer.indexOf(':');
084: if (colon == -1) {
085: throw new ParseException("Invalid header: "
086: + buffer.toString());
087: }
088: String s = buffer.substringTrimmed(0, colon);
089: if (s.length() == 0) {
090: throw new ParseException("Invalid header: "
091: + buffer.toString());
092: }
093: this .buffer = buffer;
094: this .name = s;
095: this .valuePos = colon + 1;
096: }
097:
098: public String getName() {
099: return this .name;
100: }
101:
102: public String getValue() {
103: return this .buffer.substringTrimmed(this .valuePos, this .buffer
104: .length());
105: }
106:
107: public HeaderElement[] getElements() throws ParseException {
108: ParserCursor cursor = new ParserCursor(0, this .buffer.length());
109: cursor.updatePos(this .valuePos);
110: return BasicHeaderValueParser.DEFAULT.parseElements(
111: this .buffer, cursor);
112: }
113:
114: public int getValuePos() {
115: return this .valuePos;
116: }
117:
118: public CharArrayBuffer getBuffer() {
119: return this .buffer;
120: }
121:
122: public String toString() {
123: return this .buffer.toString();
124: }
125:
126: public Object clone() throws CloneNotSupportedException {
127: // buffer is considered immutable
128: // no need to make a copy of it
129: return super.clone();
130: }
131:
132: }
|