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

vy pushed a commit to branch scheduled-for-deletion/LOG4J2-930
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 00c615fe8be24779c9d14d92da1418790969196a
Author: rpopma <[email protected]>
AuthorDate: Mon Jan 12 17:34:57 2015 +0900

    cleanup, docs, added methods
---
 .../logging/log4j/core/pattern/TextBuffer.java     | 79 +++++++++++++++++++---
 1 file changed, 69 insertions(+), 10 deletions(-)

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 0c9686f80b..095df2962b 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));
+    }
 }

Reply via email to