/** * Deletes the first N bytes of the buffer. Avoids allocating any memory * since there is no return value for this method. If you want to delete * and also see the deleted data as a return value, please use the remove() * methods. */ public void delete(int count) throws BufferSizeException { if ((count < 0) || (count > capacity())) { throw new IllegalArgumentException("Can only delete between 0 and " + capacity() + " bytes from buffer, you passed in=" + count); } if (count > size()) { throw new BufferSizeException("Buffer size (" + size() + ") not large enough to delete (" + count + ") bytes"); } this.currentReadPosition = (this.currentReadPosition + count) % this.buffer.length; this.currentBufferSize -= count; }
/** * Gets the byte at the given index relative to the beginning the circular * buffer. The index must be a value between 0 and the buffer capacity. * @param index The index of the byte relative to the beginning the buffer * (a value between 0 and the the current size). * @return The byte at the given position relative to the beginning the buffer. * @throws BufferSizeException If the index is >= size() */ public byte get(int index) throws IllegalArgumentException, BufferSizeException { if ((index < 0) || (index >= capacity())) { throw new IllegalArgumentException("The buffer index must be a value between 0 and " + capacity() + "!"); } if (index >= size()) { throw new BufferSizeException("Index " + index + " is >= buffer size of " + size()); } return this.buffer[(this.currentReadPosition + index) % this.buffer.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()); } } }
/** * Gets the byte at the given index relative to the beginning the circular * buffer. The index must be a value between 0 and the buffer capacity. * @param index The index of the byte relative to the beginning the buffer * (a value between 0 and the the current size). * @return The byte at the given position relative to the beginning the buffer. * @throws BufferSizeException If the index is >= size() */ public byte get(int index) throws IllegalArgumentException, BufferSizeException { if ((index < 0) || (index >= capacity())) { throw new IllegalArgumentException("The buffer index must be a value between 0 and " + capacity() + "!"); } if (index >= size()) { throw new BufferSizeException("Index " + index + " is >= buffer size of " + size()); } return this.buffer[(this.currentReadPosition + index) % this.buffer.length]; }
/** * Deletes the first N bytes of the buffer. Avoids allocating any memory * since there is no return value for this method. If you want to delete * and also see the deleted data as a return value, please use the remove() * methods. */ public void delete(int count) throws BufferSizeException { if ((count < 0) || (count > capacity())) { throw new IllegalArgumentException("Can only delete between 0 and " + capacity() + " bytes from buffer, you passed in=" + count); } if (count > size()) { throw new BufferSizeException("Buffer size (" + size() + ") not large enough to delete (" + count + ") bytes"); } this.currentReadPosition = (this.currentReadPosition + count) % this.buffer.length; this.currentBufferSize -= count; }
throw new BufferSizeException("Buffer size (" + size() + ") not large enough to remove (" + count + ") bytes"); throw new BufferSizeException(e.getMessage());
throw new BufferSizeException("Buffer size (" + size() + ") not large enough to remove (" + count + ") bytes"); throw new BufferSizeException(e.getMessage());