All Downloads are FREE. Search and download functionalities are using the official Maven repository.

biz.paluch.logging.gelf.intern.sender.GelfBuffers Maven / Gradle / Ivy

The newest version!
package biz.paluch.logging.gelf.intern.sender;

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;

import biz.paluch.logging.gelf.intern.GelfMessage;

/**
 * Utility to create TCP/UDP buffers from a {@link GelfMessage} by working with {@link ByteBuffer}. Buffers are provided by
 * {@link ThreadLocal} and enlarged if the buffer size is exceeded. Enlarged buffers are returned to their originating
 * {@link ThreadLocal}.
 *
 * @author Mark Paluch
 */
class GelfBuffers {

    private GelfBuffers() {
        // no instance allowed
    }

    /**
     * Create UDP buffers and apply auto-buffer-enlarging, if necessary.
     *
     * @param message
     * @param writeBuffers
     * @param tempBuffers
     * @return
     */
    protected static ByteBuffer[] toUDPBuffers(GelfMessage message, ThreadLocal writeBuffers,
            ThreadLocal tempBuffers) {

        while (true) {

            try {
                return message.toUDPBuffers(getBuffer(writeBuffers), getBuffer(tempBuffers));
            } catch (BufferOverflowException e) {
                enlargeBuffer(writeBuffers);
                enlargeBuffer(tempBuffers);
            }
        }
    }

    /**
     * Create TCP buffer and apply auto-buffer-enlarging, if necessary.
     *
     * @param message
     * @param writeBuffers
     * @return
     */
    protected static ByteBuffer toTCPBuffer(GelfMessage message, ThreadLocal writeBuffers) {

        while (true) {

            try {
                return message.toTCPBuffer(getBuffer(writeBuffers));
            } catch (BufferOverflowException e) {
                enlargeBuffer(writeBuffers);
            }
        }
    }

    private static void enlargeBuffer(ThreadLocal buffers) {

        ByteBuffer newBuffer = ByteBuffer.allocateDirect(calculateNewBufferSize(buffers.get().capacity()));
        buffers.set(newBuffer);
    }

    private static ByteBuffer getBuffer(ThreadLocal buffers) {
        return (ByteBuffer) buffers.get().clear();
    }

    private static int calculateNewBufferSize(int capacity) {
        return (int) (capacity * 1.5);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy