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
32package org.apache.http.message;
33
34import org.apache.http.FormattedHeader;
35import org.apache.http.HeaderElement;
36import org.apache.http.ParseException;
37import 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 * @deprecated Please use {@link java.net.URL#openConnection} instead.
50 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
51 *     for further details.
52 */
53@Deprecated
54public class BufferedHeader implements FormattedHeader, Cloneable {
55
56    /**
57     * Header name.
58     */
59    private final String name;
60
61    /**
62     * The buffer containing the entire header line.
63     */
64    private final CharArrayBuffer buffer;
65
66    /**
67     * The beginning of the header value in the buffer
68     */
69    private final int valuePos;
70
71
72    /**
73     * Creates a new header from a buffer.
74     * The name of the header will be parsed immediately,
75     * the value only if it is accessed.
76     *
77     * @param buffer    the buffer containing the header to represent
78     *
79     * @throws ParseException   in case of a parse error
80     */
81    public BufferedHeader(final CharArrayBuffer buffer)
82        throws ParseException {
83
84        super();
85        if (buffer == null) {
86            throw new IllegalArgumentException
87                ("Char array buffer may not be null");
88        }
89        int colon = buffer.indexOf(':');
90        if (colon == -1) {
91            throw new ParseException
92                ("Invalid header: " + buffer.toString());
93        }
94        String s = buffer.substringTrimmed(0, colon);
95        if (s.length() == 0) {
96            throw new ParseException
97                ("Invalid header: " + buffer.toString());
98        }
99        this.buffer = buffer;
100        this.name = s;
101        this.valuePos = colon + 1;
102    }
103
104
105    public String getName() {
106        return this.name;
107    }
108
109    public String getValue() {
110        return this.buffer.substringTrimmed(this.valuePos, this.buffer.length());
111    }
112
113    public HeaderElement[] getElements() throws ParseException {
114        ParserCursor cursor = new ParserCursor(0, this.buffer.length());
115        cursor.updatePos(this.valuePos);
116        return BasicHeaderValueParser.DEFAULT
117            .parseElements(this.buffer, cursor);
118    }
119
120    public int getValuePos() {
121        return this.valuePos;
122    }
123
124    public CharArrayBuffer getBuffer() {
125        return this.buffer;
126    }
127
128    public String toString() {
129        return this.buffer.toString();
130    }
131
132    public Object clone() throws CloneNotSupportedException {
133        // buffer is considered immutable
134        // no need to make a copy of it
135        return super.clone();
136    }
137
138}
139