CombinedEntity(final HttpEntity entity, final ByteArrayBuffer buf) throws IOException { super(); this.entity = entity; this.combinedStream = new SequenceInputStream( new ByteArrayInputStream(buf.array(), 0, buf.length()), entity.getContent()); }
dataConsumerRef.set(dataConsumer); writtenThrough.set(true); dataConsumer.consume(ByteBuffer.wrap(buffer.array(), 0, buffer.length()));
public static ByteArrayBuffer getRandomBuffer(final int nbytes) { final ByteArrayBuffer buf = new ByteArrayBuffer(nbytes); buf.setLength(nbytes); new Random().nextBytes(buf.array()); return buf; }
@Override public HttpCacheEntry createCacheEntry( final HttpHost host, final HttpRequest request, final HttpResponse originResponse, final ByteArrayBuffer content, final Date requestSent, final Date responseReceived) { if (log.isDebugEnabled()) { log.debug("Create cache entry: " + host + "; " + new RequestLine(request)); } final String cacheKey = cacheKeyGenerator.generateKey(host, request); try { final HttpCacheEntry entry = cacheUpdateHandler.createtCacheEntry(request, originResponse, content, requestSent, responseReceived); storeInCache(cacheKey, host, request, entry); return entry; } catch (final ResourceIOException ex) { if (log.isWarnEnabled()) { log.warn("I/O error creating cache entry with key " + cacheKey); } return new HttpCacheEntry( requestSent, responseReceived, originResponse.getCode(), originResponse.getHeaders(), content != null ? HeapResourceFactory.INSTANCE.generate(null, content.array(), 0, content.length()) : null); } }
/** * Creates a cache entry for the given request, origin response message and response content. */ public HttpCacheEntry createtCacheEntry( final HttpRequest request, final HttpResponse originResponse, final ByteArrayBuffer content, final Date requestSent, final Date responseReceived) throws ResourceIOException { return new HttpCacheEntry( requestSent, responseReceived, originResponse.getCode(), originResponse.getHeaders(), content != null ? resourceFactory.generate(request.getRequestUri(), content.array(), 0, content.length()) : null); }
originResponse.getCode(), originResponse.getHeaders(), content != null ? HeapResourceFactory.INSTANCE.generate(null, content.array(), 0, content.length()) : null)); return Operations.nonCancellable();
static void writeBytes( final ByteArrayBuffer b, final OutputStream out) throws IOException { out.write(b.array(), 0, b.length()); }
ByteBuffer getContent() { return ByteBuffer.wrap(headerBuffer.array(), 0, headerBuffer.length()); }
CombinedEntity(final HttpEntity entity, final ByteArrayBuffer buf) throws IOException { super(); this.entity = entity; this.combinedStream = new SequenceInputStream( new ByteArrayInputStream(buf.array(), 0, buf.length()), entity.getContent()); }
void copyPayload(final ByteBuffer payload) { if (payload == null) { return; } headerBuffer.ensureCapacity(payload.remaining()); payload.get(headerBuffer.array(), headerBuffer.length(), payload.remaining()); }
/** * Appends {@code len} bytes to this buffer from the given source * array starting at index {@code off}. The capacity of the buffer * is increased, if necessary, to accommodate all {@code len} bytes. * <p> * The bytes are converted to chars using simple cast. * * @param b the bytes to be appended. * @param off the index of the first byte to append. * @param len the number of bytes to append. * @throws IndexOutOfBoundsException if {@code off} is out of * range, {@code len} is negative, or * {@code off} + {@code len} is out of range. */ public void append(final ByteArrayBuffer b, final int off, final int len) { if (b == null) { return; } append(b.array(), off, len); }
/** * Appends {@code len} bytes to this buffer from the given source * array starting at index {@code off}. The capacity of the buffer * is increased, if necessary, to accommodate all {@code len} bytes. * <p> * The bytes are converted to chars using simple cast. * * @param b the bytes to be appended. * @param off the index of the first byte to append. * @param len the number of bytes to append. * @throws IndexOutOfBoundsException if {@code off} is out of * range, {@code len} is negative, or * {@code off} + {@code len} is out of range. */ public void append(final ByteArrayBuffer b, final int off, final int len) { if (b == null) { return; } append(b.array(), off, len); }
private void flushBuffer(final OutputStream outputStream) throws IOException { final int len = this.buffer.length(); if (len > 0) { outputStream.write(this.buffer.array(), 0, len); this.buffer.clear(); this.metrics.incrementBytesTransferred(len); } }
private void flushBuffer(final OutputStream outputStream) throws IOException { final int len = this.buffer.length(); if (len > 0) { outputStream.write(this.buffer.array(), 0, len); this.buffer.clear(); this.metrics.incrementBytesTransferred(len); } }
void encodeString(final ByteArrayBuffer dst, final ByteBuffer src, final boolean huffman) { final int strLen = src.remaining(); if (huffman) { this.huffmanBuf.clear(); this.huffmanBuf.ensureCapacity(strLen); Huffman.ENCODER.encode(this.huffmanBuf, src); dst.ensureCapacity(this.huffmanBuf.length() + 8); encodeInt(dst, 7, this.huffmanBuf.length(), 0x80); dst.append(this.huffmanBuf.array(), 0, this.huffmanBuf.length()); } else { dst.ensureCapacity(strLen + 8); encodeInt(dst, 7, strLen, 0x0); if (src.hasArray()) { final byte[] b = src.array(); final int off = src.position(); dst.append(b, off, strLen); src.position(off + strLen); } else { while (src.hasRemaining()) { dst.append(src.get()); } } } }
@Override public HttpCacheEntry createCacheEntry( final HttpHost host, final HttpRequest request, final HttpResponse originResponse, final ByteArrayBuffer content, final Date requestSent, final Date responseReceived) { if (log.isDebugEnabled()) { log.debug("Create cache entry: " + host + "; " + new RequestLine(request)); } final String cacheKey = cacheKeyGenerator.generateKey(host, request); try { final HttpCacheEntry entry = cacheUpdateHandler.createtCacheEntry(request, originResponse, content, requestSent, responseReceived); storeInCache(cacheKey, host, request, entry); return entry; } catch (final ResourceIOException ex) { if (log.isWarnEnabled()) { log.warn("I/O error creating cache entry with key " + cacheKey); } return new HttpCacheEntry( requestSent, responseReceived, originResponse.getCode(), originResponse.getHeaders(), content != null ? HeapResourceFactory.INSTANCE.generate(null, content.array(), 0, content.length()) : null); } }
/** * Creates a cache entry for the given request, origin response message and response content. */ public HttpCacheEntry createtCacheEntry( final HttpRequest request, final HttpResponse originResponse, final ByteArrayBuffer content, final Date requestSent, final Date responseReceived) throws ResourceIOException { return new HttpCacheEntry( requestSent, responseReceived, originResponse.getCode(), originResponse.getHeaders(), content != null ? resourceFactory.generate(request.getRequestUri(), content.array(), 0, content.length()) : null); }
private void commitHeaders( final int streamId, final List<? extends Header> headers, final boolean endStream) throws IOException { if (streamListener != null) { streamListener.onHeaderOutput(this, streamId, headers); } final ByteArrayBuffer buf = new ByteArrayBuffer(512); hPackEncoder.encodeHeaders(buf, headers); 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.createHeaders(streamId, payload, endHeaders, endStream); continuation = true; } else { frame = frameFactory.createContinuation(streamId, payload, endHeaders); } commitFrameInternal(frame); } }
final ByteBuffer payload = ByteBuffer.wrap(buf.array(), off, chunk);
@Test public void testConstructor() throws Exception { final ByteArrayBuffer buffer = new ByteArrayBuffer(16); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(0, buffer.length()); Assert.assertNotNull(buffer.array()); Assert.assertEquals(16, buffer.array().length); try { new ByteArrayBuffer(-1); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }