/** * Expensive. Makes a new buffer to hold a copy of what is in contained ByteBuffers. This * call drains this instance; it cannot be used subsequent to the call. * @return A new byte buffer with the content of all contained ByteBuffers. */ byte [] getBytes() { if (!hasRemaining()) throw new IllegalAccessError(); byte [] bytes = new byte [this.remaining]; int offset = 0; for (ByteBuffer bb: this.buffers) { int length = bb.remaining(); bb.get(bytes, offset, length); offset += length; } return bytes; }
assert !hasRemaining();
if (!buf.hasRemaining()) { resp.done(); return true;
private void writeAndVerify(BufferChain chain, String string, int chunkSize) throws IOException { FileOutputStream fos = new FileOutputStream(tmpFile); FileChannel ch = fos.getChannel(); try { long remaining = string.length(); while (chain.hasRemaining()) { long n = chain.write(ch, chunkSize); assertTrue(n == chunkSize || n == remaining); remaining -= n; } assertEquals(0, remaining); } finally { fos.close(); } assertFalse(chain.hasRemaining()); assertEquals(string, Files.toString(tmpFile, Charsets.UTF_8)); } }
/** * Expensive. Makes a new buffer to hold a copy of what is in contained ByteBuffers. This * call drains this instance; it cannot be used subsequent to the call. * @return A new byte buffer with the content of all contained ByteBuffers. */ byte [] getBytes() { if (!hasRemaining()) throw new IllegalAccessError(); byte [] bytes = new byte [this.remaining]; int offset = 0; for (ByteBuffer bb: this.buffers) { System.arraycopy(bb.array(), bb.arrayOffset(), bytes, offset, bb.limit()); offset += bb.capacity(); } return bytes; }
assert !hasRemaining();
private void writeAndVerify(BufferChain chain, String string, int chunkSize) throws IOException { FileOutputStream fos = new FileOutputStream(tmpFile); FileChannel ch = fos.getChannel(); try { long remaining = string.length(); while (chain.hasRemaining()) { long n = chain.write(ch, chunkSize); assertTrue(n == chunkSize || n == remaining); remaining -= n; } assertEquals(0, remaining); } finally { fos.close(); } assertFalse(chain.hasRemaining()); assertEquals(string, Files.toString(tmpFile, Charsets.UTF_8)); } }
if (!call.response.hasRemaining()) { call.done(); return true;