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