use of org.apache.hc.core5.http.io.SessionInputBuffer 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.SessionInputBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testMalformedInputActionReport.
@Test
public void testMalformedInputActionReport() throws Exception {
final byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(StandardCharsets.ISO_8859_1);
final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, decoder);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
Assertions.assertThrows(CharacterCodingException.class, () -> inBuffer.readLine(chbuffer, inputStream));
}
use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testLineLimit3.
// HTTPCORE-472
@Test
public void testLineLimit3() throws Exception {
final String s = "012345678\r\nblaaaaaaaaaaaaaaaaaah";
final byte[] tmp = s.getBytes(StandardCharsets.US_ASCII);
final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(128);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
inBuffer1.readLine(chbuffer, inputStream);
Assertions.assertEquals("012345678", chbuffer.toString());
}
use of org.apache.hc.core5.http.io.SessionInputBuffer 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.SessionInputBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testReadLineFringeCase1.
@Test
public void testReadLineFringeCase1() throws Exception {
final String s = "abc\r\n";
final byte[] tmp = s.getBytes(StandardCharsets.US_ASCII);
final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(128);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
Assertions.assertEquals('a', inBuffer1.read(inputStream));
Assertions.assertEquals('b', inBuffer1.read(inputStream));
Assertions.assertEquals('c', inBuffer1.read(inputStream));
Assertions.assertEquals('\r', inBuffer1.read(inputStream));
final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
Assertions.assertEquals(0, inBuffer1.readLine(chbuffer, inputStream));
}
Aggregations