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

com.github.bloodshura.ignitium.ntv.util.Buffer Maven / Gradle / Ivy

The newest version!
package com.github.bloodshura.ignitium.ntv.util;

import com.github.bloodshura.ignitium.lang.Disposable;
import com.sun.jna.Memory;

import javax.annotation.Nonnull;

public class Buffer extends Memory implements Disposable {
	private static final ThreadLocal CACHED_SIZE1 = ThreadLocal.withInitial(() -> new Buffer(1));
	private static final ThreadLocal CACHED_SIZE2 = ThreadLocal.withInitial(() -> new Buffer(2));
	private static final ThreadLocal CACHED_SIZE4 = ThreadLocal.withInitial(() -> new Buffer(4));
	private static final ThreadLocal CACHED_SIZE8 = ThreadLocal.withInitial(() -> new Buffer(8));

	public Buffer(int size) {
		super(size);
	}

	// Overriding just for changing the visibility modifiers (from PROTECTED to PUBLIC).
	@Override
	public void dispose() {
		super.dispose();
	}

	@Nonnull
	public static Buffer common(int size) {
		if (size == 1) {
			return CACHED_SIZE1.get();
		}
		if (size == 2) {
			return CACHED_SIZE2.get();
		}
		if (size == 4) {
			return CACHED_SIZE4.get();
		}
		if (size == 8) {
			return CACHED_SIZE8.get();
		}

		return new Buffer(size);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy