/** * Tests if the buffer is empty. * @return True if the buffer is empty, otherwise false. */ public boolean isEmpty() { return (size() == 0); }
/** * Tests if the buffer is empty. * @return True if the buffer is empty, otherwise false. */ public boolean isEmpty() { return (size() == 0); }
/** * 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. * @return The string in hex representation */ public String toHexString() { return toHexString(0, size()); }
/** * Tests if the buffer is full. * @return True if the buffer is full, otherwise false. */ public boolean isFull() { return (size() == capacity()); }
/** * 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()); }
/** * Tests if the buffer is full. * @return True if the buffer is full, otherwise false. */ public boolean isFull() { return (size() == capacity()); }
/** * 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()); }
/** * 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. * @return The string in hex representation */ public String toHexString() { return toHexString(0, size()); }
/** * Gets the number of free bytes this buffer has remaining for writing. * @return The number of bytes available for writing in this buffer. */ public int free() { return capacity()-size(); }
/** * Returns a string representation of the current buffer. This converts * any internal stored bytes directly to chars and appends them to a String. * @return A byte-to-char string representation of the current buffer. */ @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < size(); i++) { sb.append((char)getUnchecked(i)); } return sb.toString(); }
/** * Gets the number of free bytes this buffer has remaining for writing. * @return The number of bytes available for writing in this buffer. */ public int free() { return capacity()-size(); }
/** * Returns a string representation of the current buffer. This converts * any internal stored bytes directly to chars and appends them to a String. * @return A byte-to-char string representation of the current buffer. */ @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < size(); i++) { sb.append((char)getUnchecked(i)); } return sb.toString(); }
/** * Removes the first (oldest/head) byte in the buffer. If the buffer is empty, * this method will throw a BufferIsEmptyException * @return The first (oldest/head) byte in the buffer * @throws BufferIsEmptyException If the buffer is empty and a byte cannot be * removed. */ public byte remove() throws BufferIsEmptyException { if (size() == 0) { throw new BufferIsEmptyException("Buffer is empty and no bytes available to remove"); } byte b = this.buffer[this.currentReadPosition]; this.currentReadPosition = (this.currentReadPosition + 1) % this.buffer.length; this.currentBufferSize -= 1; return b; }
/** * Removes the first (oldest/head) byte in the buffer. If the buffer is empty, * this method will throw a BufferIsEmptyException * @return The first (oldest/head) byte in the buffer * @throws BufferIsEmptyException If the buffer is empty and a byte cannot be * removed. */ public byte remove() throws BufferIsEmptyException { if (size() == 0) { throw new BufferIsEmptyException("Buffer is empty and no bytes available to remove"); } byte b = this.buffer[this.currentReadPosition]; this.currentReadPosition = (this.currentReadPosition + 1) % this.buffer.length; this.currentBufferSize -= 1; return b; }
/** * 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; }
/** * 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]; }
/** * 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]; }