Class TextEncoderHelper
- java.lang.Object
-
- org.apache.logging.log4j.core.layout.TextEncoderHelper
-
public class TextEncoderHelper extends java.lang.Object
Helper class to encode text to binary data without allocating temporary objects.- Since:
- 2.6
-
-
Constructor Summary
Constructors Modifier Constructor Description private
TextEncoderHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description (package private) static int
copy(java.lang.StringBuilder source, int offset, java.nio.CharBuffer destination)
Copies characters from the StringBuilder into the CharBuffer, starting at the specified offset and ending when either all characters have been copied or when the CharBuffer is full.private static java.nio.ByteBuffer
drainIfByteBufferFull(ByteBufferDestination destination, java.nio.ByteBuffer temp, java.nio.charset.CoderResult result)
If the CoderResult indicates the ByteBuffer is full, synchronize on the destination and write the content of the ByteBuffer to the destination.private static java.nio.ByteBuffer
encodeAsMuchAsPossible(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, boolean endOfInput, ByteBufferDestination destination, java.nio.ByteBuffer temp)
private static void
encodeChunkedText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, java.nio.ByteBuffer byteBuf, java.lang.StringBuilder text, ByteBufferDestination destination)
This method is called before the CharEncoder has encoded any content from the CharBuffer into the ByteBuffer, but we have already detected that the CharBuffer contents is too large to fit into the ByteBuffer.static void
encodeText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, java.nio.ByteBuffer byteBuf, java.lang.StringBuilder text, ByteBufferDestination destination)
Converts the specified text to bytes and writes the resulting bytes to the specified destination.static void
encodeText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, ByteBufferDestination destination)
Deprecated.(package private) static void
encodeTextFallBack(java.nio.charset.Charset charset, java.lang.StringBuilder text, ByteBufferDestination destination)
private static void
flushRemainingBytes(java.nio.charset.CharsetEncoder charsetEncoder, ByteBufferDestination destination, java.nio.ByteBuffer temp)
private static void
throwException(java.nio.charset.CoderResult result)
private static java.nio.ByteBuffer
writeAndEncodeAsMuchAsPossible(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, boolean endOfInput, ByteBufferDestination destination, java.nio.ByteBuffer temp, java.nio.charset.CoderResult result)
Continues to write the contents of the ByteBuffer to the destination and encode more of the CharBuffer text into the ByteBuffer until the remaining encoded text fit into the ByteBuffer, at which point the ByteBuffer is returned (without flushing the CharEncoder).private static void
writeChunkedEncodedText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, ByteBufferDestination destination, java.nio.ByteBuffer byteBuf, java.nio.charset.CoderResult result)
This method is called when the CharEncoder has encoded (but not yet flushed) content from the CharBuffer into the ByteBuffer and we found that the ByteBuffer is too small to hold all the content.private static void
writeEncodedText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, java.nio.ByteBuffer byteBuf, ByteBufferDestination destination, java.nio.charset.CoderResult result)
This method is called when the CharEncoder has encoded (but not yet flushed) content from the CharBuffer into the ByteBuffer.
-
-
-
Method Detail
-
encodeTextFallBack
static void encodeTextFallBack(java.nio.charset.Charset charset, java.lang.StringBuilder text, ByteBufferDestination destination)
-
encodeText
public static void encodeText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, java.nio.ByteBuffer byteBuf, java.lang.StringBuilder text, ByteBufferDestination destination)
Converts the specified text to bytes and writes the resulting bytes to the specified destination. Attempts to postpone synchronizing on the destination as long as possible to minimize lock contention.- Parameters:
charsetEncoder
- thread-local encoder instance for converting chars to bytescharBuf
- thread-local text buffer for converting text to bytesbyteBuf
- thread-local buffer to temporarily hold converted bytes before copying them to the destinationtext
- the text to convert and write to the destinationdestination
- the destination to write the bytes to
-
writeEncodedText
private static void writeEncodedText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, java.nio.ByteBuffer byteBuf, ByteBufferDestination destination, java.nio.charset.CoderResult result)
This method is called when the CharEncoder has encoded (but not yet flushed) content from the CharBuffer into the ByteBuffer. A CoderResult of UNDERFLOW means that the contents fit into the ByteBuffer and we can move on to the next step, flushing. Otherwise, we need to synchronize on the destination, copy the ByteBuffer to the destination and encode the remainder of the CharBuffer while holding the lock on the destination.- Since:
- 2.9
-
writeChunkedEncodedText
private static void writeChunkedEncodedText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, ByteBufferDestination destination, java.nio.ByteBuffer byteBuf, java.nio.charset.CoderResult result)
This method is called when the CharEncoder has encoded (but not yet flushed) content from the CharBuffer into the ByteBuffer and we found that the ByteBuffer is too small to hold all the content. Therefore, we need to synchronize on the destination, copy the ByteBuffer to the destination and encode the remainder of the CharBuffer while holding the lock on the destination.- Since:
- 2.9
-
encodeChunkedText
private static void encodeChunkedText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, java.nio.ByteBuffer byteBuf, java.lang.StringBuilder text, ByteBufferDestination destination)
This method is called before the CharEncoder has encoded any content from the CharBuffer into the ByteBuffer, but we have already detected that the CharBuffer contents is too large to fit into the ByteBuffer. Therefore, at some point we need to synchronize on the destination, copy the ByteBuffer to the destination and encode the remainder of the CharBuffer while holding the lock on the destination.- Since:
- 2.9
-
encodeText
@Deprecated public static void encodeText(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, ByteBufferDestination destination)
Deprecated.For testing purposes only.
-
writeAndEncodeAsMuchAsPossible
private static java.nio.ByteBuffer writeAndEncodeAsMuchAsPossible(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, boolean endOfInput, ByteBufferDestination destination, java.nio.ByteBuffer temp, java.nio.charset.CoderResult result)
Continues to write the contents of the ByteBuffer to the destination and encode more of the CharBuffer text into the ByteBuffer until the remaining encoded text fit into the ByteBuffer, at which point the ByteBuffer is returned (without flushing the CharEncoder).This method is called when the CharEncoder has encoded (but not yet flushed) content from the CharBuffer into the ByteBuffer and we found that the ByteBuffer is too small to hold all the content.
Thread-safety note: This method should be called while synchronizing on the ByteBufferDestination.
- Returns:
- the ByteBuffer resulting from draining the temporary ByteBuffer to the destination. In the case of a MemoryMappedFile, a remap() may have taken place and the returned ByteBuffer is now the MappedBuffer of the newly mapped region of the memory mapped file.
- Since:
- 2.9
-
throwException
private static void throwException(java.nio.charset.CoderResult result)
-
encodeAsMuchAsPossible
private static java.nio.ByteBuffer encodeAsMuchAsPossible(java.nio.charset.CharsetEncoder charsetEncoder, java.nio.CharBuffer charBuf, boolean endOfInput, ByteBufferDestination destination, java.nio.ByteBuffer temp)
-
drainIfByteBufferFull
private static java.nio.ByteBuffer drainIfByteBufferFull(ByteBufferDestination destination, java.nio.ByteBuffer temp, java.nio.charset.CoderResult result)
If the CoderResult indicates the ByteBuffer is full, synchronize on the destination and write the content of the ByteBuffer to the destination. If the specified ByteBuffer is owned by the destination, we have reached the end of a MappedBuffer and we call drain() on the destination to remap().If the CoderResult indicates more can be encoded, this method does nothing and returns the temp ByteBuffer.
- Parameters:
destination
- the destination to write bytes totemp
- the ByteBuffer containing the encoded bytes. May be a temporary buffer or may be the ByteBuffer of the ByteBufferDestinationresult
- the CoderResult from the CharsetEncoder- Returns:
- the ByteBuffer to encode into for the remainder of the text
-
flushRemainingBytes
private static void flushRemainingBytes(java.nio.charset.CharsetEncoder charsetEncoder, ByteBufferDestination destination, java.nio.ByteBuffer temp)
-
copy
static int copy(java.lang.StringBuilder source, int offset, java.nio.CharBuffer destination)
Copies characters from the StringBuilder into the CharBuffer, starting at the specified offset and ending when either all characters have been copied or when the CharBuffer is full.- Returns:
- the number of characters that were copied
-
-