Class CharArrayBuffer

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.CharSequence

    public final class CharArrayBuffer
    extends java.lang.Object
    implements java.lang.CharSequence, java.io.Serializable
    A resizable char array.
    Since:
    4.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      CharArrayBuffer​(int capacity)
      Creates an instance of CharArrayBuffer with the given initial capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void append​(byte[] b, int off, int len)
      Appends len bytes to this buffer from the given source array starting at index off.
      void append​(char ch)
      Appends ch char to this buffer.
      void append​(char[] b, int off, int len)
      Appends len chars to this buffer from the given source array starting at index off.
      void append​(java.lang.Object obj)
      Appends chars of the textual representation of the given object to this buffer.
      void append​(java.lang.String str)
      Appends chars of the given string to this buffer.
      void append​(ByteArrayBuffer b, int off, int len)
      Appends len bytes to this buffer from the given source array starting at index off.
      void append​(CharArrayBuffer b)
      Appends all chars to this buffer from the given source buffer starting at index 0.
      void append​(CharArrayBuffer b, int off, int len)
      Appends len chars to this buffer from the given source buffer starting at index off.
      char[] buffer()
      Returns reference to the underlying char array.
      int capacity()
      Returns the current capacity.
      char charAt​(int i)
      Returns the char value in this buffer at the specified index.
      void clear()
      Clears content of the buffer.
      void ensureCapacity​(int required)
      Ensures that the capacity is at least equal to the specified minimum.
      int indexOf​(int ch)
      Returns the index within this buffer of the first occurrence of the specified character, starting the search at 0 and finishing at length().
      int indexOf​(int ch, int from, int to)
      Returns the index within this buffer of the first occurrence of the specified character, starting the search at the specified beginIndex and finishing at endIndex.
      boolean isEmpty()
      Returns true if this buffer is empty, that is, its length() is equal to 0.
      boolean isFull()
      Returns true if this buffer is full, that is, its length() is equal to its capacity().
      int length()
      Returns the length of the buffer (char count).
      void setLength​(int len)
      Sets the length of the buffer.
      java.lang.CharSequence subSequence​(int beginIndex, int endIndex)
      java.lang.String substring​(int beginIndex, int endIndex)
      Returns a substring of this buffer.
      java.lang.String substringTrimmed​(int beginIndex, int endIndex)
      Returns a substring of this buffer with leading and trailing whitespace omitted.
      char[] toCharArray()
      Converts the content of this buffer to an array of chars.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.CharSequence

        chars, codePoints
    • Constructor Detail

      • CharArrayBuffer

        public CharArrayBuffer​(int capacity)
        Creates an instance of CharArrayBuffer with the given initial capacity.
        Parameters:
        capacity - the capacity
    • Method Detail

      • append

        public void append​(char[] b,
                           int off,
                           int len)
        Appends len chars to this buffer from the given source array starting at index off. The capacity of the buffer is increased, if necessary, to accommodate all len chars.
        Parameters:
        b - the chars to be appended.
        off - the index of the first char to append.
        len - the number of chars to append.
        Throws:
        java.lang.IndexOutOfBoundsException - if off is out of range, len is negative, or off + len is out of range.
      • append

        public void append​(java.lang.String str)
        Appends chars of the given string to this buffer. The capacity of the buffer is increased, if necessary, to accommodate all chars.
        Parameters:
        str - the string.
      • append

        public void append​(CharArrayBuffer b,
                           int off,
                           int len)
        Appends len chars to this buffer from the given source buffer starting at index off. The capacity of the destination buffer is increased, if necessary, to accommodate all len chars.
        Parameters:
        b - the source buffer to be appended.
        off - the index of the first char to append.
        len - the number of chars to append.
        Throws:
        java.lang.IndexOutOfBoundsException - if off is out of range, len is negative, or off + len is out of range.
      • append

        public void append​(CharArrayBuffer b)
        Appends all chars to this buffer from the given source buffer starting at index 0. The capacity of the destination buffer is increased, if necessary, to accommodate all length() chars.
        Parameters:
        b - the source buffer to be appended.
      • append

        public void append​(char ch)
        Appends ch char to this buffer. The capacity of the buffer is increased, if necessary, to accommodate the additional char.
        Parameters:
        ch - the char to be appended.
      • append

        public void append​(byte[] b,
                           int off,
                           int len)
        Appends len bytes to this buffer from the given source array starting at index off. The capacity of the buffer is increased, if necessary, to accommodate all len bytes.

        The bytes are converted to chars using simple cast.

        Parameters:
        b - the bytes to be appended.
        off - the index of the first byte to append.
        len - the number of bytes to append.
        Throws:
        java.lang.IndexOutOfBoundsException - if off is out of range, len is negative, or off + len is out of range.
      • append

        public void append​(ByteArrayBuffer b,
                           int off,
                           int len)
        Appends len bytes to this buffer from the given source array starting at index off. The capacity of the buffer is increased, if necessary, to accommodate all len bytes.

        The bytes are converted to chars using simple cast.

        Parameters:
        b - the bytes to be appended.
        off - the index of the first byte to append.
        len - the number of bytes to append.
        Throws:
        java.lang.IndexOutOfBoundsException - if off is out of range, len is negative, or off + len is out of range.
      • append

        public void append​(java.lang.Object obj)
        Appends chars of the textual representation of the given object to this buffer. The capacity of the buffer is increased, if necessary, to accommodate all chars.
        Parameters:
        obj - the object.
      • clear

        public void clear()
        Clears content of the buffer. The underlying char array is not resized.
      • toCharArray

        public char[] toCharArray()
        Converts the content of this buffer to an array of chars.
        Returns:
        char array
      • charAt

        public char charAt​(int i)
        Returns the char value in this buffer at the specified index. The index argument must be greater than or equal to 0, and less than the length of this buffer.
        Specified by:
        charAt in interface java.lang.CharSequence
        Parameters:
        i - the index of the desired char value.
        Returns:
        the char value at the specified index.
        Throws:
        java.lang.IndexOutOfBoundsException - if index is negative or greater than or equal to length().
      • buffer

        public char[] buffer()
        Returns reference to the underlying char array.
        Returns:
        the char array.
      • capacity

        public int capacity()
        Returns the current capacity. The capacity is the amount of storage available for newly appended chars, beyond which an allocation will occur.
        Returns:
        the current capacity
      • length

        public int length()
        Returns the length of the buffer (char count).
        Specified by:
        length in interface java.lang.CharSequence
        Returns:
        the length of the buffer
      • ensureCapacity

        public void ensureCapacity​(int required)
        Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. If the required argument is non-positive, this method takes no action.
        Parameters:
        required - the minimum required capacity.
      • setLength

        public void setLength​(int len)
        Sets the length of the buffer. The new length value is expected to be less than the current capacity and greater than or equal to 0.
        Parameters:
        len - the new length
        Throws:
        java.lang.IndexOutOfBoundsException - if the len argument is greater than the current capacity of the buffer or less than 0.
      • isEmpty

        public boolean isEmpty()
        Returns true if this buffer is empty, that is, its length() is equal to 0.
        Returns:
        true if this buffer is empty, false otherwise.
      • isFull

        public boolean isFull()
        Returns true if this buffer is full, that is, its length() is equal to its capacity().
        Returns:
        true if this buffer is full, false otherwise.
      • indexOf

        public int indexOf​(int ch,
                           int from,
                           int to)
        Returns the index within this buffer of the first occurrence of the specified character, starting the search at the specified beginIndex and finishing at endIndex. If no such character occurs in this buffer within the specified bounds, -1 is returned.

        There is no restriction on the value of beginIndex and endIndex. If beginIndex is negative, it has the same effect as if it were zero. If endIndex is greater than length(), it has the same effect as if it were length(). If the beginIndex is greater than the endIndex, -1 is returned.

        Parameters:
        ch - the char to search for.
        from - the index to start the search from.
        to - the index to finish the search at.
        Returns:
        the index of the first occurrence of the character in the buffer within the given bounds, or -1 if the character does not occur.
      • indexOf

        public int indexOf​(int ch)
        Returns the index within this buffer of the first occurrence of the specified character, starting the search at 0 and finishing at length(). If no such character occurs in this buffer within those bounds, -1 is returned.
        Parameters:
        ch - the char to search for.
        Returns:
        the index of the first occurrence of the character in the buffer, or -1 if the character does not occur.
      • substring

        public java.lang.String substring​(int beginIndex,
                                          int endIndex)
        Returns a substring of this buffer. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.
        Parameters:
        beginIndex - the beginning index, inclusive.
        endIndex - the ending index, exclusive.
        Returns:
        the specified substring.
        Throws:
        java.lang.StringIndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this buffer, or beginIndex is larger than endIndex.
      • substringTrimmed

        public java.lang.String substringTrimmed​(int beginIndex,
                                                 int endIndex)
        Returns a substring of this buffer with leading and trailing whitespace omitted. The substring begins with the first non-whitespace character from beginIndex and extends to the last non-whitespace character with the index lesser than endIndex.
        Parameters:
        beginIndex - the beginning index, inclusive.
        endIndex - the ending index, exclusive.
        Returns:
        the specified substring.
        Throws:
        java.lang.IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this buffer, or beginIndex is larger than endIndex.
      • subSequence

        public java.lang.CharSequence subSequence​(int beginIndex,
                                                  int endIndex)
        Specified by:
        subSequence in interface java.lang.CharSequence
        Since:
        4.4
      • toString

        public java.lang.String toString()
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object