* The header line to parse.
* @return The header read or null.
* @throws IOException
*/
public static Parameter readHeader(CharSequence header) throws IOException {
Parameter result = null;
if (header.length() > 0) {
// Detect the end of headers
int start = 0;
int index = 0;
int next = header.charAt(index++);
if (isCarriageReturn(next)) {
next = header.charAt(index++);
if (!isLineFeed(next)) {
throw new IOException(
"Invalid end of headers. Line feed missing after the carriage return.");
}
} else {
result = new Parameter();
// Parse the header name
while ((index < header.length()) && (next != ':')) {
next = header.charAt(index++);
}
if (index == header.length()) {
throw new IOException(
"Unable to parse the header name. End of line reached too early.");
}
result.setName(header.subSequence(start, index - 1).toString());
next = header.charAt(index++);
while (isSpace(next)) {
// Skip any separator space between colon and header value
next = header.charAt(index++);
}
start = index - 1;
// Parse the header value
result.setValue(header.subSequence(start, header.length())
.toString());
}
}
return result;