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

2022-09-20 Thread Tom Strijmeers (Jira)


[ 
https://issues.apache.org/jira/browse/TEXT-218?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17607250#comment-17607250
 ] 

Tom Strijmeers commented on TEXT-218:
-

Hello [~ggregory],

you are absolutely right! I can't belief I looked over the 
{{appendTo(Appendable)}} method.

Tom

 

> 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
>Priority: Major
>
> 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:
>  * 
>  * {@code offset} is 
> negative
>  * {@code offset} is greater 
> than {@code this.length()}
>  * {@code length} is 
> negative
>  * {@code length} is greater 
> than {@code this.length()}
>  * {@code offset} and {@code 
> length} combined is greater than {@code this.length()}
>  * 
>  */
> 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)


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

2022-09-20 Thread Gary D. Gregory (Jira)


[ 
https://issues.apache.org/jira/browse/TEXT-218?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17607154#comment-17607154
 ] 

Gary D. Gregory commented on TEXT-218:
--

Hello [~tstrijmeers] 

-1: This duplicates the functionality of 
{{org.apache.commons.text.TextStringBuilder.appendTo(Appendable)}}

See {{org.apache.commons.text.TextStringBuilderTest.testAppendToWriter()}}

Or am I missing something?

 

> 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
>Priority: Major
>
> 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:
>  * 
>  * {@code offset} is 
> negative
>  * {@code offset} is greater 
> than {@code this.length()}
>  * {@code length} is 
> negative
>  * {@code length} is greater 
> than {@code this.length()}
>  * {@code offset} and {@code 
> length} combined is greater than {@code this.length()}
>  * 
>  */
> 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)