You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Tom Strijmeers (Jira)" <ji...@apache.org> on 2022/09/20 11:34:00 UTC

[jira] [Created] (TEXT-218) Add method writeTo(Writer):void to TextStringBuilder

Tom Strijmeers created TEXT-218:
-----------------------------------

             Summary: Add method writeTo(Writer):void to TextStringBuilder
                 Key: TEXT-218
                 URL: https://issues.apache.org/jira/browse/TEXT-218
             Project: Commons Text
          Issue Type: New Feature
    Affects Versions: 1.9
            Reporter: Tom Strijmeers


It would be nice if the {{org.apache.commons.text.TextStringBuilder}} had methods to write its internal char buffer to a Writer. The opposite of the current {{readFrom(java.io.Reader):int}} and {{readFrom(java.io.Reader, int):int}} methods.

The javadoc of {{org.apache.commons.text.TextStringBuilder}} states that "subclasses have direct access to character array". But that is only partially true in my opinion. The internal char array is private protected and the {{getBuffer():char[]}} method is package protected.
Meaning that I could create a subclass but it has to be in the {{org.apache.commons.text}} package. And that's something I don't like doing.

So giving the TextStringBuilder to ability to write out its internal buffer is a good alternative in my opinion.
{code:java}
    /**
     * Writes all chars from the internal buffer directly to the provided {@link java.io.Writer} without making extra copies.
     *
     * @param writer Writer to write
     * @throws IOException if an I/O error occurs.
     */
    public void writeTo(Writer writer) throws IOException {
        if(length() == 0) {
            return;
        }
        writer.write(getBuffer(), 0, length());
    }

    /**
     * Writes a portion of the chars from the internal buffer directly to the provided {@link java.io.Writer} without making extra copies.
     *
     * @param writer Writer to write
     * @param offset Offset from which to start writing characters from the internal buffer
     * @param length Number of characters to write
     * @throws IOException                     if an I/O error occurs.
     * @throws StringIndexOutOfBoundsException if any of the following is true:
     *                                         <ul>
     *                                         <li>{@code offset} is negative</li>
     *                                         <li>{@code offset} is greater than {@code this.length()}</li>
     *                                         <li>{@code length} is negative</li>
     *                                         <li>{@code length} is greater than {@code this.length()}</li>
     *                                         <li>{@code offset} and {@code length} combined is greater than {@code this.length()}</li>
     *                                         </ul>
     */
    public void writeTo(Writer writer, int offset, int length) throws IOException {
        if(offset < 0 || offset > length()) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if(length < 0 || length > length()) {
            throw new StringIndexOutOfBoundsException(length);
        }
        if((offset + length) > length()) {
            throw new StringIndexOutOfBoundsException(length);
        }
        if(length == 0) {
            return;
        }
        writer.write(getBuffer(), offset, length);
    }
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)