/** * Adds one byte to the buffer and throws an exception if the buffer is * full. * @param b Byte to add to the buffer. * @throws BufferIsFullException If the buffer is full and the byte cannot * be stored. */ public void add(byte b) throws BufferIsFullException { if (isFull()) { throw new BufferIsFullException("Buffer is full and has reached maximum capacity (" + capacity() + ")"); } // buffer is not full this.buffer[this.currentWritePosition] = b; this.currentWritePosition = (this.currentWritePosition + 1) % this.buffer.length; this.currentBufferSize += 1; }
/** * 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()); } } }
/** * Adds one byte to the buffer and throws an exception if the buffer is * full. * @param b Byte to add to the buffer. * @throws BufferIsFullException If the buffer is full and the byte cannot * be stored. */ public void add(byte b) throws BufferIsFullException { if (isFull()) { throw new BufferIsFullException("Buffer is full and has reached maximum capacity (" + capacity() + ")"); } // buffer is not full this.buffer[this.currentWritePosition] = b; this.currentWritePosition = (this.currentWritePosition + 1) % this.buffer.length; this.currentBufferSize += 1; }