use of org.apache.hc.core5.http.io.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testChunkedOutputStreamLargeChunk.
@Test
public void testChunkedOutputStreamLargeChunk() throws IOException {
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
out.write(new byte[] { '1', '2', '3', '4' });
out.finish();
out.close();
final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
Assertions.assertEquals("4\r\n1234\r\n0\r\n\r\n", content);
}
use of org.apache.hc.core5.http.io.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testNonAsciiReadWriteLine.
@Test
public void testNonAsciiReadWriteLine() throws Exception {
final String s1 = constructString(SWISS_GERMAN_HELLO);
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.ISO_8859_1.newEncoder());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
for (int i = 0; i < 10; i++) {
chbuffer.clear();
chbuffer.append(s1);
outbuffer.writeLine(chbuffer, outputStream);
}
chbuffer.clear();
outbuffer.writeLine(chbuffer, outputStream);
outbuffer.flush(outputStream);
final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
final long expected = ((s1.getBytes(StandardCharsets.ISO_8859_1).length + 2)) * 10 + 2;
Assertions.assertEquals(expected, bytesWritten);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.ISO_8859_1.newDecoder());
final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
for (int i = 0; i < 10; i++) {
chbuffer.clear();
final int len = inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(len, SWISS_GERMAN_HELLO.length);
Assertions.assertEquals(s1, chbuffer.toString());
}
chbuffer.clear();
Assertions.assertEquals(0, inBuffer.readLine(chbuffer, inputStream));
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 testReadWriteBytes.
@Test
public void testReadWriteBytes() 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) ('0' + i);
}
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int off = 0;
int remaining = out.length;
while (remaining > 0) {
int chunk = 10;
if (chunk > remaining) {
chunk = remaining;
}
outbuffer.write(out, off, chunk, outputStream);
off += chunk;
remaining -= chunk;
}
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);
// these read operations will have no effect
Assertions.assertEquals(0, inBuffer.read(null, 0, 10, inputStream));
Assertions.assertEquals(0, inBuffer.read(null, inputStream));
long bytesRead = inBuffer.getMetrics().getBytesTransferred();
Assertions.assertEquals(0, bytesRead);
final byte[] in = new byte[40];
off = 0;
remaining = in.length;
while (remaining > 0) {
int chunk = 10;
if (chunk > remaining) {
chunk = remaining;
}
final int readLen = inBuffer.read(in, off, chunk, inputStream);
if (readLen == -1) {
break;
}
off += readLen;
remaining -= readLen;
}
for (int i = 0; i < out.length; i++) {
Assertions.assertEquals(out[i], in[i]);
}
Assertions.assertEquals(-1, inBuffer.read(tmp, inputStream));
Assertions.assertEquals(-1, inBuffer.read(tmp, inputStream));
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 TestSessionInOutBuffers method testUnmappableInputActionIgnore.
@Test
public void testUnmappableInputActionIgnore() throws Exception {
final String s = "This text contains a circumflex \u0302 !!!";
final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder();
encoder.onMalformedInput(CodingErrorAction.IGNORE);
encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
chbuffer.append(s);
outbuffer.writeLine(chbuffer, outputStream);
outbuffer.flush(outputStream);
final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1);
Assertions.assertEquals("This text contains a circumflex !!!\r\n", result);
}
use of org.apache.hc.core5.http.io.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testBasicBufferProperties.
@Test
public void testBasicBufferProperties() throws Exception {
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1, 2, 3 });
Assertions.assertEquals(16, inBuffer.capacity());
Assertions.assertEquals(16, inBuffer.available());
Assertions.assertEquals(0, inBuffer.length());
inBuffer.read(inputStream);
Assertions.assertEquals(14, inBuffer.available());
Assertions.assertEquals(2, inBuffer.length());
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Assertions.assertEquals(16, outbuffer.capacity());
Assertions.assertEquals(16, outbuffer.available());
Assertions.assertEquals(0, outbuffer.length());
outbuffer.write(new byte[] { 1, 2, 3 }, outputStream);
Assertions.assertEquals(13, outbuffer.available());
Assertions.assertEquals(3, outbuffer.length());
}
Aggregations