whitespace tab to 4spaces formating only change
Signed-off-by: Kai Boernert <kai-boernert@visiongamestudios.de>
This commit is contained in:
parent
810a4c3350
commit
846232063c
@ -1,14 +1,12 @@
|
|||||||
package com.jme3.util;
|
package com.jme3.util;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.nio.Buffer;
|
import java.nio.Buffer;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.DoubleBuffer;
|
|
||||||
|
|
||||||
public interface BufferAllocator {
|
public interface BufferAllocator {
|
||||||
|
|
||||||
void destroyDirectBuffer(Buffer toBeDestroyed);
|
void destroyDirectBuffer(Buffer toBeDestroyed);
|
||||||
|
|
||||||
ByteBuffer allocate(int size);
|
ByteBuffer allocate(int size);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1347,6 +1347,6 @@ public final class BufferUtils {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,95 +35,96 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.nio.Buffer;
|
import java.nio.Buffer;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.DoubleBuffer;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class contains the reflection based way to remove DirectByteBuffers in java < 9,
|
* This class contains the reflection based way to remove DirectByteBuffers in
|
||||||
* allocation is done via ByteBuffer.allocateDirect
|
* java < 9, allocation is done via ByteBuffer.allocateDirect
|
||||||
*/
|
*/
|
||||||
public final class ReflectionAllocator implements BufferAllocator {
|
public final class ReflectionAllocator implements BufferAllocator {
|
||||||
private static Method cleanerMethod = null;
|
private static Method cleanerMethod = null;
|
||||||
private static Method cleanMethod = null;
|
private static Method cleanMethod = null;
|
||||||
private static Method viewedBufferMethod = null;
|
private static Method viewedBufferMethod = null;
|
||||||
private static Method freeMethod = null;
|
private static Method freeMethod = null;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// Oracle JRE / OpenJDK
|
// Oracle JRE / OpenJDK
|
||||||
cleanerMethod = loadMethod("sun.nio.ch.DirectBuffer", "cleaner");
|
cleanerMethod = loadMethod("sun.nio.ch.DirectBuffer", "cleaner");
|
||||||
cleanMethod = loadMethod("sun.misc.Cleaner", "clean");
|
cleanMethod = loadMethod("sun.misc.Cleaner", "clean");
|
||||||
viewedBufferMethod = loadMethod("sun.nio.ch.DirectBuffer", "viewedBuffer");
|
viewedBufferMethod = loadMethod("sun.nio.ch.DirectBuffer", "viewedBuffer");
|
||||||
if (viewedBufferMethod == null) {
|
if (viewedBufferMethod == null) {
|
||||||
// They changed the name in Java 7 (???)
|
// They changed the name in Java 7 (???)
|
||||||
viewedBufferMethod = loadMethod("sun.nio.ch.DirectBuffer", "attachment");
|
viewedBufferMethod = loadMethod("sun.nio.ch.DirectBuffer", "attachment");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apache Harmony (allocated directly, to not trigger allocator used logic in BufferUtils)
|
// Apache Harmony (allocated directly, to not trigger allocator used
|
||||||
ByteBuffer bb = ByteBuffer.allocateDirect(1);
|
// logic in BufferUtils)
|
||||||
Class<?> clazz = bb.getClass();
|
ByteBuffer bb = ByteBuffer.allocateDirect(1);
|
||||||
try {
|
Class<?> clazz = bb.getClass();
|
||||||
freeMethod = clazz.getMethod("free");
|
try {
|
||||||
} catch (NoSuchMethodException ex) {
|
freeMethod = clazz.getMethod("free");
|
||||||
} catch (SecurityException ex) {
|
} catch (NoSuchMethodException ex) {
|
||||||
}
|
} catch (SecurityException ex) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static Method loadMethod(String className, String methodName) {
|
private static Method loadMethod(String className, String methodName) {
|
||||||
try {
|
try {
|
||||||
Method method = Class.forName(className).getMethod(methodName);
|
Method method = Class.forName(className).getMethod(methodName);
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
return method;
|
return method;
|
||||||
} catch (NoSuchMethodException ex) {
|
} catch (NoSuchMethodException ex) {
|
||||||
return null; // the method was not found
|
return null; // the method was not found
|
||||||
} catch (SecurityException ex) {
|
} catch (SecurityException ex) {
|
||||||
return null; // setAccessible not allowed by security policy
|
return null; // setAccessible not allowed by security policy
|
||||||
} catch (ClassNotFoundException ex) {
|
} catch (ClassNotFoundException ex) {
|
||||||
return null; // the direct buffer implementation was not found
|
return null; // the direct buffer implementation was not found
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
/**
|
/**
|
||||||
* This function explicitly calls the Cleaner method of a direct buffer.
|
* This function explicitly calls the Cleaner method of a direct buffer.
|
||||||
*
|
*
|
||||||
* @param toBeDestroyed
|
* @param toBeDestroyed
|
||||||
* The direct buffer that will be "cleaned". Utilizes reflection.
|
* The direct buffer that will be "cleaned". Utilizes reflection.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void destroyDirectBuffer(Buffer toBeDestroyed) {
|
public void destroyDirectBuffer(Buffer toBeDestroyed) {
|
||||||
try {
|
try {
|
||||||
if (freeMethod != null) {
|
if (freeMethod != null) {
|
||||||
freeMethod.invoke(toBeDestroyed);
|
freeMethod.invoke(toBeDestroyed);
|
||||||
} else {
|
} else {
|
||||||
Object cleaner = cleanerMethod.invoke(toBeDestroyed);
|
Object cleaner = cleanerMethod.invoke(toBeDestroyed);
|
||||||
if (cleaner != null) {
|
if (cleaner != null) {
|
||||||
cleanMethod.invoke(cleaner);
|
cleanMethod.invoke(cleaner);
|
||||||
} else {
|
} else {
|
||||||
// Try the alternate approach of getting the viewed buffer
|
// Try the alternate approach of getting the viewed buffer
|
||||||
// first
|
// first
|
||||||
Object viewedBuffer = viewedBufferMethod.invoke(toBeDestroyed);
|
Object viewedBuffer = viewedBufferMethod.invoke(toBeDestroyed);
|
||||||
if (viewedBuffer != null) {
|
if (viewedBuffer != null) {
|
||||||
destroyDirectBuffer((Buffer) viewedBuffer);
|
destroyDirectBuffer((Buffer) viewedBuffer);
|
||||||
} else {
|
} else {
|
||||||
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "Buffer cannot be destroyed: {0}", toBeDestroyed);
|
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE,
|
||||||
}
|
"Buffer cannot be destroyed: {0}", toBeDestroyed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IllegalAccessException ex) {
|
}
|
||||||
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "{0}", ex);
|
} catch (IllegalAccessException ex) {
|
||||||
} catch (IllegalArgumentException ex) {
|
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "{0}", ex);
|
||||||
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "{0}", ex);
|
} catch (IllegalArgumentException ex) {
|
||||||
} catch (InvocationTargetException ex) {
|
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "{0}", ex);
|
||||||
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "{0}", ex);
|
} catch (InvocationTargetException ex) {
|
||||||
} catch (SecurityException ex) {
|
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "{0}", ex);
|
||||||
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "{0}", ex);
|
} catch (SecurityException ex) {
|
||||||
}
|
Logger.getLogger(BufferUtils.class.getName()).log(Level.SEVERE, "{0}", ex);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuffer allocate(int size) {
|
public ByteBuffer allocate(int size) {
|
||||||
return ByteBuffer.allocateDirect(size);
|
return ByteBuffer.allocateDirect(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user