You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2017/04/16 07:56:43 UTC
[4/9] logging-log4j2 git commit: LOG4J2-1858 create trimToMaxSize
utility method in StringBuilders, with test
LOG4J2-1858 create trimToMaxSize utility method in StringBuilders, with test
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f1b71ea7
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f1b71ea7
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f1b71ea7
Branch: refs/heads/master
Commit: f1b71ea775e04731f3a03c2657474fcda205987a
Parents: 11fb367
Author: rpopma <rp...@apache.org>
Authored: Sun Apr 16 16:49:04 2017 +0900
Committer: rpopma <rp...@apache.org>
Committed: Sun Apr 16 16:49:04 2017 +0900
----------------------------------------------------------------------
.../logging/log4j/util/StringBuilders.java | 14 ++++++++
.../logging/log4j/util/StringBuildersTest.java | 37 ++++++++++++++++++++
2 files changed, 51 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f1b71ea7/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java
index a67bfb2..23d7b65 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java
@@ -143,4 +143,18 @@ public final class StringBuilders {
}
return false;
}
+
+ /**
+ * Ensures that the char[] array of the specified StringBuilder does not exceed the specified number of characters.
+ * This method is useful to ensure that excessively long char[] arrays are not kept in memory forever.
+ *
+ * @param stringBuilder the StringBuilder to check
+ * @param maxSize the maximum number of characters the StringBuilder is allowed to have
+ */
+ public static void trimToMaxSize(final StringBuilder stringBuilder, final int maxSize) {
+ if (stringBuilder != null && stringBuilder.length() > maxSize) {
+ stringBuilder.setLength(maxSize);
+ stringBuilder.trimToSize();
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f1b71ea7/log4j-api/src/test/java/org/apache/logging/log4j/util/StringBuildersTest.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/util/StringBuildersTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/util/StringBuildersTest.java
new file mode 100644
index 0000000..061cb8c
--- /dev/null
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/util/StringBuildersTest.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests the StringBuilders class.
+ */
+public class StringBuildersTest {
+ @Test
+ public void trimToMaxSize() throws Exception {
+ StringBuilder sb = new StringBuilder();
+ char[] value = new char[4 * 1024];
+ sb.append(value);
+
+ assertTrue("needs trimming", sb.length() > Constants.MAX_REUSABLE_MESSAGE_SIZE);
+ StringBuilders.trimToMaxSize(sb, Constants.MAX_REUSABLE_MESSAGE_SIZE);
+ assertTrue("trimmed OK", sb.length() <= Constants.MAX_REUSABLE_MESSAGE_SIZE);
+ }
+}
\ No newline at end of file