Search in sources :

Example 36 with CharArrayBuffer

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());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 37 with CharArrayBuffer

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);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) HttpTransportMetrics(org.apache.hc.core5.http.io.HttpTransportMetrics) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 38 with CharArrayBuffer

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));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 39 with CharArrayBuffer

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 40 with CharArrayBuffer

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));
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CharsetEncoder(java.nio.charset.CharsetEncoder) Test(org.junit.jupiter.api.Test)

Aggregations

CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)91 Test (org.junit.jupiter.api.Test)74 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)13 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)11 ReadableByteChannel (java.nio.channels.ReadableByteChannel)10 Header (org.apache.hc.core5.http.Header)10 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)10 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)10 HeaderElement (org.apache.hc.core5.http.HeaderElement)9 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)9 NameValuePair (org.apache.hc.core5.http.NameValuePair)8 MessageConstraintException (org.apache.hc.core5.http.MessageConstraintException)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 CharsetEncoder (java.nio.charset.CharsetEncoder)6 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)6 FormattedHeader (org.apache.hc.core5.http.FormattedHeader)5