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);
}
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();
}
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());
}
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();
}
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());
}
Aggregations