1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http.message;
29
30 import java.io.Serializable;
31
32 import org.apache.hc.core5.http.FormattedHeader;
33 import org.apache.hc.core5.http.ParseException;
34 import org.apache.hc.core5.util.Args;
35 import org.apache.hc.core5.util.CharArrayBuffer;
36
37
38
39
40
41
42
43 public class BufferedHeader implements FormattedHeader, Serializable {
44
45 private static final long serialVersionUID = -2768352615787625448L;
46
47
48
49
50 private final String name;
51
52
53
54
55 private final CharArrayBuffer buffer;
56
57
58
59
60 private final int valuePos;
61
62
63
64
65 public static BufferedHeader create(final CharArrayBuffer buffer) {
66 try {
67 return new BufferedHeader(buffer);
68 } catch (final ParseException ex) {
69 throw new IllegalArgumentException(ex.getMessage());
70 }
71 }
72
73
74
75
76
77
78
79
80
81
82 public BufferedHeader(final CharArrayBuffer buffer) throws ParseException {
83 this(buffer, true);
84 }
85
86 BufferedHeader(final CharArrayBuffer buffer, final boolean strict) throws ParseException {
87 super();
88 Args.notNull(buffer, "Char array buffer");
89 final int colon = buffer.indexOf(':');
90 if (colon <= 0) {
91 throw new ParseException("Invalid header", buffer, 0, buffer.length());
92 }
93 if (strict && TokenParser.isWhitespace(buffer.charAt(colon - 1))) {
94 throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon - 1);
95 }
96 final String s = buffer.substringTrimmed(0, colon);
97 if (s.isEmpty()) {
98 throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon);
99 }
100 this.buffer = buffer;
101 this.name = s;
102 this.valuePos = colon + 1;
103 }
104
105 @Override
106 public String getName() {
107 return this.name;
108 }
109
110 @Override
111 public String getValue() {
112 return this.buffer.substringTrimmed(this.valuePos, this.buffer.length());
113 }
114
115 @Override
116 public boolean isSensitive() {
117 return false;
118 }
119
120 @Override
121 public int getValuePos() {
122 return this.valuePos;
123 }
124
125 @Override
126 public CharArrayBuffer getBuffer() {
127 return this.buffer;
128 }
129
130 @Override
131 public String toString() {
132 return this.buffer.toString();
133 }
134
135 }