@Override public byte[] get() throws ResourceIOException { final File file = this.fileRef.get(); if (file == null) { throw new ResourceIOException("Resouce already dispoased"); } try (final InputStream in = new FileInputStream(file)) { final ByteArrayBuffer buf = new ByteArrayBuffer(1024); final byte[] tmp = new byte[2048]; int len; while ((len = in.read(tmp)) != -1) { buf.append(tmp, 0, len); } return buf.toByteArray(); } catch (final IOException ex) { throw new ResourceIOException(ex.getMessage(), ex); } }
@Override public int capacity() { return this.buffer.capacity(); }
public static ByteArrayBuffer getRandomBuffer(final int nbytes) { final ByteArrayBuffer buf = new ByteArrayBuffer(nbytes); buf.setLength(nbytes); new Random().nextBytes(buf.array()); return buf; }
@Test public void testExpandAppend() throws Exception { final ByteArrayBuffer buffer = new ByteArrayBuffer(4); Assert.assertEquals(4, buffer.capacity()); final byte[] tmp = new byte[] { 1, 2, 3, 4}; buffer.append(tmp, 0, 2); buffer.append(tmp, 0, 4); buffer.append(tmp, 0, 0); Assert.assertEquals(8, buffer.capacity()); Assert.assertEquals(6, buffer.length()); buffer.append(tmp, 0, 4); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(10, buffer.length()); }
@Test public void testSimpleAppend() throws Exception { final ByteArrayBuffer buffer = new ByteArrayBuffer(16); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(0, buffer.length()); final byte[] b1 = buffer.toByteArray(); Assert.assertNotNull(b1); Assert.assertEquals(0, b1.length); Assert.assertTrue(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); final byte[] tmp = new byte[] { 1, 2, 3, 4}; buffer.append(tmp, 0, tmp.length); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(4, buffer.length()); Assert.assertFalse(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); final byte[] b2 = buffer.toByteArray(); Assert.assertNotNull(b2); Assert.assertEquals(4, b2.length); for (int i = 0; i < tmp.length; i++) { Assert.assertEquals(tmp[i], b2[i]); Assert.assertEquals(tmp[i], buffer.byteAt(i)); } buffer.clear(); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(0, buffer.length()); Assert.assertTrue(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); }
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()); } } } }
@Test public void testAppendNullCharArray() throws Exception { final ByteArrayBuffer buffer = new ByteArrayBuffer(8); buffer.append((char[])null, 0, 0); Assert.assertEquals(0, buffer.length()); }
final HttpEntity entity = backendResponse.getEntity(); if (entity != null) { buf = new ByteArrayBuffer(1024); final InputStream inStream = entity.getContent(); final byte[] tmp = new byte[2048]; int l; while ((l = inStream.read(tmp)) != -1) { buf.append(tmp, 0, l); total += l; if (total > cacheConfig.getMaxObjectSize()) {
if (buffer != null) { if (src.hasArray()) { buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining()); } else { while (src.hasRemaining()) { buffer.append(src.get()); if (buffer.length() > cacheConfig.getMaxObjectSize()) { log.debug("Backend response content length exceeds maximum"); dataConsumerRef.set(dataConsumer); writtenThrough.set(true); dataConsumer.consume(ByteBuffer.wrap(buffer.array(), 0, buffer.length()));
throws IOException { int len = this.lineBuffer.length(); if (len > 0) { if (this.lineBuffer.byteAt(len - 1) == Chars.LF) { len--; if (len > 0 && this.lineBuffer.byteAt(len - 1) == Chars.CR) { len--; charBuffer.append(this.lineBuffer, 0, len); } else { final ByteBuffer bbuf = ByteBuffer.wrap(this.lineBuffer.array(), 0, len); len = appendDecoded(charBuffer, bbuf); this.lineBuffer.clear(); return len;
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)); int remaining = buf.length(); boolean continuation = false; final ByteBuffer payload = ByteBuffer.wrap(buf.array(), off, chunk);
CachingAsyncDataConsumer( final AsyncExecCallback fallback, final HttpResponse backendResponse, final EntityDetails entityDetails) { this.fallback = fallback; this.backendResponse = backendResponse; this.entityDetails = entityDetails; this.writtenThrough = new AtomicBoolean(false); this.bufferRef = new AtomicReference<>(entityDetails != null ? new ByteArrayBuffer(1024) : null); this.dataConsumerRef = new AtomicReference<>(); }
@Test public void testIndexOf() throws Exception { final byte COLON = (byte) ':'; final byte COMMA = (byte) ','; final byte[] bytes = "name1: value1; name2: value2".getBytes(StandardCharsets.US_ASCII); final int index1 = 5; final int index2 = 20; final ByteArrayBuffer buffer = new ByteArrayBuffer(16); buffer.append(bytes, 0, bytes.length); Assert.assertEquals(index1, buffer.indexOf(COLON)); Assert.assertEquals(-1, buffer.indexOf(COMMA)); Assert.assertEquals(index1, buffer.indexOf(COLON, -1, 11)); Assert.assertEquals(index1, buffer.indexOf(COLON, 0, 1000)); Assert.assertEquals(-1, buffer.indexOf(COLON, 2, 1)); Assert.assertEquals(index2, buffer.indexOf(COLON, index1 + 1, buffer.length())); }
@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 } }
static void encodeInt(final ByteArrayBuffer dst, final int n, final int i, final int mask) { final int nbits = 0xFF >>> (8 - n); int value = i; if (value < nbits) { dst.append(i | mask); } else { dst.append(nbits | mask); value -= nbits; while (value >= 0x80) { dst.append((value & 0x7F) | 0x80); value >>>= 7; } dst.append(value); } }
@Override public void write(final byte[] b, final int off, final int len, final OutputStream outputStream) throws IOException { if (b == null) { return; } Args.notNull(outputStream, "Output stream"); // Do not want to buffer large-ish chunks // if the byte array is larger then MIN_CHUNK_LIMIT // write it directly to the output stream if (len > this.fragementSizeHint || len > this.buffer.capacity()) { // flush the buffer flushBuffer(outputStream); // write directly to the out stream outputStream.write(b, off, len); this.metrics.incrementBytesTransferred(len); } else { // Do not let the buffer grow unnecessarily final int freecapacity = this.buffer.capacity() - this.buffer.length(); if (len > freecapacity) { // flush the buffer flushBuffer(outputStream); } // buffer this.buffer.append(b, off, len); } }
final int currentLen = this.lineBuffer.length() + (pos >= 0 ? pos : this.bufferLen) - this.bufferPos; if (currentLen >= this.maxLineLen) { if (this.lineBuffer.isEmpty()) { this.lineBuffer.append(this.buffer, this.bufferPos, len); this.bufferPos = pos + 1; } else { this.lineBuffer.append(this.buffer, this.bufferPos, len); this.bufferPos = this.bufferLen; if (readLen == -1 && this.lineBuffer.isEmpty()) {