Search in sources :

Example 81 with SessionInputBuffer

use of org.apache.hc.core5.http.nio.SessionInputBuffer 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 82 with SessionInputBuffer

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

the class TestContentLengthInputStream method testAvailable.

@Test
public void testAvailable() throws IOException {
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1, 2, 3 });
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 3L);
    Assertions.assertEquals(0, in.available());
    in.read();
    Assertions.assertEquals(2, in.available());
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 83 with SessionInputBuffer

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

the class TestContentLengthInputStream method testTruncatedContent.

@Test
public void testTruncatedContent() throws IOException {
    final String s = "1234567890123456";
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 32L);
    final byte[] tmp = new byte[32];
    final int byteRead = in.read(tmp);
    Assertions.assertEquals(16, byteRead);
    Assertions.assertThrows(ConnectionClosedException.class, () -> in.read(tmp));
    Assertions.assertThrows(ConnectionClosedException.class, () -> in.read());
    Assertions.assertThrows(ConnectionClosedException.class, () -> in.close());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 84 with SessionInputBuffer

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

the class TestIdentityInputStream method testAvailable.

@Test
public void testAvailable() throws Exception {
    final byte[] input = new byte[] { 'a', 'b', 'c' };
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(), 16, 16, 1024, null);
    final IdentityInputStream in = new IdentityInputStream(inBuffer, inputStream);
    in.read();
    Assertions.assertEquals(2, in.available());
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Example 85 with SessionInputBuffer

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

the class TestIdentityInputStream method testClosedCondition.

@Test
public void testClosedCondition() throws Exception {
    final byte[] input = new byte[] { 'a', 'b', 'c' };
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final IdentityInputStream in = new IdentityInputStream(inBuffer, inputStream);
    in.close();
    in.close();
    Assertions.assertEquals(0, in.available());
    final byte[] tmp = new byte[2];
    Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp, 0, tmp.length));
    Assertions.assertThrows(StreamClosedException.class, () -> in.read());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)123 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)62 ReadableByteChannel (java.nio.channels.ReadableByteChannel)61 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)61 ByteArrayInputStream (java.io.ByteArrayInputStream)58 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)47 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)46 ByteBuffer (java.nio.ByteBuffer)30 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)25 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Header (org.apache.hc.core5.http.Header)13 RandomAccessFile (java.io.RandomAccessFile)12 FileChannel (java.nio.channels.FileChannel)12 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)10 InputStream (java.io.InputStream)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 InterruptedIOException (java.io.InterruptedIOException)4 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4