@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); } }
int l; while ((l = inStream.read(tmp)) != -1) { buf.append(tmp, 0, l); total += l; if (total > cacheConfig.getMaxObjectSize()) {
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); } }
@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); Assert.assertEquals(-1, entity.getContentLength()); Assert.assertFalse(entity.isRepeatable()); Assert.assertTrue(entity.isStreaming()); Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, EntityUtils.toByteArray(entity)); verify(httpEntity).getContent(); entity.close(); verify(httpEntity).close(); }
@Override protected void data(final ByteBuffer src, final boolean endOfStream) throws IOException { if (src == null) { return; } if (src.hasArray()) { buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining()); } else { while (src.hasRemaining()) { buffer.append(src.get()); } } }
@Override protected void data(final ByteBuffer src, final boolean endOfStream) throws IOException { if (src == null) { return; } if (src.hasArray()) { buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining()); } else { while (src.hasRemaining()) { buffer.append(src.get()); } } }
static ByteArrayBuffer encode( final Charset charset, final String string) { final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string)); final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining()); bab.append(encoded.array(), encoded.position(), encoded.remaining()); return bab; }
static void decodePlainString(final ByteArrayBuffer buffer, final ByteBuffer src) throws HPackException { final int strLen = decodeInt(src, 7); if (strLen > src.remaining()) { throw new HPackException(UNEXPECTED_EOS); } if (src.hasArray()) { final byte[] b = src.array(); final int off = src.position(); buffer.append(b, off, strLen); src.position(off + strLen); } else { for (int i = 0; i < strLen; i++) { buffer.append(src.get()); } } }
@Override protected void data(final ByteBuffer src, final boolean endOfStream) throws IOException { if (src == null) { return; } if (src.hasArray()) { buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining()); } else { while (src.hasRemaining()) { buffer.append(src.get()); } } }
@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 void write(final int b, final OutputStream outputStream) throws IOException { Args.notNull(outputStream, "Output stream"); if (this.fragementSizeHint > 0) { if (this.buffer.isFull()) { flushBuffer(outputStream); } this.buffer.append(b); } else { flushBuffer(outputStream); outputStream.write(b); } }
@Override public void write(final int b, final OutputStream outputStream) throws IOException { Args.notNull(outputStream, "Output stream"); if (this.fragementSizeHint > 0) { if (this.buffer.isFull()) { flushBuffer(outputStream); } this.buffer.append(b); } else { flushBuffer(outputStream); outputStream.write(b); } }
@Test public void testAppendCharArrayAsAscii() throws Exception { final String s1 = "stuff"; final String s2 = " and more stuff"; final char[] b1 = s1.toCharArray(); final char[] b2 = s2.toCharArray(); final ByteArrayBuffer buffer = new ByteArrayBuffer(8); buffer.append(b1, 0, b1.length); buffer.append(b2, 0, b2.length); Assert.assertEquals(s1 + s2, new String(buffer.toByteArray(), "US-ASCII")); }
@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 testAppendNullCharArray() throws Exception { final ByteArrayBuffer buffer = new ByteArrayBuffer(8); buffer.append((char[])null, 0, 0); Assert.assertEquals(0, buffer.length()); }
@Test public void testAppendEmptyCharArray() throws Exception { final ByteArrayBuffer buffer = new ByteArrayBuffer(8); buffer.append(new char[] {}, 0, 0); Assert.assertEquals(0, buffer.length()); }
@Test public void testAppendNullCharArrayBuffer() throws Exception { final ByteArrayBuffer buffer = new ByteArrayBuffer(8); buffer.append((CharArrayBuffer)null, 0, 0); Assert.assertEquals(0, buffer.length()); }
@Test public void testAppendOneByte() throws Exception { final ByteArrayBuffer buffer = new ByteArrayBuffer(4); Assert.assertEquals(4, buffer.capacity()); final byte[] tmp = new byte[] { 1, 127, -1, -128, 1, -2}; for (final byte element : tmp) { buffer.append(element); } Assert.assertEquals(8, buffer.capacity()); Assert.assertEquals(6, buffer.length()); for (int i = 0; i < tmp.length; i++) { Assert.assertEquals(tmp[i], buffer.byteAt(i)); } }
@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())); }