Search in sources :

Example 51 with SessionInputBuffer

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

the class TestIdentityDecoder method testDecodingFileWithLimit.

@Test
public void testDecodingFileWithLimit() throws Exception {
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff; more stuff; ", "a lot more stuff!" }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);
    final int i = inbuf.fill(channel);
    Assertions.assertEquals(19, i);
    createTempFile();
    try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
        final FileChannel fchannel = testfile.getChannel();
        long pos = 0;
        // transferred from buffer
        long bytesRead = decoder.transfer(fchannel, pos, 1);
        Assertions.assertEquals(1, bytesRead);
        Assertions.assertFalse(decoder.isCompleted());
        Assertions.assertEquals(0, metrics.getBytesTransferred());
        pos += bytesRead;
        bytesRead = decoder.transfer(fchannel, pos, 2);
        Assertions.assertEquals(2, bytesRead);
        Assertions.assertFalse(decoder.isCompleted());
        Assertions.assertEquals(0, metrics.getBytesTransferred());
        pos += bytesRead;
        bytesRead = decoder.transfer(fchannel, pos, 17);
        Assertions.assertEquals(16, bytesRead);
        Assertions.assertFalse(decoder.isCompleted());
        Assertions.assertEquals(0, metrics.getBytesTransferred());
        pos += bytesRead;
        // transferred from channel
        bytesRead = decoder.transfer(fchannel, pos, 1);
        Assertions.assertEquals(1, bytesRead);
        Assertions.assertFalse(decoder.isCompleted());
        Assertions.assertEquals(1, metrics.getBytesTransferred());
        pos += bytesRead;
        bytesRead = decoder.transfer(fchannel, pos, 2);
        Assertions.assertEquals(2, bytesRead);
        Assertions.assertFalse(decoder.isCompleted());
        Assertions.assertEquals(3, metrics.getBytesTransferred());
        pos += bytesRead;
        bytesRead = decoder.transfer(fchannel, pos, 15);
        Assertions.assertEquals(14, bytesRead);
        Assertions.assertFalse(decoder.isCompleted());
        Assertions.assertEquals(17, metrics.getBytesTransferred());
        pos += bytesRead;
        bytesRead = decoder.transfer(fchannel, pos, 1);
        Assertions.assertEquals(-1, bytesRead);
        Assertions.assertTrue(decoder.isCompleted());
        Assertions.assertEquals(17, metrics.getBytesTransferred());
    }
    Assertions.assertEquals("stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 52 with SessionInputBuffer

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

the class TestIdentityDecoder method testBasicDecoding.

@Test
public void testBasicDecoding() throws Exception {
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff;", "more stuff" }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    int bytesRead = decoder.read(dst);
    Assertions.assertEquals(6, bytesRead);
    Assertions.assertEquals("stuff;", CodecTestUtils.convert(dst));
    Assertions.assertFalse(decoder.isCompleted());
    Assertions.assertEquals(6, metrics.getBytesTransferred());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(10, bytesRead);
    Assertions.assertEquals("more stuff", CodecTestUtils.convert(dst));
    Assertions.assertFalse(decoder.isCompleted());
    Assertions.assertEquals(16, metrics.getBytesTransferred());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(-1, bytesRead);
    Assertions.assertTrue(decoder.isCompleted());
    Assertions.assertEquals(16, metrics.getBytesTransferred());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(-1, bytesRead);
    Assertions.assertTrue(decoder.isCompleted());
    Assertions.assertEquals(16, metrics.getBytesTransferred());
    Assertions.assertEquals("[identity; completed: true]", decoder.toString());
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 53 with SessionInputBuffer

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

the class TestIdentityDecoder method testWriteBeyondFileSize.

@Test
public void testWriteBeyondFileSize() throws Exception {
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "a" }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);
    createTempFile();
    try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
        Assertions.assertEquals(0, testfile.length());
        final FileChannel fchannel = testfile.getChannel();
        Assertions.assertThrows(IOException.class, () -> decoder.transfer(fchannel, 5, 10));
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 54 with SessionInputBuffer

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

the class TestIdentityDecoder method testBasicDecodingFile.

@Test
public void testBasicDecodingFile() throws Exception {
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff; ", "more stuff; ", "a lot more stuff!" }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);
    createTempFile();
    try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
        final FileChannel fchannel = testfile.getChannel();
        long pos = 0;
        while (!decoder.isCompleted()) {
            final long bytesRead = decoder.transfer(fchannel, pos, 10);
            if (bytesRead > 0) {
                pos += bytesRead;
            }
        }
        Assertions.assertEquals(testfile.length(), metrics.getBytesTransferred());
    }
    Assertions.assertEquals("stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 55 with SessionInputBuffer

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

the class TestIdentityDecoder method testDecodingFileWithOffsetAndBufferedSessionData.

@Test
public void testDecodingFileWithOffsetAndBufferedSessionData() throws Exception {
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff; ", "more stuff; ", "a lot more stuff!" }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);
    final int i = inbuf.fill(channel);
    Assertions.assertEquals(7, i);
    final byte[] beginning = "beginning; ".getBytes(StandardCharsets.US_ASCII);
    createTempFile();
    RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        testfile.write(beginning);
    } finally {
        testfile.close();
    }
    testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        final FileChannel fchannel = testfile.getChannel();
        long pos = beginning.length;
        while (!decoder.isCompleted()) {
            if (testfile.length() < pos) {
                testfile.setLength(pos);
            }
            final long bytesRead = decoder.transfer(fchannel, pos, 10);
            if (bytesRead > 0) {
                pos += bytesRead;
            }
        }
        // count everything except the initial 7 bytes that went to the session buffer
        Assertions.assertEquals(testfile.length() - 7 - beginning.length, metrics.getBytesTransferred());
    } finally {
        testfile.close();
    }
    Assertions.assertEquals("beginning; stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) 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