This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git


The following commit(s) were added to refs/heads/master by this push:
     new 509070e  Refactor 
org.apache.commons.text.TextStringBuilder.readFrom(Readable) to split out 
overrides.
509070e is described below

commit 509070ee5c33edef7660f2e8c27c6f4ebd835978
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Jun 22 20:50:18 2020 -0400

    Refactor org.apache.commons.text.TextStringBuilder.readFrom(Readable) to
    split out overrides.
    
    - TextStringBuilder.readFrom(CharBuffer)
    - TextStringBuilder.readFrom(Reader)
    - These are already tested.
---
 .../org/apache/commons/text/TextStringBuilder.java | 57 +++++++++++++++++-----
 1 file changed, 44 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java 
b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index adbacc4..6de3a0a 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -448,7 +448,26 @@ public class TextStringBuilder implements CharSequence, 
Appendable, Serializable
         System.arraycopy(buffer, startIndex, destination, destinationIndex, 
endIndex - startIndex);
     }
 
-    // -----------------------------------------------------------------------
+    /**
+     * If possible, reads chars from the provided {@link CharBuffer} directly 
into underlying character buffer without
+     * making extra copies.
+     *
+     * @param charBuffer CharBuffer to read.
+     * @return The number of characters read.
+     * @throws IOException if an I/O error occurs.
+     *
+     * @see #appendTo(Appendable)
+     * @since 1.9
+     */
+    public int readFrom(final CharBuffer charBuffer) throws IOException {
+        final int oldSize = size;
+        final int remaining = charBuffer.remaining();
+        ensureCapacity(size + remaining);
+        charBuffer.get(buffer, size, remaining);
+        size += remaining;
+        return size - oldSize;
+    }
+
     /**
      * If possible, reads chars from the provided {@link Readable} directly 
into underlying character buffer without
      * making extra copies.
@@ -464,19 +483,9 @@ public class TextStringBuilder implements CharSequence, 
Appendable, Serializable
     public int readFrom(final Readable readable) throws IOException {
         final int oldSize = size;
         if (readable instanceof Reader) {
-            final Reader r = (Reader) readable;
-            ensureCapacity(size + 1);
-            int read;
-            while ((read = r.read(buffer, size, buffer.length - size)) != -1) {
-                size += read;
-                ensureCapacity(size + 1);
-            }
+            return readFrom((Reader) readable);
         } else if (readable instanceof CharBuffer) {
-            final CharBuffer cb = (CharBuffer) readable;
-            final int remaining = cb.remaining();
-            ensureCapacity(size + remaining);
-            cb.get(buffer, size, remaining);
-            size += remaining;
+            return readFrom((CharBuffer) readable);
         } else {
             while (true) {
                 ensureCapacity(size + 1);
@@ -491,6 +500,28 @@ public class TextStringBuilder implements CharSequence, 
Appendable, Serializable
         return size - oldSize;
     }
 
+    /**
+     * If possible, reads chars from the provided {@link Reader} directly into 
underlying character buffer without
+     * making extra copies.
+     *
+     * @param reader Reader to read.
+     * @return The number of characters read.
+     * @throws IOException if an I/O error occurs.
+     *
+     * @see #appendTo(Appendable)
+     * @since 1.9
+     */
+    public int readFrom(final Reader reader) throws IOException {
+        final int oldSize = size;
+        ensureCapacity(size + 1);
+        int read;
+        while ((read = reader.read(buffer, size, buffer.length - size)) != -1) 
{
+            size += read;
+            ensureCapacity(size + 1);
+        }
+        return size - oldSize;
+    }
+
     // -----------------------------------------------------------------------
     /**
      * Appends the new line string to this string builder.

Reply via email to