Search in sources :

Example 56 with SessionInputBuffer

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

the class TestContentLengthInputStream method testBasics.

@Test
public void testBasics() 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, 10L);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final byte[] buffer = new byte[50];
    int len = in.read(buffer, 0, 2);
    outputStream.write(buffer, 0, len);
    len = in.read(buffer);
    outputStream.write(buffer, 0, len);
    final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1);
    Assertions.assertEquals(result, "1234567890");
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 57 with SessionInputBuffer

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

the class TestContentLengthInputStream method testClose.

@Test
public void testClose() 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, 16L);
    in.close();
    in.close();
    Assertions.assertThrows(StreamClosedException.class, in::read);
    final byte[] tmp = new byte[10];
    Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp));
    Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp, 0, tmp.length));
    Assertions.assertEquals('-', inBuffer.read(inputStream));
}
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 58 with SessionInputBuffer

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

the class TestContentLengthInputStream method testSkip.

@Test
public void testSkip() throws IOException {
    final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(new byte[20]);
    final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16);
    final InputStream in1 = new ContentLengthInputStream(inBuffer1, inputStream1, 10L);
    Assertions.assertEquals(10, in1.skip(10));
    Assertions.assertEquals(-1, in1.read());
    in1.close();
    final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(new byte[20]);
    final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16);
    final InputStream in2 = new ContentLengthInputStream(inBuffer2, inputStream2, 10L);
    in2.read();
    Assertions.assertEquals(9, in2.skip(10));
    Assertions.assertEquals(-1, in2.read());
    in2.close();
    final ByteArrayInputStream inputStream3 = new ByteArrayInputStream(new byte[20]);
    final SessionInputBuffer inBuffer3 = new SessionInputBufferImpl(16);
    final InputStream in3 = new ContentLengthInputStream(inBuffer3, inputStream3, 2L);
    in3.read();
    in3.read();
    Assertions.assertTrue(in3.skip(10) <= 0);
    Assertions.assertEquals(0, in3.skip(-1));
    Assertions.assertEquals(-1, in3.read());
    in3.close();
    final ByteArrayInputStream inputStream4 = new ByteArrayInputStream(new byte[20]);
    final SessionInputBuffer inBuffer4 = new SessionInputBufferImpl(16);
    final InputStream in4 = new ContentLengthInputStream(inBuffer4, inputStream4, 10L);
    Assertions.assertEquals(5, in4.skip(5));
    Assertions.assertEquals(5, in4.read(new byte[20]));
    in4.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 59 with SessionInputBuffer

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

the class TestChunkCoding method testCorruptChunkedInputStreamNegativeSize.

// Negative chunk size
@Test
public void testCorruptChunkedInputStreamNegativeSize() throws IOException {
    final String s = "-5\r\n01234\r\n5\r\n56789\r\n0\r\n";
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    Assertions.assertThrows(MalformedChunkCodingException.class, in::read);
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 60 with SessionInputBuffer

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

the class TestChunkCoding method testChunkedInputStreamClose.

@Test
public void testChunkedInputStreamClose() throws IOException {
    final String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    in.close();
    in.close();
    Assertions.assertThrows(StreamClosedException.class, () -> in.read());
    final byte[] tmp = new byte[10];
    Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp));
    Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp, 0, tmp.length));
}
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