Search in sources :

Example 41 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestChunkCoding method testChunkedOutputStreamLargeChunk.

@Test
public void testChunkedOutputStreamLargeChunk() throws IOException {
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
    out.write(new byte[] { '1', '2', '3', '4' });
    out.finish();
    out.close();
    final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
    Assertions.assertEquals("4\r\n1234\r\n0\r\n\r\n", content);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 42 with SessionOutputBuffer

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

Example 43 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestSessionInOutBuffers method testReadWriteBytes.

@Test
public void testReadWriteBytes() throws Exception {
    // make the buffer larger than that of outbuffer
    final byte[] out = new byte[40];
    for (int i = 0; i < out.length; i++) {
        out[i] = (byte) ('0' + i);
    }
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int off = 0;
    int remaining = out.length;
    while (remaining > 0) {
        int chunk = 10;
        if (chunk > remaining) {
            chunk = remaining;
        }
        outbuffer.write(out, off, chunk, outputStream);
        off += chunk;
        remaining -= chunk;
    }
    outbuffer.flush(outputStream);
    final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(out.length, bytesWritten);
    final byte[] tmp = outputStream.toByteArray();
    Assertions.assertEquals(out.length, tmp.length);
    for (int i = 0; i < out.length; i++) {
        Assertions.assertEquals(out[i], tmp[i]);
    }
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
    // these read operations will have no effect
    Assertions.assertEquals(0, inBuffer.read(null, 0, 10, inputStream));
    Assertions.assertEquals(0, inBuffer.read(null, inputStream));
    long bytesRead = inBuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(0, bytesRead);
    final byte[] in = new byte[40];
    off = 0;
    remaining = in.length;
    while (remaining > 0) {
        int chunk = 10;
        if (chunk > remaining) {
            chunk = remaining;
        }
        final int readLen = inBuffer.read(in, off, chunk, inputStream);
        if (readLen == -1) {
            break;
        }
        off += readLen;
        remaining -= readLen;
    }
    for (int i = 0; i < out.length; i++) {
        Assertions.assertEquals(out[i], in[i]);
    }
    Assertions.assertEquals(-1, inBuffer.read(tmp, inputStream));
    Assertions.assertEquals(-1, inBuffer.read(tmp, inputStream));
    bytesRead = inBuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(out.length, bytesRead);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 44 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestSessionInOutBuffers method testUnmappableInputActionIgnore.

@Test
public void testUnmappableInputActionIgnore() 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.IGNORE);
    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);
}
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)

Example 45 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestSessionInOutBuffers method testBasicBufferProperties.

@Test
public void testBasicBufferProperties() throws Exception {
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1, 2, 3 });
    Assertions.assertEquals(16, inBuffer.capacity());
    Assertions.assertEquals(16, inBuffer.available());
    Assertions.assertEquals(0, inBuffer.length());
    inBuffer.read(inputStream);
    Assertions.assertEquals(14, inBuffer.available());
    Assertions.assertEquals(2, inBuffer.length());
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Assertions.assertEquals(16, outbuffer.capacity());
    Assertions.assertEquals(16, outbuffer.available());
    Assertions.assertEquals(0, outbuffer.length());
    outbuffer.write(new byte[] { 1, 2, 3 }, outputStream);
    Assertions.assertEquals(13, outbuffer.available());
    Assertions.assertEquals(3, outbuffer.length());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)88 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)63 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)54 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)52 ByteArrayOutputStream (java.io.ByteArrayOutputStream)34 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)30 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)26 RandomAccessFile (java.io.RandomAccessFile)11 FileChannel (java.nio.channels.FileChannel)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)10 OutputStream (java.io.OutputStream)9 WritableByteChannel (java.nio.channels.WritableByteChannel)9 ByteBuffer (java.nio.ByteBuffer)6 CharsetEncoder (java.nio.charset.CharsetEncoder)6 ReadableByteChannel (java.nio.channels.ReadableByteChannel)5 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)4 Header (org.apache.hc.core5.http.Header)3 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1