use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testMultibyteCodedReadWriteLine.
@Test
public void testMultibyteCodedReadWriteLine() throws Exception {
final String s1 = constructString(SWISS_GERMAN_HELLO);
final String s2 = constructString(RUSSIAN_HELLO);
final String s3 = "Like hello and stuff";
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.UTF_8.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();
chbuffer.append(s2);
outbuffer.writeLine(chbuffer, outputStream);
chbuffer.clear();
chbuffer.append(s3);
outbuffer.writeLine(chbuffer, outputStream);
}
outbuffer.flush(outputStream);
final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
final long expected = ((s1.getBytes(StandardCharsets.UTF_8).length + 2) + (s2.getBytes(StandardCharsets.UTF_8).length + 2) + (s3.getBytes(StandardCharsets.UTF_8).length + 2)) * 10;
Assertions.assertEquals(expected, bytesWritten);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder());
final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
for (int i = 0; i < 10; i++) {
chbuffer.clear();
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(s1, chbuffer.toString());
chbuffer.clear();
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(s2, chbuffer.toString());
chbuffer.clear();
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(s3, chbuffer.toString());
}
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 testUnmappableInputActionReplace.
@Test
public void testUnmappableInputActionReplace() throws Exception {
final String s = "This text contains a circumflex \u0302 !!!";
final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder();
encoder.onMalformedInput(CodingErrorAction.IGNORE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
chbuffer.append(s);
outbuffer.writeLine(chbuffer, outputStream);
outbuffer.flush(outputStream);
final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1);
Assertions.assertEquals("This text contains a circumflex ? !!!\r\n", result);
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testMultibyteCodedReadWriteLongLine.
@Test
public void testMultibyteCodedReadWriteLongLine() throws Exception {
final String s1 = constructString(SWISS_GERMAN_HELLO);
final String s2 = constructString(RUSSIAN_HELLO);
final String s3 = "Like hello and stuff";
final StringBuilder buf = new StringBuilder();
for (int i = 0; i < 1024; i++) {
buf.append(s1).append(s2).append(s3);
}
final String s = buf.toString();
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.UTF_8.newEncoder());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
chbuffer.append(s);
outbuffer.writeLine(chbuffer, outputStream);
outbuffer.flush(outputStream);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder());
final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
chbuffer.clear();
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(s, chbuffer.toString());
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testMalformedInputActionReplace.
@Test
public void testMalformedInputActionReplace() throws Exception {
final byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(StandardCharsets.ISO_8859_1);
final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, decoder);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals("Gr\ufffdezi_z\ufffdm\ufffd", chbuffer.toString());
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class EntityUtils method parse.
/**
* Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
* The encoding is taken from the entity's Content-Encoding header.
* <p>
* This is typically used while parsing an HTTP POST.
* </p>
*
* @param entity
* The entity to parse
* @param maxStreamLength
* The maximum size of the stream to read; use it to guard against unreasonable or malicious processing.
* @return a list of {@link NameValuePair} as built from the URI's query portion.
* @throws IOException
* If there was an exception getting the entity's data.
*/
public static List<NameValuePair> parse(final HttpEntity entity, final int maxStreamLength) throws IOException {
Args.notNull(entity, "HttpEntity");
final int contentLength = toContentLength((int) Args.checkContentLength(entity));
final ContentType contentType = ContentType.parse(entity.getContentType());
if (!ContentType.APPLICATION_FORM_URLENCODED.isSameMimeType(contentType)) {
return Collections.emptyList();
}
final Charset charset = contentType.getCharset(DEFAULT_CHARSET);
final CharArrayBuffer buf;
try (final InputStream inStream = entity.getContent()) {
if (inStream == null) {
return Collections.emptyList();
}
buf = toCharArrayBuffer(inStream, contentLength, charset, maxStreamLength);
}
if (buf.isEmpty()) {
return Collections.emptyList();
}
return WWWFormCodec.parse(buf, charset);
}
Aggregations