Top Description Inners Fields Constructors Methods
org.apache.logging.log4j.util

public Class Unbox

extends Object
Class Inheritance
Annotations
@PerformanceSensitive:allocation
Imports
org.apache.logging.log4j.Logger, org.apache.logging.log4j.status.StatusLogger

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
Unbox.WebSafeState

State implementation that only puts JDK classes in ThreadLocals, so this is safe to be used from web applications.

Field Summary

Modifier and TypeField and Description
private static final int
private static final Logger
private static final int
private static final int
private static final int
private static ThreadLocal<Unbox.State>
private static Unbox.WebSafeState

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static StringBuilder

Returns:

a StringBuilder containing the text representation of the specified primitive value
box
(final float
the value whose text representation to return
value
)

Returns a StringBuilder containing the text representation of the specified primitive value.

public static StringBuilder

Returns:

a StringBuilder containing the text representation of the specified primitive value
box
(final double
the value whose text representation to return
value
)

Returns a StringBuilder containing the text representation of the specified primitive value.

public static StringBuilder

Returns:

a StringBuilder containing the text representation of the specified primitive value
box
(final short
the value whose text representation to return
value
)

Returns a StringBuilder containing the text representation of the specified primitive value.

public static StringBuilder

Returns:

a StringBuilder containing the text representation of the specified primitive value
box
(final int
the value whose text representation to return
value
)

Returns a StringBuilder containing the text representation of the specified primitive value.

public static StringBuilder

Returns:

a StringBuilder containing the text representation of the specified primitive value
box
(final char
the value whose text representation to return
value
)

Returns a StringBuilder containing the text representation of the specified primitive value.

public static StringBuilder

Returns:

a StringBuilder containing the text representation of the specified primitive value
box
(final long
the value whose text representation to return
value
)

Returns a StringBuilder containing the text representation of the specified primitive value.

public static StringBuilder

Returns:

a StringBuilder containing the text representation of the specified primitive value
box
(final byte
the value whose text representation to return
value
)

Returns a StringBuilder containing the text representation of the specified primitive value.

public static StringBuilder

Returns:

a StringBuilder containing the text representation of the specified primitive value
box
(final boolean
the value whose text representation to return
value
)

Returns a StringBuilder containing the text representation of the specified primitive value.

private static int
calculateRingBufferSize(final String propertyName)

private static int

Returns:

The next power of 2 from x inclusive
ceilingNextPowerOfTwo
(final int
Value to round up
x
)

Calculate the next power of 2, greater than or equal to x.

pack-priv static int
getRingbufferSize()

For testing.

private static StringBuilder
private static Unbox.State
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BITS_PER_INTback to summary
private static final int BITS_PER_INT
LOGGERback to summary
private static final Logger LOGGER
MASKback to summary
private static final int MASK
RINGBUFFER_MIN_SIZEback to summary
private static final int RINGBUFFER_MIN_SIZE
RINGBUFFER_SIZEback to summary
private static final int RINGBUFFER_SIZE
threadLocalStateback to summary
private static ThreadLocal<Unbox.State> threadLocalState
webSafeStateback to summary
private static Unbox.WebSafeState webSafeState

Constructor Detail

Unboxback to summary
private Unbox()

Method Detail

boxback to summary
public static StringBuilder box(final float value)

Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.

Parameters
value:float

the value whose text representation to return

Returns:StringBuilder

a StringBuilder containing the text representation of the specified primitive value

Annotations
@PerformanceSensitive:allocation
boxback to summary
public static StringBuilder box(final double value)

Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.

Parameters
value:double

the value whose text representation to return

Returns:StringBuilder

a StringBuilder containing the text representation of the specified primitive value

Annotations
@PerformanceSensitive:allocation
boxback to summary
public static StringBuilder box(final short value)

Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.

Parameters
value:short

the value whose text representation to return

Returns:StringBuilder

a StringBuilder containing the text representation of the specified primitive value

Annotations
@PerformanceSensitive:allocation
boxback to summary
public static StringBuilder box(final int value)

Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.

Parameters
value:int

the value whose text representation to return

Returns:StringBuilder

a StringBuilder containing the text representation of the specified primitive value

Annotations
@PerformanceSensitive:allocation
boxback to summary
public static StringBuilder box(final char value)

Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.

Parameters
value:char

the value whose text representation to return

Returns:StringBuilder

a StringBuilder containing the text representation of the specified primitive value

Annotations
@PerformanceSensitive:allocation
boxback to summary
public static StringBuilder box(final long value)

Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.

Parameters
value:long

the value whose text representation to return

Returns:StringBuilder

a StringBuilder containing the text representation of the specified primitive value

Annotations
@PerformanceSensitive:allocation
boxback to summary
public static StringBuilder box(final byte value)

Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.

Parameters
value:byte

the value whose text representation to return

Returns:StringBuilder

a StringBuilder containing the text representation of the specified primitive value

Annotations
@PerformanceSensitive:allocation
boxback to summary
public static StringBuilder box(final boolean value)

Returns a StringBuilder containing the text representation of the specified primitive value. This method will not allocate temporary objects.

Parameters
value:boolean

the value whose text representation to return

Returns:StringBuilder

a StringBuilder containing the text representation of the specified primitive value

Annotations
@PerformanceSensitive:allocation
calculateRingBufferSizeback to summary
private static int calculateRingBufferSize(final String propertyName)
ceilingNextPowerOfTwoback to summary
private static int ceilingNextPowerOfTwo(final 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:int

Value to round up

Returns:int

The next power of 2 from x inclusive

getRingbufferSizeback to summary
pack-priv static int getRingbufferSize()

For testing.

getSBback to summary
private static StringBuilder getSB()
getStateback to summary
private static Unbox.State getState()
org.apache.logging.log4j.util back to summary

private Class Unbox.State

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int
private final StringBuilder[]

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public StringBuilder
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentback to summary
private int current
ringBufferback to summary
private final StringBuilder[] ringBuffer

Constructor Detail

Stateback to summary
pack-priv State()

Method Detail

getStringBuilderback to summary
public StringBuilder getStringBuilder()
org.apache.logging.log4j.util back to summary

private Class Unbox.WebSafeState

extends Object
Class Inheritance
Annotations
@SuppressWarnings:ThreadLocalUsage

State implementation that only puts JDK classes in ThreadLocals, so this is safe to be used from web applications. Web application containers have thread pools that may hold on to ThreadLocal objects after the application was stopped. This may prevent the classes of the application from being unloaded, causing memory leaks.

Such memory leaks will not occur if only JDK classes are stored in ThreadLocals.

Field Summary

Modifier and TypeField and Description
private final ThreadLocal<int[]>
private final ThreadLocal<StringBuilder[]>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public StringBuilder
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentback to summary
private final ThreadLocal<int[]> current
ringBufferback to summary
private final ThreadLocal<StringBuilder[]> ringBuffer

Constructor Detail

WebSafeStateback to summary
private WebSafeState()

Method Detail

getStringBuilderback to summary
public StringBuilder getStringBuilder()