Search in sources :

Example 46 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testComplexReadWriteLine.

@Test
public void testComplexReadWriteLine() throws Exception {
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outbuffer.write(new byte[] { 'a', '\n' }, outputStream);
    outbuffer.write(new byte[] { '\r', '\n' }, outputStream);
    outbuffer.write(new byte[] { '\r', '\r', '\n' }, outputStream);
    outbuffer.write(new byte[] { '\n' }, outputStream);
    // these write operations should have no effect
    outbuffer.write(null, outputStream);
    outbuffer.write(null, 0, 12, outputStream);
    outbuffer.flush(outputStream);
    long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8, bytesWritten);
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 14; i++) {
        buffer.append("a");
    }
    final String s1 = buffer.toString();
    buffer.append("\r\n");
    outbuffer.write(buffer.toString().getBytes(StandardCharsets.US_ASCII), outputStream);
    outbuffer.flush(outputStream);
    bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8 + 14 + 2, bytesWritten);
    buffer.setLength(0);
    for (int i = 0; i < 15; i++) {
        buffer.append("a");
    }
    final String s2 = buffer.toString();
    buffer.append("\r\n");
    outbuffer.write(buffer.toString().getBytes(StandardCharsets.US_ASCII), outputStream);
    outbuffer.flush(outputStream);
    bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8 + 14 + 2 + 15 + 2, bytesWritten);
    buffer.setLength(0);
    for (int i = 0; i < 16; i++) {
        buffer.append("a");
    }
    final String s3 = buffer.toString();
    buffer.append("\r\n");
    outbuffer.write(buffer.toString().getBytes(StandardCharsets.US_ASCII), outputStream);
    outbuffer.flush(outputStream);
    bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8 + 14 + 2 + 15 + 2 + 16 + 2, bytesWritten);
    outbuffer.write(new byte[] { 'a' }, outputStream);
    outbuffer.flush(outputStream);
    bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8 + 14 + 2 + 15 + 2 + 16 + 2 + 1, bytesWritten);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("a", chbuffer.toString());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("", chbuffer.toString());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("\r", chbuffer.toString());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("", chbuffer.toString());
    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();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("a", chbuffer.toString());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    final long bytesRead = inBuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(bytesWritten, 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 47 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testBasicReadWriteLineLargeBuffer.

@Test
public void testBasicReadWriteLineLargeBuffer() 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);
    final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    long expected = 0;
    for (final String teststr : teststrs) {
        expected += (teststr.length() + 2);
    }
    Assertions.assertEquals(expected, bytesWritten);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(1024);
    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));
    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 48 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testReadWriteByte.

@Test
public void testReadWriteByte() 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) (120 + i);
    }
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (final byte element : out) {
        outbuffer.write(element, outputStream);
    }
    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);
    final byte[] in = new byte[40];
    for (int i = 0; i < in.length; i++) {
        in[i] = (byte) inBuffer.read(inputStream);
    }
    for (int i = 0; i < out.length; i++) {
        Assertions.assertEquals(out[i], in[i]);
    }
    Assertions.assertEquals(-1, inBuffer.read(inputStream));
    Assertions.assertEquals(-1, inBuffer.read(inputStream));
    final long 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 49 with SessionOutputBuffer

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

the class TestContentLengthOutputStream method testClose.

@Test
public void testClose() throws Exception {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final OutputStream out = new ContentLengthOutputStream(outbuffer, outputStream, 15L);
    out.close();
    out.close();
    final byte[] tmp = new byte[10];
    Assertions.assertThrows(StreamClosedException.class, () -> out.write(tmp));
    Assertions.assertThrows(StreamClosedException.class, () -> out.write(1));
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 50 with SessionOutputBuffer

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

the class TestContentLengthOutputStream method testBasics.

@Test
public void testBasics() throws Exception {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final OutputStream out = new ContentLengthOutputStream(outbuffer, outputStream, 15L);
    final byte[] tmp = new byte[10];
    out.write(tmp, 0, 10);
    out.write(1);
    out.write(tmp, 0, 10);
    out.write(tmp, 0, 10);
    out.write(tmp);
    out.write(1);
    out.write(2);
    out.flush();
    out.close();
    final byte[] data = outputStream.toByteArray();
    Assertions.assertEquals(15, data.length);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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