Search in sources :

Example 16 with ByteArrayBuffer

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);
    }
}
Also used : H2ConnectionException(org.apache.hc.core5.http2.H2ConnectionException) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) ByteBuffer(java.nio.ByteBuffer) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer)

Example 17 with ByteArrayBuffer

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 &gt; 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();
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer)

Example 18 with ByteArrayBuffer

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();
}
Also used : HttpEntity(org.apache.hc.core5.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 19 with ByteArrayBuffer

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);
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpCacheEntry(org.apache.hc.client5.http.cache.HttpCacheEntry) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 20 with ByteArrayBuffer

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);
}
Also used : ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer)

Aggregations

ByteArrayBuffer (org.apache.hc.core5.util.ByteArrayBuffer)37 Test (org.junit.jupiter.api.Test)26 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)11 Header (org.apache.hc.core5.http.Header)10 ByteBuffer (java.nio.ByteBuffer)9 InputStream (java.io.InputStream)4 HttpCacheEntry (org.apache.hc.client5.http.cache.HttpCacheEntry)4 Charset (java.nio.charset.Charset)2 ResourceIOException (org.apache.hc.client5.http.cache.ResourceIOException)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 HttpEntity (org.apache.hc.core5.http.HttpEntity)2 HttpHost (org.apache.hc.core5.http.HttpHost)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)2 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1