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

Alachisoft.NCache.Common.Util.BufferPool Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Util;

import Alachisoft.NCache.Common.Enum.EventType;
import Alachisoft.NCache.Common.Logger.EventLogger;

public class BufferPool {
    private static final int DEFAULT_BUFFER_SIZE = 512 * 1024;
    private static final int DEFAULT_POOL_SIZE = 40;

    private static int _bufferSize = DEFAULT_BUFFER_SIZE;
    private static int _poolSize = DEFAULT_POOL_SIZE;
    private static java.util.LinkedList _pool = new java.util.LinkedList();
    private static Object _sync_lock = new Object();

    static {
        //Mansoor: Should be replaced with getting files with properties.
        String temp = "";//System.Configuration.ConfigurationSettings.AppSettings["NCacheServer.LOHPoolBufferSize"];

        if (!tangible.DotNetToJavaStringHelper.isNullOrEmpty(temp)) {
            int bufferSize = DEFAULT_BUFFER_SIZE;
            tangible.RefObject tempRef_bufferSize = new tangible.RefObject(bufferSize);
            boolean tempVar = false;
            try {
                Integer.parseInt(temp);
                tempVar = true;
            } catch (NumberFormatException ex) {
            }
            //boolean tempVar = Integer.TryParse(temp, tempRef_bufferSize);
            bufferSize = tempRef_bufferSize.argvalue;
            if (tempVar) {
                _bufferSize = bufferSize;
            }
        }
        //Mansoor: Should be replaced with getting files with properties.
        temp = "";//System.Configuration.ConfigurationSettings.AppSettings["NCacheServer.LOHPoolSize"];
        if (!tangible.DotNetToJavaStringHelper.isNullOrEmpty(temp)) {
            int poolSize = DEFAULT_POOL_SIZE;
            tangible.RefObject tempRef_poolSize = new tangible.RefObject(poolSize);
            boolean tempVar2 = false;
            try {
                Integer.parseInt(temp);
                tempVar2 = true;
            } catch (NumberFormatException ex) {
            }
            //boolean tempVar2 = Integer.TryParse(temp, tempRef_poolSize);
            poolSize = tempRef_poolSize.argvalue;
            if (tempVar2) {
                _poolSize = poolSize;
            }
        }
        AllocateNewBuffers();
    }

    private static void AllocateNewBuffers() {
        synchronized (_sync_lock) {
            java.util.ArrayList newBuffers = new java.util.ArrayList();
            for (int i = 1; i <= _poolSize; i++) {
                try {
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] buffer = new byte[_bufferSize];
                    byte[] buffer = new byte[_bufferSize];
                    _pool.offer(buffer);
                } catch (OutOfMemoryError e) {
                    //Mansoor: log event using logger
                    EventLogger.LogEvent("BufferPool can't allocate new buffer", EventType.ERROR);
                } catch (RuntimeException e2) {

                }
            }
        }
    }

    /**
     * Gets a larg buffer from the pool.
     *
     * @return
     */
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static byte[] CheckoutBuffer(int size)
    public static byte[] CheckoutBuffer(int size) {
        if (size != -1 && size > _bufferSize) {
            return new byte[size];
        }

        synchronized (_sync_lock) {
            if (_pool.isEmpty()) {
                AllocateNewBuffers();
            }

            if (_pool.size() > 0) {
                Object tempVar = _pool.poll();
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: {
                return (byte[]) ((tempVar instanceof byte[]) ? tempVar : null);
            } else {
                return new byte[_bufferSize];
            }
        }
    }

    /**
     * Frees a buffer allocated from the pool.
     *
     * @param buffer
     */
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static void CheckinBuffer(byte[] buffer)
    public static void CheckinBuffer(byte[] buffer) {
        if (buffer == null) {
            return;
        }
        if (buffer.length > _bufferSize) { //This is not a pool buffer.
            return;
        }
        synchronized (_sync_lock) {
            _pool.offer(buffer);
        }
    }

    /**
     * Releases all the buffers.
     */
    public static void Clear() {
        synchronized (_sync_lock) {
            _pool.clear();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy