use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.
the class AbstractH2StreamMultiplexer method commitPushPromise.
private void commitPushPromise(final int streamId, final int promisedStreamId, final List<Header> headers) throws IOException {
if (headers == null || headers.isEmpty()) {
throw new H2ConnectionException(H2Error.INTERNAL_ERROR, "Message headers are missing");
}
if (streamListener != null) {
streamListener.onHeaderOutput(this, streamId, headers);
}
final ByteArrayBuffer buf = new ByteArrayBuffer(512);
buf.append((byte) (promisedStreamId >> 24));
buf.append((byte) (promisedStreamId >> 16));
buf.append((byte) (promisedStreamId >> 8));
buf.append((byte) (promisedStreamId));
hPackEncoder.encodeHeaders(buf, headers, localConfig.isCompressionEnabled());
int off = 0;
int remaining = buf.length();
boolean continuation = false;
while (remaining > 0) {
final int chunk = Math.min(remoteConfig.getMaxFrameSize(), remaining);
final ByteBuffer payload = ByteBuffer.wrap(buf.array(), off, chunk);
remaining -= chunk;
off += chunk;
final boolean endHeaders = remaining == 0;
final RawFrame frame;
if (!continuation) {
frame = frameFactory.createPushPromise(streamId, payload, endHeaders);
continuation = true;
} else {
frame = frameFactory.createContinuation(streamId, payload, endHeaders);
}
commitFrameInternal(frame);
}
}
use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.
the class EntityUtils method toByteArray.
/**
* Reads the contents of an entity and return it as a byte array.
*
* @param entity the entity to read from=
* @return byte array containing the entity content. May be null if
* {@link HttpEntity#getContent()} is null.
* @param maxResultLength
* The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
* @throws IOException if an error occurs reading the input stream
* @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
*/
public static byte[] toByteArray(final HttpEntity entity, final int maxResultLength) throws IOException {
Args.notNull(entity, "HttpEntity");
final int contentLength = toContentLength((int) Args.checkContentLength(entity));
try (final InputStream inStream = entity.getContent()) {
if (inStream == null) {
return null;
}
final ByteArrayBuffer buffer = new ByteArrayBuffer(Math.min(maxResultLength, contentLength));
final byte[] tmp = new byte[DEFAULT_BYTE_BUFFER_SIZE];
int l;
while ((l = inStream.read(tmp, 0, Math.min(DEFAULT_BYTE_BUFFER_SIZE, buffer.capacity() - buffer.length()))) > 0) {
buffer.append(tmp, 0, l);
}
return buffer.toByteArray();
}
}
use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-client by apache.
the class TestCombinedEntity method testCombinedEntityBasics.
@Test
public void testCombinedEntityBasics() throws Exception {
final HttpEntity httpEntity = mock(HttpEntity.class);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(new byte[] { 6, 7, 8, 9, 10 }));
final ByteArrayBuffer buf = new ByteArrayBuffer(1024);
final byte[] tmp = new byte[] { 1, 2, 3, 4, 5 };
buf.append(tmp, 0, tmp.length);
final CombinedEntity entity = new CombinedEntity(httpEntity, buf);
Assertions.assertEquals(-1, entity.getContentLength());
Assertions.assertFalse(entity.isRepeatable());
Assertions.assertTrue(entity.isStreaming());
Assertions.assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, EntityUtils.toByteArray(entity));
verify(httpEntity).getContent();
entity.close();
verify(httpEntity).close();
}
use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-client by apache.
the class TestBasicHttpCache method testGetCacheEntryReturnsNullIfNoVariantInCache.
@Test
public void testGetCacheEntryReturnsNullIfNoVariantInCache() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final HttpRequest origRequest = new HttpGet("http://foo.example.com/bar");
origRequest.setHeader("Accept-Encoding", "gzip");
final ByteArrayBuffer buf = HttpTestUtils.getRandomBuffer(128);
final HttpResponse origResponse = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
origResponse.setHeader("Date", DateUtils.formatStandardDate(Instant.now()));
origResponse.setHeader("Cache-Control", "max-age=3600, public");
origResponse.setHeader("ETag", "\"etag\"");
origResponse.setHeader("Vary", "Accept-Encoding");
origResponse.setHeader("Content-Encoding", "gzip");
impl.createCacheEntry(host, origRequest, origResponse, buf, Instant.now(), Instant.now());
final HttpRequest request = new HttpGet("http://foo.example.com/bar");
final HttpCacheEntry result = impl.getCacheEntry(host, request);
assertNull(result);
}
use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-client by apache.
the class AbstractMultipartFormat method writeBytes.
static void writeBytes(final String s, final OutputStream out) throws IOException {
final ByteArrayBuffer b = encode(StandardCharsets.ISO_8859_1, s);
writeBytes(b, out);
}
Aggregations