mirror of
https://github.com/GTNewHorizons/lwjgl3ify.git
synced 2026-11-25 14:34:42 +00:00
Use MemoryUtil.memByteBuffer instead of copying
This commit is contained in:
1 parent
e1baafb935
commit
40fbeff618
1 file changed
+14
-50
@@ -1,82 +1,46 @@
|
||||
package me.eigenraven.lwjgl3ify;
|
||||
|
||||
import java.nio.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
public class BufferCasts {
|
||||
public static ByteBuffer toByteBuffer(CharBuffer buffer) {
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder());
|
||||
newBuffer.asCharBuffer().put(buffer);
|
||||
return newBuffer;
|
||||
return MemoryUtil.memByteBuffer(buffer);
|
||||
}
|
||||
|
||||
public static ByteBuffer toByteBuffer(ShortBuffer buffer) {
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder());
|
||||
newBuffer.asShortBuffer().put(buffer);
|
||||
return newBuffer;
|
||||
return MemoryUtil.memByteBuffer(buffer);
|
||||
}
|
||||
|
||||
public static ByteBuffer toByteBuffer(IntBuffer buffer) {
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder());
|
||||
newBuffer.asIntBuffer().put(buffer);
|
||||
return newBuffer;
|
||||
return MemoryUtil.memByteBuffer(buffer);
|
||||
}
|
||||
|
||||
public static ByteBuffer toByteBuffer(LongBuffer buffer) {
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder());
|
||||
newBuffer.asLongBuffer().put(buffer);
|
||||
return newBuffer;
|
||||
return MemoryUtil.memByteBuffer(buffer);
|
||||
}
|
||||
|
||||
public static ByteBuffer toByteBuffer(FloatBuffer buffer) {
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder());
|
||||
newBuffer.asFloatBuffer().put(buffer);
|
||||
return newBuffer;
|
||||
return MemoryUtil.memByteBuffer(buffer);
|
||||
}
|
||||
|
||||
public static ByteBuffer toByteBuffer(DoubleBuffer buffer) {
|
||||
ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder());
|
||||
newBuffer.asDoubleBuffer().put(buffer);
|
||||
return newBuffer;
|
||||
return MemoryUtil.memByteBuffer(buffer);
|
||||
}
|
||||
|
||||
public static void updateBuffer(CharBuffer destination, ByteBuffer source) {
|
||||
destination.clear();
|
||||
destination.put(source.asCharBuffer());
|
||||
}
|
||||
public static void updateBuffer(CharBuffer destination, ByteBuffer source) {}
|
||||
|
||||
public static void updateBuffer(ShortBuffer destination, ByteBuffer source) {
|
||||
destination.clear();
|
||||
destination.put(source.asShortBuffer());
|
||||
}
|
||||
public static void updateBuffer(ShortBuffer destination, ByteBuffer source) {}
|
||||
|
||||
public static void updateBuffer(IntBuffer destination, ByteBuffer source) {
|
||||
destination.clear();
|
||||
destination.put(source.asIntBuffer());
|
||||
}
|
||||
public static void updateBuffer(IntBuffer destination, ByteBuffer source) {}
|
||||
|
||||
public static void updateBuffer(LongBuffer destination, ByteBuffer source) {
|
||||
destination.clear();
|
||||
destination.put(source.asLongBuffer());
|
||||
}
|
||||
public static void updateBuffer(LongBuffer destination, ByteBuffer source) {}
|
||||
|
||||
public static void updateBuffer(FloatBuffer destination, ByteBuffer source) {
|
||||
destination.clear();
|
||||
destination.put(source.asFloatBuffer());
|
||||
}
|
||||
public static void updateBuffer(FloatBuffer destination, ByteBuffer source) {}
|
||||
|
||||
public static void updateBuffer(DoubleBuffer destination, ByteBuffer source) {
|
||||
destination.clear();
|
||||
destination.put(source.asDoubleBuffer());
|
||||
}
|
||||
public static void updateBuffer(DoubleBuffer destination, ByteBuffer source) {}
|
||||
|
||||
public static CharSequence bufferToCharSeq(ByteBuffer buffer) {
|
||||
if (buffer.hasArray()) {
|
||||
return new String(buffer.array(), StandardCharsets.UTF_8);
|
||||
} else {
|
||||
byte[] tmp = new byte[buffer.remaining()];
|
||||
buffer.get(tmp);
|
||||
return new String(tmp, StandardCharsets.UTF_8);
|
||||
}
|
||||
return MemoryUtil.memUTF8(buffer);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user