You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/06/28 21:59:54 UTC
[commons-text] branch master updated: Add
org.apache.commons.text.TextStringBuilder.readFrom(Reader, int).
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 d57ae18 Add org.apache.commons.text.TextStringBuilder.readFrom(Reader, int).
d57ae18 is described below
commit d57ae18c2d9fa72c93ecdfa9fc2cb7e47c9d8573
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jun 28 17:59:47 2020 -0400
Add org.apache.commons.text.TextStringBuilder.readFrom(Reader, int).
---
src/changes/changes.xml | 1 +
.../org/apache/commons/text/TextStringBuilder.java | 26 ++++++-
.../apache/commons/text/TextStringBuilderTest.java | 91 ++++++++++++++++++----
3 files changed, 102 insertions(+), 16 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d10edb9..dac7735 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -58,6 +58,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function<String, V>).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BiStringLookup and implementation BiFunctionStringLookup.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.toString(int, int).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.readFrom(Reader, int).</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] junit-jupiter 5.5.1 -> 5.5.2.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] org.assertj:assertj-core 3.13.2 -> 3.16.1.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[build] com.puppycrawl.tools:checkstyle 8.23 -> 8.27.</action>
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index 72a945b..4ce6bcd 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -2695,6 +2695,30 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
}
/**
+ * If possible, reads chars from the provided {@link Reader} directly into underlying character buffer without
+ * making extra copies.
+ *
+ * @param reader Reader to read.
+ * @param count The maximum characters 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, final int count) throws IOException {
+ final int oldSize = size;
+ ensureCapacity(size + count);
+ int readCount;
+ int target = count;
+ while (target > 0 && (readCount = reader.read(buffer, size, target)) != -1) {
+ target -= readCount;
+ size += readCount;
+ }
+ return size - oldSize;
+ }
+
+ /**
* Replaces a portion of the string builder with another string. The length of the inserted string does not have to
* match the removed length.
*
@@ -3182,7 +3206,7 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable
* @throws IndexOutOfBoundsException if the index is invalid
* @since 1.9
*/
- public String toString(final int startIndex, int count) {
+ public String toString(final int startIndex, final int count) {
validateIndices(startIndex, startIndex + count);
return new String(buffer, startIndex, count);
}
diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
index 8be8278..8151e8b 100644
--- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
+++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
@@ -266,7 +266,7 @@ public class TextStringBuilderTest {
try (Reader reader = sb.asReader()) {
assertEquals('s', reader.read());
reader.mark(-1);
- char[] array = new char[3];
+ final char[] array = new char[3];
assertEquals(3, reader.read(array, 0, 3));
assertEquals('o', array[0]);
assertEquals('m', array[1]);
@@ -1422,7 +1422,6 @@ public class TextStringBuilderTest {
assertEquals("Test 123", sb.toString());
}
- // -----------------------------------------------------------------------
@Test
public void testReadFromReader() throws Exception {
String s = "";
@@ -1444,6 +1443,68 @@ public class TextStringBuilderTest {
assertEquals("Test 123", sb.toString());
}
+ @Test
+ public void testReadFromReaderInt() throws Exception {
+ String s = "";
+ for (int i = 0; i < 100; ++i) {
+ final TextStringBuilder sb = new TextStringBuilder();
+ final int len = sb.readFrom(new StringReader(s), s.length());
+
+ assertEquals(s.length(), len);
+ assertEquals(s, sb.toString());
+
+ s += Integer.toString(i);
+ }
+ //
+ TextStringBuilder sb;
+ int count;
+ int target;
+ final String source = "abc";
+ final int sourceLen = source.length();
+ //
+ target = -1;
+ sb = new TextStringBuilder();
+ count = sb.readFrom(new StringReader(source), target);
+ assertEquals(0, count);
+ assertEquals(0, sb.size());
+ assertEquals(source.substring(0, 0), sb.toString());
+ //
+ target = 0;
+ sb = new TextStringBuilder();
+ count = sb.readFrom(new StringReader(source), target);
+ assertEquals(target, count);
+ assertEquals(target, sb.size());
+ assertEquals(source.substring(0, target), sb.toString());
+ //
+ target = 1;
+ sb = new TextStringBuilder();
+ count = sb.readFrom(new StringReader(source), target);
+ assertEquals(target, count);
+ assertEquals(target, sb.size());
+ assertEquals(source.substring(0, target), sb.toString());
+ //
+ target = 2;
+ sb = new TextStringBuilder();
+ count = sb.readFrom(new StringReader(source), target);
+ assertEquals(target, count);
+ assertEquals(target, sb.size());
+ assertEquals(source.substring(0, target), sb.toString());
+ //
+ target = 3;
+ sb = new TextStringBuilder();
+ count = sb.readFrom(new StringReader(source), target);
+ assertEquals(target, count);
+ assertEquals(target, sb.size());
+ assertEquals(source.substring(0, target), sb.toString());
+ //
+ target = 4;
+ sb = new TextStringBuilder();
+ count = sb.readFrom(new StringReader(source), target);
+ assertEquals(sourceLen, count);
+ assertEquals(sourceLen, sb.size());
+ assertEquals(source.substring(0, sourceLen), sb.toString());
+ }
+
// -----------------------------------------------------------------------
@Test
public void testReplace_int_int_String() {
@@ -1973,19 +2034,6 @@ public class TextStringBuilderTest {
assertThrows(IndexOutOfBoundsException.class, () -> sb.substring(15, 20));
}
- @Test
- public void testToStringIntInt() {
- final TextStringBuilder sb = new TextStringBuilder("hello goodbye");
- assertEquals("hello", sb.substring(0, 5));
- assertEquals("hello goodbye".substring(0, 6), sb.toString(0, 6));
-
- assertEquals("goodbye", sb.toString(6, 7));
-
- assertThrows(IndexOutOfBoundsException.class, () -> sb.toString(-1, 5));
-
- assertThrows(IndexOutOfBoundsException.class, () -> sb.toString(15, 20));
- }
-
// -----------------------------------------------------------------------
@Test
public void testToCharArray() {
@@ -2055,6 +2103,19 @@ public class TextStringBuilderTest {
assertEquals(new StringBuilder("junit").toString(), sb.toStringBuilder().toString());
}
+ @Test
+ public void testToStringIntInt() {
+ final TextStringBuilder sb = new TextStringBuilder("hello goodbye");
+ assertEquals("hello", sb.substring(0, 5));
+ assertEquals("hello goodbye".substring(0, 6), sb.toString(0, 6));
+
+ assertEquals("goodbye", sb.toString(6, 7));
+
+ assertThrows(IndexOutOfBoundsException.class, () -> sb.toString(-1, 5));
+
+ assertThrows(IndexOutOfBoundsException.class, () -> sb.toString(15, 20));
+ }
+
// -----------------------------------------------------------------------
@Test
public void testTrim() {