Skip to content

Commit 08fe573

Browse files
committed
use our own StringUtils at more places
1 parent 6057398 commit 08fe573

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

src/main/java/org/htmlunit/DefaultPageCreator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.util.Locale;
2222

23-
import org.apache.commons.lang3.ArrayUtils;
2423
import org.htmlunit.html.DomElement;
2524
import org.htmlunit.html.Html;
2625
import org.htmlunit.html.HtmlPage;
@@ -281,7 +280,10 @@ private static byte[] read(final InputStream stream, final int maxNb) throws IOE
281280
if (nbRead == buffer.length) {
282281
return buffer;
283282
}
284-
return ArrayUtils.subarray(buffer, 0, nbRead);
283+
284+
final byte[] result = new byte[nbRead];
285+
System.arraycopy(buffer, 0, result, 0, nbRead);
286+
return result;
285287
}
286288

287289
/**

src/main/java/org/htmlunit/html/serializer/HtmlSerializerNormalizedText.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Iterator;
2121
import java.util.List;
2222

23-
import org.apache.commons.lang3.StringUtils;
2423
import org.htmlunit.Page;
2524
import org.htmlunit.SgmlPage;
2625
import org.htmlunit.WebWindow;
@@ -61,6 +60,7 @@
6160
import org.htmlunit.html.HtmlUnorderedList;
6261
import org.htmlunit.html.TableRowGroup;
6362
import org.htmlunit.html.serializer.HtmlSerializerNormalizedText.HtmlSerializerTextBuilder.Mode;
63+
import org.htmlunit.util.StringUtils;
6464

6565
/**
6666
* Utility to handle conversion from HTML code to string.
@@ -634,7 +634,7 @@ public void append(final String content, final Mode mode) {
634634

635635
String text = content;
636636
if (mode == Mode.PRESERVE_BLANK_NEWLINE) {
637-
text = StringUtils.stripEnd(text, null);
637+
text = StringUtils.trimRight(text);
638638
}
639639

640640
boolean crFound = false;

src/main/java/org/htmlunit/util/StringUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,4 +915,30 @@ public static float toFloat(final String str, final float defaultValue) {
915915
return defaultValue;
916916
}
917917
}
918+
919+
/**
920+
* Strips any whitespace from the end of a String.
921+
* <p>
922+
* A {@code null} input String returns {@code null}. An empty string ("") input returns the empty string.
923+
* </p>
924+
*
925+
* @param str the String to remove characters from, may be null.
926+
* @return the stripped String, {@code null} if null String input.
927+
*/
928+
public static String trimRight(final String str) {
929+
if (isEmptyOrNull(str)) {
930+
return str;
931+
}
932+
933+
int end = str.length();
934+
while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) {
935+
end--;
936+
}
937+
938+
if (end == str.length()) {
939+
return str;
940+
}
941+
942+
return str.substring(0, end);
943+
}
918944
}

src/test/java/org/htmlunit/util/StringUtilsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,4 +556,19 @@ public void substringAfter() throws Exception {
556556
assertEquals("", StringUtils.substringAfter("abc", "c"));
557557
assertEquals("", StringUtils.substringAfter("abc", "d"));
558558
}
559+
560+
/**
561+
* @throws Exception if the test fails
562+
*/
563+
@Test
564+
public void trimRight() throws Exception {
565+
assertNull(StringUtils.trimRight(null));
566+
assertEquals(StringUtils.EMPTY_STRING, StringUtils.trimRight(""));
567+
assertEquals("", StringUtils.trimRight(StringUtils.EMPTY_STRING));
568+
569+
assertEquals("abc", StringUtils.trimRight("abc"));
570+
assertEquals(" abc", StringUtils.trimRight(" abc"));
571+
assertEquals("abc", StringUtils.trimRight("abc "));
572+
assertEquals(" a b c", StringUtils.trimRight(" a b c "));
573+
}
559574
}

0 commit comments

Comments
 (0)