protected static void checkBytes(byte[] bytes, int offset, int length, int expectedLength) { checkBytesNotNull(bytes); ByteBuffer.checkOffsetLength(bytes.length, offset, length); if (length != expectedLength) { throw new IllegalArgumentException("Unexpected length of byte array [expected=" + expectedLength + ", actual=" + length + "]"); } }
protected static void checkBytes(byte[] bytes, int offset, int length, int expectedLength) { checkBytesNotNull(bytes); ByteBuffer.checkOffsetLength(bytes.length, offset, length); if (length != expectedLength) { throw new IllegalArgumentException("Unexpected length of byte array [expected=" + expectedLength + ", actual=" + length + "]"); } }
ByteBuffer.checkOffsetLength(size(), offset, length); ByteBuffer.checkOffsetLength(targetBuffer.length, targetOffset, length);
/** * Constructs a new instance of <code>ByteBuffer</code> with the specified * capacity. Buffer is initialized with the byte[] bytes starting from * <code>offset</code> and added up to <code>length</code> bytes. Capacity * must be large enough to store the length. * @param bytes The byte array to initialize buffer with * @param offset The offset in the byte array to start from * @param length The length starting from offset within the byte array * @param capacity The buffer capacity. Must be >= 1. * @throws IllegalArgumentException Thrown if offset or length is negative, * capacity is too small, or if the offset+length would cause a read * past the length of the byte array. */ public ByteBuffer(byte[] bytes, int offset, int length, int capacity) throws IllegalArgumentException { this(capacity); // will check for valid capacity checkOffsetLength(bytes.length, offset, length); // user should have requested >= the length of the bytes they passed in if (capacity < length) { throw new IllegalArgumentException("Buffer capacity (" + capacity + ") must be >= the byte[] length (" + bytes.length + ")"); } // at this point, guaranteed to have enough capacity to add this // entire byte array to this buffer -- we can at least prevent // any BufferSizeException try { this.add(bytes, offset, length); } catch (BufferSizeException e) { logger.error("Impossible case of BufferSizeException in ByteBuffer constructor", e); } }
/** * Constructs a new instance of <code>ByteBuffer</code> with the specified * capacity. Buffer is initialized with the byte[] bytes starting from * <code>offset</code> and added up to <code>length</code> bytes. Capacity * must be large enough to store the length. * @param bytes The byte array to initialize buffer with * @param offset The offset in the byte array to start from * @param length The length starting from offset within the byte array * @param capacity The buffer capacity. Must be >= 1. * @throws IllegalArgumentException Thrown if offset or length is negative, * capacity is too small, or if the offset+length would cause a read * past the length of the byte array. */ public ByteBuffer(byte[] bytes, int offset, int length, int capacity) throws IllegalArgumentException { this(capacity); // will check for valid capacity checkOffsetLength(bytes.length, offset, length); // user should have requested >= the length of the bytes they passed in if (capacity < length) { throw new IllegalArgumentException("Buffer capacity (" + capacity + ") must be >= the byte[] length (" + bytes.length + ")"); } // at this point, guaranteed to have enough capacity to add this // entire byte array to this buffer -- we can at least prevent // any BufferSizeException try { this.add(bytes, offset, length); } catch (BufferSizeException e) { logger.error("Impossible case of BufferSizeException in ByteBuffer constructor", e); } }
ByteBuffer.checkOffsetLength(size(), offset, length); ByteBuffer.checkOffsetLength(targetBuffer.length, targetOffset, length);
/** * 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; }
/** * Return a hexidecimal String representation of the current buffer with each byte * displayed in a 2 character hexidecimal format. Useful for debugging. * Convert a ByteBuffer to a String with a hexidecimal format. * @param offset * @param length * @return The string in hex representation */ public String toHexString(int offset, int length) { // is offset, length valid for the current size() of our internal byte[] checkOffsetLength(size(), offset, length); // if length is 0, return an empty string if (length == 0 || size() == 0) { return ""; } StringBuilder s = new StringBuilder(length * 2); int end = offset + length; for (int i = offset; i < end; i++) { HexUtil.appendHexString(s, getUnchecked(i)); } return s.toString(); } }
/** * Return a hexidecimal String representation of the current buffer with each byte * displayed in a 2 character hexidecimal format. Useful for debugging. * Convert a ByteBuffer to a String with a hexidecimal format. * @param offset * @param length * @return The string in hex representation */ public String toHexString(int offset, int length) { // is offset, length valid for the current size() of our internal byte[] checkOffsetLength(size(), offset, length); // if length is 0, return an empty string if (length == 0 || size() == 0) { return ""; } StringBuilder s = new StringBuilder(length * 2); int end = offset + length; for (int i = offset; i < end; i++) { HexUtil.appendHexString(s, getUnchecked(i)); } return s.toString(); } }
ByteBuffer.checkOffsetLength(size(), offset, length);
ByteBuffer.checkOffsetLength(size(), offset, length);
/** * Adds a byte array to this buffer starting from the offset up to the * length requested. If the free space remaining in the buffer * is not large enough, this method will throw a BufferSizeException. * @param bytes A byte array to add to this buffer. * @param offset The offset within the byte array to begin to add * @param length The length starting from offset to begin to add * @throws BufferSizeException If this buffer's free space is not large enough to store add the byte array */ public void add(byte[] bytes, int offset, int length) throws IllegalArgumentException, BufferSizeException { // validate the bytes, offset, length checkOffsetLength(bytes.length, offset, length); // is there enough free space in this buffer to add the entire array if (length > free()) { throw new BufferSizeException("Buffer does not have enough free space (" + free() + " bytes) to add " + length + " bytes of data"); } // add each byte to this array for (int i = 0; i < length; i++) { try { this.add(bytes[i+offset]); } catch (BufferIsFullException e) { // this should be an impossible case since we checked the size() above logger.error("Buffer is full even though this method checked its size() ahead of time", e); throw new BufferSizeException(e.getMessage()); } } }
/** * Adds a byte array to this buffer starting from the offset up to the * length requested. If the free space remaining in the buffer * is not large enough, this method will throw a BufferSizeException. * @param bytes A byte array to add to this buffer. * @param offset The offset within the byte array to begin to add * @param length The length starting from offset to begin to add * @throws BufferSizeException If this buffer's free space is not large enough to store add the byte array */ public void add(byte[] bytes, int offset, int length) throws IllegalArgumentException, BufferSizeException { // validate the bytes, offset, length checkOffsetLength(bytes.length, offset, length); // is there enough free space in this buffer to add the entire array if (length > free()) { throw new BufferSizeException("Buffer does not have enough free space (" + free() + " bytes) to add " + length + " bytes of data"); } // add each byte to this array for (int i = 0; i < length; i++) { try { this.add(bytes[i+offset]); } catch (BufferIsFullException e) { // this should be an impossible case since we checked the size() above logger.error("Buffer is full even though this method checked its size() ahead of time", e); throw new BufferSizeException(e.getMessage()); } } }