use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class ContentType method toString.
/**
* Generates textual representation of this content type which can be used as the value
* of a {@code Content-Type} header.
*/
@Override
public String toString() {
final CharArrayBuffer buf = new CharArrayBuffer(64);
buf.append(this.mimeType);
if (this.params != null) {
buf.append("; ");
BasicHeaderValueFormatter.INSTANCE.formatParameters(buf, this.params, false);
} else if (this.charset != null) {
buf.append("; charset=");
buf.append(this.charset.name());
}
return buf.toString();
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class BasicLineParser method parseRequestLine.
/**
* Parses a request line.
*
* @param buffer a buffer holding the line to parse
*
* @return the parsed request line
*
* @throws ParseException in case of a parse error
*/
@Override
public RequestLine parseRequestLine(final CharArrayBuffer buffer) throws ParseException {
Args.notNull(buffer, "Char array buffer");
final ParserCursor cursor = new ParserCursor(0, buffer.length());
this.tokenizer.skipWhiteSpace(buffer, cursor);
final String method = this.tokenizer.parseToken(buffer, cursor, BLANKS);
if (TextUtils.isEmpty(method)) {
throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
}
this.tokenizer.skipWhiteSpace(buffer, cursor);
final String uri = this.tokenizer.parseToken(buffer, cursor, BLANKS);
if (TextUtils.isEmpty(uri)) {
throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
}
final ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
this.tokenizer.skipWhiteSpace(buffer, cursor);
if (!cursor.atEnd()) {
throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos());
}
return new RequestLine(method, uri, ver);
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class MessageSupport method format.
public static Header format(final String name, final String... tokens) {
Args.notBlank(name, "Header name");
if (tokens == null || tokens.length == 0) {
return null;
}
final CharArrayBuffer buffer = new CharArrayBuffer(256);
buffer.append(name);
buffer.append(": ");
formatTokens(buffer, tokens);
return BufferedHeader.create(buffer);
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testNonAsciiReadWriteLine.
@Test
public void testNonAsciiReadWriteLine() throws Exception {
final String s1 = constructString(SWISS_GERMAN_HELLO);
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.ISO_8859_1.newEncoder());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
for (int i = 0; i < 10; i++) {
chbuffer.clear();
chbuffer.append(s1);
outbuffer.writeLine(chbuffer, outputStream);
}
chbuffer.clear();
outbuffer.writeLine(chbuffer, outputStream);
outbuffer.flush(outputStream);
final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
final long expected = ((s1.getBytes(StandardCharsets.ISO_8859_1).length + 2)) * 10 + 2;
Assertions.assertEquals(expected, bytesWritten);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.ISO_8859_1.newDecoder());
final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
for (int i = 0; i < 10; i++) {
chbuffer.clear();
final int len = inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(len, SWISS_GERMAN_HELLO.length);
Assertions.assertEquals(s1, chbuffer.toString());
}
chbuffer.clear();
Assertions.assertEquals(0, inBuffer.readLine(chbuffer, inputStream));
chbuffer.clear();
Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
chbuffer.clear();
Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
final long bytesRead = inBuffer.getMetrics().getBytesTransferred();
Assertions.assertEquals(expected, bytesRead);
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testMalformedInputActionReport.
@Test
public void testMalformedInputActionReport() throws Exception {
final byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(StandardCharsets.ISO_8859_1);
final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, decoder);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
Assertions.assertThrows(CharacterCodingException.class, () -> inBuffer.readLine(chbuffer, inputStream));
}
Aggregations