1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BufferedHeader.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,129 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.message; 1.32 + 1.33 +import java.io.Serializable; 1.34 + 1.35 +import ch.boye.httpclientandroidlib.FormattedHeader; 1.36 +import ch.boye.httpclientandroidlib.HeaderElement; 1.37 +import ch.boye.httpclientandroidlib.ParseException; 1.38 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.39 + 1.40 +/** 1.41 + * This class represents a raw HTTP header whose content is parsed 'on demand' 1.42 + * only when the header value needs to be consumed. 1.43 + * 1.44 + * @since 4.0 1.45 + */ 1.46 +public class BufferedHeader implements FormattedHeader, Cloneable, Serializable { 1.47 + 1.48 + private static final long serialVersionUID = -2768352615787625448L; 1.49 + 1.50 + /** 1.51 + * Header name. 1.52 + */ 1.53 + private final String name; 1.54 + 1.55 + /** 1.56 + * The buffer containing the entire header line. 1.57 + */ 1.58 + private final CharArrayBuffer buffer; 1.59 + 1.60 + /** 1.61 + * The beginning of the header value in the buffer 1.62 + */ 1.63 + private final int valuePos; 1.64 + 1.65 + 1.66 + /** 1.67 + * Creates a new header from a buffer. 1.68 + * The name of the header will be parsed immediately, 1.69 + * the value only if it is accessed. 1.70 + * 1.71 + * @param buffer the buffer containing the header to represent 1.72 + * 1.73 + * @throws ParseException in case of a parse error 1.74 + */ 1.75 + public BufferedHeader(final CharArrayBuffer buffer) 1.76 + throws ParseException { 1.77 + 1.78 + super(); 1.79 + if (buffer == null) { 1.80 + throw new IllegalArgumentException 1.81 + ("Char array buffer may not be null"); 1.82 + } 1.83 + int colon = buffer.indexOf(':'); 1.84 + if (colon == -1) { 1.85 + throw new ParseException 1.86 + ("Invalid header: " + buffer.toString()); 1.87 + } 1.88 + String s = buffer.substringTrimmed(0, colon); 1.89 + if (s.length() == 0) { 1.90 + throw new ParseException 1.91 + ("Invalid header: " + buffer.toString()); 1.92 + } 1.93 + this.buffer = buffer; 1.94 + this.name = s; 1.95 + this.valuePos = colon + 1; 1.96 + } 1.97 + 1.98 + 1.99 + public String getName() { 1.100 + return this.name; 1.101 + } 1.102 + 1.103 + public String getValue() { 1.104 + return this.buffer.substringTrimmed(this.valuePos, this.buffer.length()); 1.105 + } 1.106 + 1.107 + public HeaderElement[] getElements() throws ParseException { 1.108 + ParserCursor cursor = new ParserCursor(0, this.buffer.length()); 1.109 + cursor.updatePos(this.valuePos); 1.110 + return BasicHeaderValueParser.DEFAULT 1.111 + .parseElements(this.buffer, cursor); 1.112 + } 1.113 + 1.114 + public int getValuePos() { 1.115 + return this.valuePos; 1.116 + } 1.117 + 1.118 + public CharArrayBuffer getBuffer() { 1.119 + return this.buffer; 1.120 + } 1.121 + 1.122 + public String toString() { 1.123 + return this.buffer.toString(); 1.124 + } 1.125 + 1.126 + public Object clone() throws CloneNotSupportedException { 1.127 + // buffer is considered immutable 1.128 + // no need to make a copy of it 1.129 + return super.clone(); 1.130 + } 1.131 + 1.132 +}