@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 protected byte[] generateContent() throws IOException { return buffer.toByteArray(); }
@Override protected byte[] generateContent() throws IOException { return buffer.toByteArray(); }
@Override protected byte[] generateContent() throws IOException { return buffer.toByteArray(); }
@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); } }
/** * Read 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. * @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) throws IOException { Args.notNull(entity, "Entity"); try (final InputStream inStream = entity.getContent()) { if (inStream == null) { return null; } int i = (int) Args.checkContentLength(entity); if (i < 0) { i = 4096; } final ByteArrayBuffer buffer = new ByteArrayBuffer(i); final byte[] tmp = new byte[4096]; int l; while ((l = inStream.read(tmp)) != -1) { buffer.append(tmp, 0, l); } return buffer.toByteArray(); } }
/** * Read 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. * @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) throws IOException { Args.notNull(entity, "Entity"); try (final InputStream inStream = entity.getContent()) { if (inStream == null) { return null; } int i = (int) Args.checkContentLength(entity); if (i < 0) { i = 4096; } final ByteArrayBuffer buffer = new ByteArrayBuffer(i); final byte[] tmp = new byte[4096]; int l; while ((l = inStream.read(tmp)) != -1) { buffer.append(tmp, 0, l); } return buffer.toByteArray(); } }
@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")); }
public byte[] decode(final byte[] bytes) throws DecoderException { if (bytes == null) { return null; } final ByteArrayBuffer buffer = new ByteArrayBuffer(bytes.length); for (int i = 0; i < bytes.length; i++) { final int b = bytes[i]; if (b == ESCAPE_CHAR) { try { final int u = Utils.digit16(bytes[++i]); final int l = Utils.digit16(bytes[++i]); buffer.append((char) ((u << 4) + l)); } catch (final ArrayIndexOutOfBoundsException e) { throw new DecoderException("Invalid URL encoding: ", e); } } else { buffer.append(b); } } return buffer.toByteArray(); } }
@Test public void testSerialization() throws Exception { final ByteArrayBuffer orig = new ByteArrayBuffer(32); orig.append(1); orig.append(2); orig.append(3); final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream(); try (final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer)) { outStream.writeObject(orig); } final byte[] raw = outbuffer.toByteArray(); final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw); final ObjectInputStream inStream = new ObjectInputStream(inBuffer); final ByteArrayBuffer clone = (ByteArrayBuffer) inStream.readObject(); Assert.assertEquals(orig.capacity(), clone.capacity()); Assert.assertEquals(orig.length(), clone.length()); final byte[] data = clone.toByteArray(); Assert.assertNotNull(data); Assert.assertEquals(3, data.length); Assert.assertEquals(1, data[0]); Assert.assertEquals(2, data[1]); Assert.assertEquals(3, data[2]); }
@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()); }