Package org.apache.logging.log4j.util
Class Unbox
- java.lang.Object
-
- org.apache.logging.log4j.util.Unbox
-
public class Unbox extends java.lang.ObjectUtility 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.sizeto the desired ring buffer size. Note that the specified number will be rounded up to the nearest power of 2.- Since:
- 2.6
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classUnbox.Stateprivate static classUnbox.WebSafeStateState implementation that only puts JDK classes in ThreadLocals, so this is safe to be used from web applications.
-
Field Summary
Fields Modifier and Type Field Description private static intBITS_PER_INTprivate static LoggerLOGGERprivate static intMASKprivate static intRINGBUFFER_MIN_SIZEprivate static intRINGBUFFER_SIZEprivate static java.lang.ThreadLocal<Unbox.State>threadLocalStateprivate static Unbox.WebSafeStatewebSafeState
-
Constructor Summary
Constructors Modifier Constructor Description privateUnbox()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringBuilderbox(boolean value)Returns aStringBuildercontaining the text representation of the specified primitive value.static java.lang.StringBuilderbox(byte value)Returns aStringBuildercontaining the text representation of the specified primitive value.static java.lang.StringBuilderbox(char value)Returns aStringBuildercontaining the text representation of the specified primitive value.static java.lang.StringBuilderbox(double value)Returns aStringBuildercontaining the text representation of the specified primitive value.static java.lang.StringBuilderbox(float value)Returns aStringBuildercontaining the text representation of the specified primitive value.static java.lang.StringBuilderbox(int value)Returns aStringBuildercontaining the text representation of the specified primitive value.static java.lang.StringBuilderbox(long value)Returns aStringBuildercontaining the text representation of the specified primitive value.static java.lang.StringBuilderbox(short value)Returns aStringBuildercontaining the text representation of the specified primitive value.private static intcalculateRingBufferSize(java.lang.String propertyName)private static intceilingNextPowerOfTwo(int x)Calculate the next power of 2, greater than or equal to x.(package private) static intgetRingbufferSize()For testing.private static java.lang.StringBuildergetSB()private static Unbox.StategetState()
-
-
-
Field Detail
-
LOGGER
private static final Logger LOGGER
-
BITS_PER_INT
private static final int BITS_PER_INT
- See Also:
- Constant Field Values
-
RINGBUFFER_MIN_SIZE
private static final int RINGBUFFER_MIN_SIZE
- See Also:
- Constant Field Values
-
RINGBUFFER_SIZE
private static final int RINGBUFFER_SIZE
-
MASK
private static final int MASK
-
threadLocalState
private static java.lang.ThreadLocal<Unbox.State> threadLocalState
-
webSafeState
private static Unbox.WebSafeState webSafeState
-
-
Method Detail
-
calculateRingBufferSize
private static int calculateRingBufferSize(java.lang.String propertyName)
-
ceilingNextPowerOfTwo
private static int ceilingNextPowerOfTwo(int x)
Calculate the next power of 2, greater than or equal to x.From Hacker's Delight, Chapter 3, Harry S. Warren Jr.
- Parameters:
x- Value to round up- Returns:
- The next power of 2 from x inclusive
-
box
public static java.lang.StringBuilder box(float value)
Returns aStringBuildercontaining 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
StringBuildercontaining the text representation of the specified primitive value
-
box
public static java.lang.StringBuilder box(double value)
Returns aStringBuildercontaining 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
StringBuildercontaining the text representation of the specified primitive value
-
box
public static java.lang.StringBuilder box(short value)
Returns aStringBuildercontaining 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
StringBuildercontaining the text representation of the specified primitive value
-
box
public static java.lang.StringBuilder box(int value)
Returns aStringBuildercontaining 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
StringBuildercontaining the text representation of the specified primitive value
-
box
public static java.lang.StringBuilder box(char value)
Returns aStringBuildercontaining 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
StringBuildercontaining the text representation of the specified primitive value
-
box
public static java.lang.StringBuilder box(long value)
Returns aStringBuildercontaining 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
StringBuildercontaining the text representation of the specified primitive value
-
box
public static java.lang.StringBuilder box(byte value)
Returns aStringBuildercontaining 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
StringBuildercontaining the text representation of the specified primitive value
-
box
public static java.lang.StringBuilder box(boolean value)
Returns aStringBuildercontaining 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
StringBuildercontaining the text representation of the specified primitive value
-
getState
private static Unbox.State getState()
-
getSB
private static java.lang.StringBuilder getSB()
-
getRingbufferSize
static int getRingbufferSize()
For testing.
-
-