/** * Gets a copy of the current buffer as byte array, but only copies data * starting from an offset and length. Method will allocate * only enough memory to hold a copy of the slice of the current buffer data. * @param offset The offset to start from * @param length The length from the offset * @return A byte array. Could be empty. * @throws IllegalArgumentException If capacity isn't large enough enough * to hold the new byte[] */ public byte[] toArray(int offset, int length) { return toArray(offset, length, length); }
/** * Gets a copy of the current buffer as byte array, but only copies data * starting from an offset and length. Method will allocate * only enough memory to hold a copy of the slice of the current buffer data. * @param offset The offset to start from * @param length The length from the offset * @return A byte array. Could be empty. * @throws IllegalArgumentException If capacity isn't large enough enough * to hold the new byte[] */ public byte[] toArray(int offset, int length) { return toArray(offset, length, length); }
/** * Gets a copy of the current buffer as byte array. Method will allocate * only enough memory to hold a copy of the current buffer data. * @return A byte array. Could be empty if this buffer's size() is zero. */ public byte[] toArray() { return toArray(0, size()); }
/** * Gets a copy of the current buffer as byte array. Method will allocate * only enough memory to hold a copy of the current buffer data. * @return A byte array. Could be empty if this buffer's size() is zero. */ public byte[] toArray() { return toArray(0, size()); }
/** * Gets a copy of the current buffer as byte array, but the new byte[] * has the specified capacity. Useful if you need to store additional bytes * in the returned byte[] and dont' want to do an additional System.arraycopy() * afterwards. Method will allocate memory to hold a copy of the current array * and return it. * @param offset The offset to start from * @param length The length from the offset * @param capacity The size of the new byte[]. Must be >= this buffer's size() * @return A byte array. Could be empty. * @throws IllegalArgumentException If capacity isn't large enough enough * to hold the new byte[] */ public byte[] toArray(int offset, int length, int capacity) { // validate the offset, length are ok ByteBuffer.checkOffsetLength(size(), offset, length); // will we have a large enough byte[] allocated? if (capacity < length) { throw new IllegalArgumentException("Capacity must be large enough to hold a byte[] of at least a size=" + length); } byte[] arrayCopy = new byte[capacity]; this.toArray(offset, length, arrayCopy, 0); return arrayCopy; }
/** * Gets a copy of the current buffer as byte array, but the new byte[] * has the specified capacity. Useful if you need to store additional bytes * in the returned byte[] and dont' want to do an additional System.arraycopy() * afterwards. Method will allocate memory to hold a copy of the current array * and return it. * @param offset The offset to start from * @param length The length from the offset * @param capacity The size of the new byte[]. Must be >= this buffer's size() * @return A byte array. Could be empty. * @throws IllegalArgumentException If capacity isn't large enough enough * to hold the new byte[] */ public byte[] toArray(int offset, int length, int capacity) { // validate the offset, length are ok ByteBuffer.checkOffsetLength(size(), offset, length); // will we have a large enough byte[] allocated? if (capacity < length) { throw new IllegalArgumentException("Capacity must be large enough to hold a byte[] of at least a size=" + length); } byte[] arrayCopy = new byte[capacity]; this.toArray(offset, length, arrayCopy, 0); return arrayCopy; }
this.toArray(offset, length, copyBuffer.buffer, 0);
this.toArray(offset, length, copyBuffer.buffer, 0);