use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testMalformedInputActionIgnore.
@Test
public void testMalformedInputActionIgnore() throws Exception {
final byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(StandardCharsets.ISO_8859_1);
final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
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("Grezi_zm", chbuffer.toString());
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testBasicReadWriteLine.
@Test
public void testBasicReadWriteLine() throws Exception {
final String[] teststrs = new String[5];
teststrs[0] = "Hello";
teststrs[1] = "This string should be much longer than the size of the output buffer " + "which is only 16 bytes for this test";
final StringBuilder buffer = new StringBuilder();
for (int i = 0; i < 15; i++) {
buffer.append("123456789 ");
}
buffer.append("and stuff like that");
teststrs[2] = buffer.toString();
teststrs[3] = "";
teststrs[4] = "And goodbye";
final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (final String teststr : teststrs) {
chbuffer.clear();
chbuffer.append(teststr);
outbuffer.writeLine(chbuffer, outputStream);
}
// these write operations should have no effect
outbuffer.writeLine(null, outputStream);
outbuffer.flush(outputStream);
HttpTransportMetrics tmetrics = outbuffer.getMetrics();
final long bytesWritten = tmetrics.getBytesTransferred();
long expected = 0;
for (final String teststr : teststrs) {
expected += (teststr.length() + 2);
}
Assertions.assertEquals(expected, bytesWritten);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
for (final String teststr : teststrs) {
chbuffer.clear();
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(teststr, chbuffer.toString());
}
chbuffer.clear();
Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
chbuffer.clear();
Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
tmetrics = inBuffer.getMetrics();
final long bytesRead = tmetrics.getBytesTransferred();
Assertions.assertEquals(expected, bytesRead);
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testLineLimit.
@Test
public void testLineLimit() throws Exception {
final String s = "LoooooooooooooooooooooooooOOOOOOOOOOOOOOOOOOoooooooooooooooooooooong line\r\n";
final CharArrayBuffer line = new CharArrayBuffer(64);
final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(128, 128);
final ReadableByteChannel channel1 = newChannel(s);
inbuf1.fill(channel1);
Assertions.assertTrue(inbuf1.readLine(line, false));
line.clear();
final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(128, 128, 10);
final ReadableByteChannel channel2 = newChannel(s);
inbuf2.fill(channel2);
Assertions.assertThrows(MessageConstraintException.class, () -> inbuf2.readLine(line, false));
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testWriteLineChunks.
@Test
public void testWriteLineChunks() throws Exception {
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, 0);
ReadableByteChannel inChannel = newChannel("One\r\nTwo\r\nThree");
inbuf.fill(inChannel);
final CharArrayBuffer line = new CharArrayBuffer(64);
line.clear();
Assertions.assertTrue(inbuf.readLine(line, false));
Assertions.assertEquals("One", line.toString());
outbuf.writeLine(line);
line.clear();
Assertions.assertTrue(inbuf.readLine(line, false));
Assertions.assertEquals("Two", line.toString());
outbuf.writeLine(line);
line.clear();
Assertions.assertFalse(inbuf.readLine(line, false));
inChannel = newChannel("\r\nFour");
inbuf.fill(inChannel);
line.clear();
Assertions.assertTrue(inbuf.readLine(line, false));
Assertions.assertEquals("Three", line.toString());
outbuf.writeLine(line);
inbuf.fill(inChannel);
line.clear();
Assertions.assertTrue(inbuf.readLine(line, true));
Assertions.assertEquals("Four", line.toString());
outbuf.writeLine(line);
line.clear();
Assertions.assertFalse(inbuf.readLine(line, true));
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
final WritableByteChannel outChannel = newChannel(outStream);
outbuf.flush(outChannel);
final String s = new String(outStream.toByteArray(), StandardCharsets.US_ASCII);
Assertions.assertEquals("One\r\nTwo\r\nThree\r\nFour\r\n", s);
}
use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testUnmappableInputActionReport.
@Test
public void testUnmappableInputActionReport() 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.REPORT);
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
chbuffer.append(s);
Assertions.assertThrows(CharacterCodingException.class, () -> outbuffer.writeLine(chbuffer, outputStream));
}
Aggregations