@Override public int capacity() { return this.buffer.capacity(); }
@Override public int capacity() { return this.buffer.capacity(); }
@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); } }
@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); } }
int remaining = charbuffer.length(); while (remaining > 0) { int chunk = this.buffer.capacity() - this.buffer.length(); chunk = Math.min(chunk, remaining); if (chunk > 0) {
int remaining = charbuffer.length(); while (remaining > 0) { int chunk = this.buffer.capacity() - this.buffer.length(); chunk = Math.min(chunk, remaining); if (chunk > 0) {
@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 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 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 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 } }
@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()); }