Class Unbox

java.lang.Object
org.apache.logging.log4j.util.Unbox

public class Unbox extends Object
Utility for preventing primitive parameter values from being auto-boxed. Auto-boxing creates temporary objects which contribute to pressure on the garbage collector. With this utility users can convert primitive values directly into text without allocating temporary objects.

Example usage:

 import static org.apache.logging.log4j.util.Unbox.box;
 ...
 long longValue = 123456L;
 double doubleValue = 3.14;
 // prevent primitive values from being auto-boxed
 logger.debug("Long value={}, double value={}", box(longValue), box(doubleValue));
 

This class manages a small thread-local ring buffer of StringBuilders. Each time one of the box() methods is called, the next slot in the ring buffer is used, until the ring buffer is full and the first slot is reused. By default the Unbox ring buffer has 32 slots, so user code can have up to 32 boxed primitives in a single logger call.

If more slots are required, set system property log4j.unbox.ringbuffer.size to the desired ring buffer size. Note that the specified number will be rounded up to the nearest power of 2.

Since:
2.6
  • Method Summary

    Modifier and Type
    Method
    Description
    box(boolean value)
    Returns a StringBuilder containing the text representation of the specified primitive value.
    box(byte value)
    Returns a StringBuilder containing the text representation of the specified primitive value.
    box(char value)
    Returns a StringBuilder containing the text representation of the specified primitive value.
    box(double value)
    Returns a StringBuilder containing the text representation of the specified primitive value.
    box(float value)
    Returns a StringBuilder containing the text representation of the specified primitive value.
    box(int value)
    Returns a StringBuilder containing the text representation of the specified primitive value.
    box(long value)
    Returns a StringBuilder containing the text representation of the specified primitive value.
    box(short value)
    Returns a StringBuilder containing the text representation of the specified primitive value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • box

      public static StringBuilder box(float value)
      Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.
      Parameters:
      value - the value whose text representation to return
      Returns:
      a StringBuilder containing the text representation of the specified primitive value
    • box

      public static StringBuilder box(double value)
      Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.
      Parameters:
      value - the value whose text representation to return
      Returns:
      a StringBuilder containing the text representation of the specified primitive value
    • box

      public static StringBuilder box(short value)
      Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.
      Parameters:
      value - the value whose text representation to return
      Returns:
      a StringBuilder containing the text representation of the specified primitive value
    • box

      public static StringBuilder box(int value)
      Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.
      Parameters:
      value - the value whose text representation to return
      Returns:
      a StringBuilder containing the text representation of the specified primitive value
    • box

      public static StringBuilder box(char value)
      Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.
      Parameters:
      value - the value whose text representation to return
      Returns:
      a StringBuilder containing the text representation of the specified primitive value
    • box

      public static StringBuilder box(long value)
      Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.
      Parameters:
      value - the value whose text representation to return
      Returns:
      a StringBuilder containing the text representation of the specified primitive value
    • box

      public static StringBuilder box(byte value)
      Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.
      Parameters:
      value - the value whose text representation to return
      Returns:
      a StringBuilder containing the text representation of the specified primitive value
    • box

      public static StringBuilder box(boolean value)
      Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.
      Parameters:
      value - the value whose text representation to return
      Returns:
      a StringBuilder containing the text representation of the specified primitive value