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 import org.apache.hc.core5.util.Tokenizer;
37
38
39
40
41
42
43
44 public class BufferedHeader implements FormattedHeader, Serializable {
45
46 private static final long serialVersionUID = -2768352615787625448L;
47
48
49
50
51 private final String name;
52
53
54
55
56 private final CharArrayBuffer buffer;
57
58
59
60
61 private final int valuePos;
62
63
64
65
66 public static BufferedHeader create(final CharArrayBuffer buffer) {
67 try {
68 return new BufferedHeader(buffer);
69 } catch (final ParseException ex) {
70 throw new IllegalArgumentException(ex.getMessage());
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83 public BufferedHeader(final CharArrayBuffer buffer) throws ParseException {
84 this(buffer, true);
85 }
86
87 BufferedHeader(final CharArrayBuffer buffer, final boolean strict) throws ParseException {
88 super();
89 Args.notNull(buffer, "Char array buffer");
90 final int colon = buffer.indexOf(':');
91 if (colon <= 0) {
92 throw new ParseException("Invalid header", buffer, 0, buffer.length());
93 }
94 if (strict && Tokenizer.isWhitespace(buffer.charAt(colon - 1))) {
95 throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon - 1);
96 }
97 final String s = buffer.substringTrimmed(0, colon);
98 if (s.isEmpty()) {
99 throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon);
100 }
101 this.buffer = buffer;
102 this.name = s;
103 this.valuePos = colon + 1;
104 }
105
106 @Override
107 public String getName() {
108 return this.name;
109 }
110
111 @Override
112 public String getValue() {
113 return this.buffer.substringTrimmed(this.valuePos, this.buffer.length());
114 }
115
116 @Override
117 public boolean isSensitive() {
118 return false;
119 }
120
121 @Override
122 public int getValuePos() {
123 return this.valuePos;
124 }
125
126 @Override
127 public CharArrayBuffer getBuffer() {
128 return this.buffer;
129 }
130
131 @Override
132 public String toString() {
133 return this.buffer.toString();
134 }
135
136 }