Class StringBuilderEncoder

  • All Implemented Interfaces:
    Encoder<java.lang.StringBuilder>

    public class StringBuilderEncoder
    extends java.lang.Object
    implements Encoder<java.lang.StringBuilder>
    Encoder for StringBuilders that uses ThreadLocals to avoid locking as much as possible.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int byteBufferSize  
      private int charBufferSize  
      private java.nio.charset.Charset charset  
      private static int DEFAULT_BYTE_BUFFER_SIZE  
      private java.lang.ThreadLocal<java.lang.Object[]> threadLocal
      This ThreadLocal uses raw and inconvenient Object[] to store three heterogeneous objects (CharEncoder, CharBuffer and ByteBuffer) instead of a custom class, because it needs to contain JDK classes, no custom (Log4j) classes.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringBuilderEncoder​(java.nio.charset.Charset charset)  
      StringBuilderEncoder​(java.nio.charset.Charset charset, int charBufferSize, int byteBufferSize)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void encode​(java.lang.StringBuilder source, ByteBufferDestination destination)
      Encodes the specified source object to some binary representation and writes the result to the specified destination.
      private java.lang.Object[] getThreadLocalState()  
      private void logEncodeTextException​(java.lang.Exception ex, java.lang.StringBuilder text, ByteBufferDestination destination)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_BYTE_BUFFER_SIZE

        private static final int DEFAULT_BYTE_BUFFER_SIZE
        See Also:
        Constant Field Values
      • threadLocal

        private final java.lang.ThreadLocal<java.lang.Object[]> threadLocal
        This ThreadLocal uses raw and inconvenient Object[] to store three heterogeneous objects (CharEncoder, CharBuffer and ByteBuffer) instead of a custom class, because it needs to contain JDK classes, no custom (Log4j) classes. Where possible putting only JDK classes in ThreadLocals is preferable to avoid memory leaks in web containers: the Log4j classes may be loaded by a separate class loader which cannot be garbage collected if a thread pool threadlocal still has a reference to it. Using just one ThreadLocal instead of three separate ones is an optimization: ThreadLocal.ThreadLocalMap is polluted less, ThreadLocal.ThreadLocalMap#get() is called only once on each call to encode(java.lang.StringBuilder, org.apache.logging.log4j.core.layout.ByteBufferDestination) instead of three times.
      • charset

        private final java.nio.charset.Charset charset
      • charBufferSize

        private final int charBufferSize
      • byteBufferSize

        private final int byteBufferSize
    • Constructor Detail

      • StringBuilderEncoder

        public StringBuilderEncoder​(java.nio.charset.Charset charset)
      • StringBuilderEncoder

        public StringBuilderEncoder​(java.nio.charset.Charset charset,
                                    int charBufferSize,
                                    int byteBufferSize)
    • Method Detail

      • encode

        public void encode​(java.lang.StringBuilder source,
                           ByteBufferDestination destination)
        Description copied from interface: Encoder
        Encodes the specified source object to some binary representation and writes the result to the specified destination.
        Specified by:
        encode in interface Encoder<java.lang.StringBuilder>
        Parameters:
        source - the object to encode.
        destination - holds the ByteBuffer to write into.
      • getThreadLocalState

        private java.lang.Object[] getThreadLocalState()
      • logEncodeTextException

        private void logEncodeTextException​(java.lang.Exception ex,
                                            java.lang.StringBuilder text,
                                            ByteBufferDestination destination)