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 2015/01/12 09:53:06 UTC
[09/18] logging-log4j2 git commit: cleanup, docs, added methods
cleanup, docs, added methods
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/00c615fe
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/00c615fe
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/00c615fe
Branch: refs/heads/LOG4J2-930
Commit: 00c615fe8be24779c9d14d92da1418790969196a
Parents: 17b23f8
Author: rpopma <rp...@apache.org>
Authored: Mon Jan 12 17:34:57 2015 +0900
Committer: rpopma <rp...@apache.org>
Committed: Mon Jan 12 17:34:57 2015 +0900
----------------------------------------------------------------------
.../logging/log4j/core/pattern/TextBuffer.java | 79 +++++++++++++++++---
1 file changed, 69 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/00c615fe/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/TextBuffer.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/TextBuffer.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/TextBuffer.java
index 0c9686f..095df29 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/TextBuffer.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/TextBuffer.java
@@ -17,49 +17,98 @@
package org.apache.logging.log4j.core.pattern;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+
+import org.apache.logging.log4j.core.util.Assert;
+import org.apache.logging.log4j.core.util.Charsets;
+
/**
* Buffer implementation that internally tracks the appended data as text.
*/
public class TextBuffer implements Buffer {
- private StringBuilder buffer = new StringBuilder(1024);
+ private final StringBuilder buffer;
+
+ /**
+ * Constructs a {@code TextBuffer} with a 1024-character initial capacity.
+ */
+ public TextBuffer() {
+ this(new StringBuilder(1024));
+ }
+
+ /**
+ * Constructs a {@code TextBuffer} wrapping the specified {@code StringBuilder}.
+ *
+ * @param buffer the internal buffer to use
+ */
+ public TextBuffer(final StringBuilder buffer) {
+ this.buffer = Assert.requireNonNull(buffer, "buffer is null");
+ }
@Override
- public TextBuffer append(Object object) {
+ public TextBuffer append(final Object object) {
return append(String.valueOf(object));
}
@Override
- public TextBuffer append(String text) {
+ public TextBuffer append(final String text) {
buffer.append(text);
return this;
}
+ @Override
+ public TextBuffer append(final String unformatted, final FormattingInfo formattingInfo) {
+ final int position = buffer.length();
+ buffer.append(unformatted);
+ formattingInfo.format(position, buffer);
+ return this;
+ }
+
+ /**
+ * Appends the specified long to this buffer and applies the specified alignment and width adjustments.
+ *
+ * @param value the value to append (before applying alignment and width adjustments)
+ * @param formattingInfo can apply alignment and width adjustments
+ */
+ public TextBuffer append(final long value, final FormattingInfo formattingInfo) {
+ final int position = buffer.length();
+ buffer.append(value);
+ formattingInfo.format(position, buffer);
+ return this;
+ }
+
/*
* (non-Javadoc)
*
* @see org.apache.logging.log4j.core.pattern.Buffer#append(byte[])
*/
@Override
- public TextBuffer append(byte[] data) {
+ public TextBuffer append(final byte[] data) {
final String text = new String(data); // don't specify Charset: avoid StringDecoder instantiation
buffer.append(text);
return this;
}
@Override
- public TextBuffer append(char ch) {
+ public TextBuffer append(final byte ch) {
+ buffer.append((char) ch);
+ return this;
+ }
+
+ @Override
+ public TextBuffer append(final char ch) {
buffer.append(ch);
return this;
}
@Override
- public TextBuffer append(int number) {
+ public TextBuffer append(final int number) {
buffer.append(number);
return this;
}
@Override
- public TextBuffer append(long number) {
+ public TextBuffer append(final long number) {
buffer.append(number);
return this;
}
@@ -80,7 +129,7 @@ public class TextBuffer implements Buffer {
* @see org.apache.logging.log4j.core.pattern.Buffer#setLength(int)
*/
@Override
- public void setLength(int length) {
+ public void setLength(final int length) {
buffer.setLength(length);
}
@@ -92,7 +141,7 @@ public class TextBuffer implements Buffer {
@Override
public boolean hasTrailingWhitespace() {
final int len = buffer.length();
- return len > 0 && !Character.isWhitespace(buffer.charAt(len - 1));
+ return len == 0 || Character.isWhitespace(buffer.charAt(len - 1));
}
/**
@@ -105,7 +154,7 @@ public class TextBuffer implements Buffer {
* @return the char value at the specified index.
* @throws IndexOutOfBoundsException if index is negative or greater than or equal to length().
*/
- public char charAt(int index) {
+ public char charAt(final int index) {
return buffer.charAt(index);
}
@@ -116,4 +165,14 @@ public class TextBuffer implements Buffer {
public String toString() {
return buffer.toString();
}
+
+ /**
+ * Writes the content of this buffer into the specified destination.
+ *
+ * @param destination the destination buffer to write into
+ * @param charset used to convert the buffered text to bytes
+ */
+ public void writeInto(final ByteBuffer destination, final Charset charset) {
+ destination.put(Charsets.getBytes(buffer.toString(), charset));
+ }
}