From 699a36d1e92e96568abfaac38784bafad3dd452c Mon Sep 17 00:00:00 2001 From: Raven Szewczyk Date: Sun, 15 Jan 2023 09:06:26 +0000 Subject: [PATCH] Convert some of the lwjgl2 copies into lwjgl3 calls --- log.txt | 52 +- .../me/eigenraven/lwjgl3ify/BufferCasts.java | 12 +- src/main/java/org/lwjglx/BufferUtils.java | 9 - src/main/java/org/lwjglx/LWJGLUtil.java | 67 +- src/main/java/org/lwjglx/MemoryUtil.java | 303 +----- src/main/java/org/lwjglx/PointerBuffer.java | 893 +----------------- src/main/java/org/lwjglx/PointerWrapper.java | 9 +- src/main/java/org/lwjglx/Sys.java | 12 +- 8 files changed, 161 insertions(+), 1196 deletions(-) diff --git a/log.txt b/log.txt index 3b2c14f..e44f20c 100644 --- a/log.txt +++ b/log.txt @@ -2,7 +2,7 @@ > Configure project : Configuring spotless -> Task :compileUtilJava +> Task :compileUtilJava UP-TO-DATE > Task :compileUtilScala NO-SOURCE > Task :injectTags UP-TO-DATE > Task :compileInjectedTagsJava UP-TO-DATE @@ -39,12 +39,52 @@ Configuring spotless > Task :classes UP-TO-DATE > Task :jar UP-TO-DATE > Task :processUtilResources NO-SOURCE -> Task :utilClasses +> Task :utilClasses UP-TO-DATE > Task :runComparisonTool -Missing in mod: org/lwjgl/LWJGLUtil : mapLibraryName:(Ljava/lang/String;)Ljava/lang/String; -Missing in mod: org/lwjgl/Sys : getClipboard:()Ljava/lang/String; -Missing in mod: org/lwjgl/Sys : is64Bit:()Z +Missing in mod: org/lwjgl/PointerBuffer : :(I)V +Missing in mod: org/lwjgl/PointerBuffer : :(Ljava/nio/ByteBuffer;)V +Missing in mod: org/lwjgl/PointerBuffer : asReadOnlyBuffer:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : capacity:()I +Missing in mod: org/lwjgl/PointerBuffer : clear:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : compact:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : compareTo:(Ljava/lang/Object;)I +Missing in mod: org/lwjgl/PointerBuffer : duplicate:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : equals:(Ljava/lang/Object;)Z +Missing in mod: org/lwjgl/PointerBuffer : flip:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : get:()J +Missing in mod: org/lwjgl/PointerBuffer : get:(I)J +Missing in mod: org/lwjgl/PointerBuffer : get:([J)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : get:([JII)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : getBuffer:()Ljava/nio/ByteBuffer; +Missing in mod: org/lwjgl/PointerBuffer : getPointerSize:()I +Missing in mod: org/lwjgl/PointerBuffer : hasRemaining:()Z +Missing in mod: org/lwjgl/PointerBuffer : hashCode:()I +Missing in mod: org/lwjgl/PointerBuffer : is64Bit:()Z +Missing in mod: org/lwjgl/PointerBuffer : isReadOnly:()Z +Missing in mod: org/lwjgl/PointerBuffer : limit:()I +Missing in mod: org/lwjgl/PointerBuffer : limit:(I)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : mark:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : newInstance:(Ljava/nio/ByteBuffer;)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : order:()Ljava/nio/ByteOrder; +Missing in mod: org/lwjgl/PointerBuffer : position:()I +Missing in mod: org/lwjgl/PointerBuffer : position:(I)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : positionByte:()I +Missing in mod: org/lwjgl/PointerBuffer : put:(IJ)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : put:(ILorg/lwjgl/PointerWrapper;)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : put:(J)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : put:(Ljava/nio/ByteBuffer;IJ)V +Missing in mod: org/lwjgl/PointerBuffer : put:(Ljava/nio/ByteBuffer;J)V +Missing in mod: org/lwjgl/PointerBuffer : put:(Lorg/lwjgl/PointerBuffer;)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : put:(Lorg/lwjgl/PointerWrapper;)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : put:([J)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : put:([JII)Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : remaining:()I +Missing in mod: org/lwjgl/PointerBuffer : remainingByte:()I +Missing in mod: org/lwjgl/PointerBuffer : reset:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : rewind:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : slice:()Lorg/lwjgl/PointerBuffer; +Missing in mod: org/lwjgl/PointerBuffer : toString:()Ljava/lang/String; Missing gl3: org/lwjgl/input/Controller Missing gl3: org/lwjgl/input/Controllers Missing in mod: org/lwjgl/input/Keyboard : areRepeatEventsEnabled:()Z @@ -3566,4 +3606,4 @@ Missing in mod: org/lwjgl/util/vector/Vector3f : equals:(Ljava/lang/Object;)Z Missing in mod: org/lwjgl/util/vector/Vector4f : equals:(Ljava/lang/Object;)Z BUILD SUCCESSFUL in 1s -21 actionable tasks: 2 executed, 19 up-to-date +21 actionable tasks: 1 executed, 20 up-to-date diff --git a/src/main/java/me/eigenraven/lwjgl3ify/BufferCasts.java b/src/main/java/me/eigenraven/lwjgl3ify/BufferCasts.java index fedd089..a1f3f78 100644 --- a/src/main/java/me/eigenraven/lwjgl3ify/BufferCasts.java +++ b/src/main/java/me/eigenraven/lwjgl3ify/BufferCasts.java @@ -5,37 +5,37 @@ import java.nio.charset.StandardCharsets; public class BufferCasts { public static ByteBuffer toByteBuffer(CharBuffer buffer) { - ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4); + ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder()); newBuffer.asCharBuffer().put(buffer); return newBuffer; } public static ByteBuffer toByteBuffer(ShortBuffer buffer) { - ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4); + ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder()); newBuffer.asShortBuffer().put(buffer); return newBuffer; } public static ByteBuffer toByteBuffer(IntBuffer buffer) { - ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4); + ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder()); newBuffer.asIntBuffer().put(buffer); return newBuffer; } public static ByteBuffer toByteBuffer(LongBuffer buffer) { - ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4); + ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder()); newBuffer.asLongBuffer().put(buffer); return newBuffer; } public static ByteBuffer toByteBuffer(FloatBuffer buffer) { - ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4); + ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder()); newBuffer.asFloatBuffer().put(buffer); return newBuffer; } public static ByteBuffer toByteBuffer(DoubleBuffer buffer) { - ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4); + ByteBuffer newBuffer = ByteBuffer.allocateDirect(buffer.capacity() * 4).order(ByteOrder.nativeOrder()); newBuffer.asDoubleBuffer().put(buffer); return newBuffer; } diff --git a/src/main/java/org/lwjglx/BufferUtils.java b/src/main/java/org/lwjglx/BufferUtils.java index 754f40b..032763d 100644 --- a/src/main/java/org/lwjglx/BufferUtils.java +++ b/src/main/java/org/lwjglx/BufferUtils.java @@ -186,13 +186,4 @@ public final class BufferUtils { /** Fill buffer with zeros from position to remaining */ private static native void zeroBuffer0(Buffer b, long off, long size); - - /** - * Returns the memory address of the specified buffer. - * - * @param buffer the buffer - * - * @return the memory address - */ - static native long getBufferAddress(Buffer buffer); } diff --git a/src/main/java/org/lwjglx/LWJGLUtil.java b/src/main/java/org/lwjglx/LWJGLUtil.java index e1b534f..dc47ccb 100644 --- a/src/main/java/org/lwjglx/LWJGLUtil.java +++ b/src/main/java/org/lwjglx/LWJGLUtil.java @@ -41,6 +41,7 @@ import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.*; +import org.lwjgl.system.Platform; /** *

@@ -269,14 +270,19 @@ public class LWJGLUtil { private static final int PLATFORM; static { - final String osName = getPrivilegedProperty("os.name"); - if (osName.startsWith("Windows")) PLATFORM = PLATFORM_WINDOWS; - else if (osName.startsWith("Linux") - || osName.startsWith("FreeBSD") - || osName.startsWith("SunOS") - || osName.startsWith("Unix")) PLATFORM = PLATFORM_LINUX; - else if (osName.startsWith("Mac OS X") || osName.startsWith("Darwin")) PLATFORM = PLATFORM_MACOSX; - else throw new LinkageError("Unknown platform: " + osName); + switch (Platform.get()) { + case WINDOWS: + PLATFORM = PLATFORM_WINDOWS; + break; + case LINUX: + PLATFORM = PLATFORM_LINUX; + break; + case MACOSX: + PLATFORM = PLATFORM_MACOSX; + break; + default: + throw new LinkageError("Unknown platform: " + Platform.get()); + } } private static ByteBuffer loadIcon(String data) { @@ -384,28 +390,8 @@ public class LWJGLUtil { return possible_paths.toArray(new String[possible_paths.size()]); } - static void execPrivileged(final String[] cmd_array) throws Exception { - try { - Process process = AccessController.doPrivileged(new PrivilegedExceptionAction() { - public Process run() throws Exception { - return Runtime.getRuntime().exec(cmd_array); - } - }); - // Close unused streams to make sure the child process won't hang - process.getInputStream().close(); - process.getOutputStream().close(); - process.getErrorStream().close(); - } catch (PrivilegedActionException e) { - throw (Exception) e.getCause(); - } - } - private static String getPrivilegedProperty(final String property_name) { - return AccessController.doPrivileged(new PrivilegedAction() { - public String run() { - return System.getProperty(property_name); - } - }); + return AccessController.doPrivileged((PrivilegedAction) () -> System.getProperty(property_name)); } /** @@ -449,11 +435,7 @@ public class LWJGLUtil { * Gets a boolean property as a privileged action. */ public static boolean getPrivilegedBoolean(final String property_name) { - return AccessController.doPrivileged(new PrivilegedAction() { - public Boolean run() { - return Boolean.getBoolean(property_name); - } - }); + return AccessController.doPrivileged((PrivilegedAction) () -> Boolean.getBoolean(property_name)); } /** @@ -464,11 +446,7 @@ public class LWJGLUtil { * @return the property value */ public static Integer getPrivilegedInteger(final String property_name) { - return AccessController.doPrivileged(new PrivilegedAction() { - public Integer run() { - return Integer.getInteger(property_name); - } - }); + return AccessController.doPrivileged((PrivilegedAction) () -> Integer.getInteger(property_name)); } /** @@ -480,11 +458,8 @@ public class LWJGLUtil { * @return the property value */ public static Integer getPrivilegedInteger(final String property_name, final int default_val) { - return AccessController.doPrivileged(new PrivilegedAction() { - public Integer run() { - return Integer.getInteger(property_name, default_val); - } - }); + return AccessController.doPrivileged( + (PrivilegedAction) () -> Integer.getInteger(property_name, default_val)); } /** @@ -605,4 +580,8 @@ public class LWJGLUtil { */ boolean accept(Field field, int value); } + + public static String mapLibraryName(String libName) { + return Platform.mapLibraryNameBundled(libName); + } } diff --git a/src/main/java/org/lwjglx/MemoryUtil.java b/src/main/java/org/lwjglx/MemoryUtil.java index 0b0ac58..111d87b 100644 --- a/src/main/java/org/lwjglx/MemoryUtil.java +++ b/src/main/java/org/lwjglx/MemoryUtil.java @@ -31,7 +31,6 @@ */ package org.lwjglx; -import java.lang.reflect.Field; import java.nio.*; import java.nio.charset.*; @@ -44,63 +43,6 @@ import java.nio.charset.*; */ public final class MemoryUtil { - private static final Charset ascii; - private static final Charset utf8; - private static final Charset utf16; - - static { - ascii = Charset.forName("ISO-8859-1"); - utf8 = Charset.forName("UTF-8"); - utf16 = Charset.forName("UTF-16LE"); - } - - private static final Accessor memUtil; - - static { - Accessor util; - try { - // Depends on java.nio.Buffer#address and sun.misc.Unsafe - util = loadAccessor("org.lwjgl.MemoryUtilSun$AccessorUnsafe"); - } catch (Exception e0) { - try { - // Depends on java.nio.Buffer#address and sun.reflect.FieldAccessor - util = loadAccessor("org.lwjgl.MemoryUtilSun$AccessorReflectFast"); - } catch (Exception e1) { - try { - // Depends on java.nio.Buffer#address - util = new AccessorReflect(); - } catch (Exception e2) { - LWJGLUtil.log( - "Unsupported JVM detected, this will likely result in low performance. Please inform LWJGL developers."); - util = new AccessorJNI(); - } - } - } - - LWJGLUtil.log("MemoryUtil Accessor: " + util.getClass().getSimpleName()); - memUtil = util; - - /* - BENCHMARK RESULTS - Oracle Server VM: - - Unsafe: 4ns - ReflectFast: 8ns - Reflect: 10ns - JNI: 82ns - - BENCHMARK RESULTS - Oracle Client VM: - - Unsafe: 5ns - ReflectFast: 81ns - Reflect: 85ns - JNI: 87ns - - On non-Oracle VMs, Unsafe should be the fastest implementation as well. In the absence - of Unsafe, performance will depend on how reflection and JNI are implemented. For now - we'll go with what we see on the Oracle VM (that is, we'll prefer reflection over JNI). - */ - } - private MemoryUtil() {} /** @@ -111,91 +53,91 @@ public final class MemoryUtil { * @return the memory address */ public static long getAddress0(Buffer buffer) { - return memUtil.getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress0Safe(Buffer buffer) { - return buffer == null ? 0L : memUtil.getAddress(buffer); + return buffer == null ? 0 : org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress0(PointerBuffer buffer) { - return memUtil.getAddress(buffer.getBuffer()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress0Safe(PointerBuffer buffer) { - return buffer == null ? 0L : memUtil.getAddress(buffer.getBuffer()); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } // --- [ API utilities ] --- public static long getAddress(ByteBuffer buffer) { - return getAddress(buffer, buffer.position()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress(ByteBuffer buffer, int position) { - return getAddress0(buffer) + position; + return org.lwjgl.system.MemoryUtil.memAddress(buffer, position); } public static long getAddress(ShortBuffer buffer) { - return getAddress(buffer, buffer.position()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress(ShortBuffer buffer, int position) { - return getAddress0(buffer) + (position << 1); + return org.lwjgl.system.MemoryUtil.memAddress(buffer, position); } public static long getAddress(CharBuffer buffer) { - return getAddress(buffer, buffer.position()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress(CharBuffer buffer, int position) { - return getAddress0(buffer) + (position << 1); + return org.lwjgl.system.MemoryUtil.memAddress(buffer, position); } public static long getAddress(IntBuffer buffer) { - return getAddress(buffer, buffer.position()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress(IntBuffer buffer, int position) { - return getAddress0(buffer) + (position << 2); + return org.lwjgl.system.MemoryUtil.memAddress(buffer, position); } public static long getAddress(FloatBuffer buffer) { - return getAddress(buffer, buffer.position()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress(FloatBuffer buffer, int position) { - return getAddress0(buffer) + (position << 2); + return org.lwjgl.system.MemoryUtil.memAddress(buffer, position); } public static long getAddress(LongBuffer buffer) { - return getAddress(buffer, buffer.position()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress(LongBuffer buffer, int position) { - return getAddress0(buffer) + (position << 3); + return org.lwjgl.system.MemoryUtil.memAddress(buffer, position); } public static long getAddress(DoubleBuffer buffer) { - return getAddress(buffer, buffer.position()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress(DoubleBuffer buffer, int position) { - return getAddress0(buffer) + (position << 3); + return org.lwjgl.system.MemoryUtil.memAddress(buffer, position); } public static long getAddress(PointerBuffer buffer) { - return getAddress(buffer, buffer.position()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer); } public static long getAddress(PointerBuffer buffer, int position) { - return getAddress0(buffer) + (position * PointerBuffer.getPointerSize()); + return org.lwjgl.system.MemoryUtil.memAddress(buffer, position); } // --- [ API utilities - Safe ] --- public static long getAddressSafe(ByteBuffer buffer) { - return buffer == null ? 0L : getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } public static long getAddressSafe(ByteBuffer buffer, int position) { @@ -203,7 +145,7 @@ public final class MemoryUtil { } public static long getAddressSafe(ShortBuffer buffer) { - return buffer == null ? 0L : getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } public static long getAddressSafe(ShortBuffer buffer, int position) { @@ -211,7 +153,7 @@ public final class MemoryUtil { } public static long getAddressSafe(CharBuffer buffer) { - return buffer == null ? 0L : getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } public static long getAddressSafe(CharBuffer buffer, int position) { @@ -219,7 +161,7 @@ public final class MemoryUtil { } public static long getAddressSafe(IntBuffer buffer) { - return buffer == null ? 0L : getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } public static long getAddressSafe(IntBuffer buffer, int position) { @@ -227,7 +169,7 @@ public final class MemoryUtil { } public static long getAddressSafe(FloatBuffer buffer) { - return buffer == null ? 0L : getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } public static long getAddressSafe(FloatBuffer buffer, int position) { @@ -235,7 +177,7 @@ public final class MemoryUtil { } public static long getAddressSafe(LongBuffer buffer) { - return buffer == null ? 0L : getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } public static long getAddressSafe(LongBuffer buffer, int position) { @@ -243,7 +185,7 @@ public final class MemoryUtil { } public static long getAddressSafe(DoubleBuffer buffer) { - return buffer == null ? 0L : getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } public static long getAddressSafe(DoubleBuffer buffer, int position) { @@ -251,7 +193,7 @@ public final class MemoryUtil { } public static long getAddressSafe(PointerBuffer buffer) { - return buffer == null ? 0L : getAddress(buffer); + return org.lwjgl.system.MemoryUtil.memAddressSafe(buffer); } public static long getAddressSafe(PointerBuffer buffer, int position) { @@ -271,7 +213,7 @@ public final class MemoryUtil { * @see String#getBytes() */ public static ByteBuffer encodeASCII(final CharSequence text) { - return encode(text, ascii); + return org.lwjgl.system.MemoryUtil.memASCII(text); } /** @@ -285,7 +227,7 @@ public final class MemoryUtil { * @see String#getBytes() */ public static ByteBuffer encodeUTF8(final CharSequence text) { - return encode(text, utf8); + return org.lwjgl.system.MemoryUtil.memUTF8(text); } /** @@ -297,195 +239,18 @@ public final class MemoryUtil { * @return the encoded text */ public static ByteBuffer encodeUTF16(final CharSequence text) { - return encode(text, utf16); - } - - /** - * Wraps the specified text in a null-terminated CharBuffer and encodes it using the specified Charset. - * - * @param text the text to encode - * @param charset the charset to use for encoding - * - * @return the encoded text - */ - private static ByteBuffer encode(final CharSequence text, final Charset charset) { - if (text == null) return null; - - return encode(CharBuffer.wrap(new CharSequenceNT(text)), charset); - } - - /** - * A {@link CharsetEncoder#encode(java.nio.CharBuffer)} implementation that uses {@link BufferUtils#createByteBuffer(int)} - * instead of {@link ByteBuffer#allocate(int)}. - * - * @see CharsetEncoder#encode(java.nio.CharBuffer) - */ - private static ByteBuffer encode(final CharBuffer in, final Charset charset) { - final CharsetEncoder encoder = - charset.newEncoder(); // encoders are not thread-safe, create a new one on every call - - int n = (int) (in.remaining() * encoder.averageBytesPerChar()); - ByteBuffer out = BufferUtils.createByteBuffer(n); - - if (n == 0 && in.remaining() == 0) return out; - - encoder.reset(); - while (true) { - CoderResult cr = in.hasRemaining() ? encoder.encode(in, out, true) : CoderResult.UNDERFLOW; - if (cr.isUnderflow()) cr = encoder.flush(out); - - if (cr.isUnderflow()) break; - - if (cr.isOverflow()) { - n = 2 * n + 1; // Ensure progress; n might be 0! - ByteBuffer o = BufferUtils.createByteBuffer(n); - out.flip(); - o.put(out); - out = o; - continue; - } - - try { - cr.throwException(); - } catch (CharacterCodingException e) { - throw new RuntimeException(e); - } - } - out.flip(); - return out; + return org.lwjgl.system.MemoryUtil.memUTF16(text); } public static String decodeASCII(final ByteBuffer buffer) { - return decode(buffer, ascii); + return org.lwjgl.system.MemoryUtil.memASCII(buffer); } public static String decodeUTF8(final ByteBuffer buffer) { - return decode(buffer, utf8); + return org.lwjgl.system.MemoryUtil.memUTF8(buffer); } public static String decodeUTF16(final ByteBuffer buffer) { - return decode(buffer, utf16); - } - - private static String decode(final ByteBuffer buffer, final Charset charset) { - if (buffer == null) return null; - - return decodeImpl(buffer, charset); - } - - private static String decodeImpl(final ByteBuffer in, final Charset charset) { - final CharsetDecoder decoder = - charset.newDecoder(); // decoders are not thread-safe, create a new one on every call - - int n = (int) (in.remaining() * decoder.averageCharsPerByte()); - CharBuffer out = BufferUtils.createCharBuffer(n); - - if ((n == 0) && (in.remaining() == 0)) return ""; - - decoder.reset(); - for (; ; ) { - CoderResult cr = in.hasRemaining() ? decoder.decode(in, out, true) : CoderResult.UNDERFLOW; - if (cr.isUnderflow()) cr = decoder.flush(out); - - if (cr.isUnderflow()) break; - if (cr.isOverflow()) { - n = 2 * n + 1; // Ensure progress; n might be 0! - CharBuffer o = BufferUtils.createCharBuffer(n); - out.flip(); - o.put(out); - out = o; - continue; - } - try { - cr.throwException(); - } catch (CharacterCodingException e) { - throw new RuntimeException(e); - } - } - out.flip(); - return out.toString(); - } - - /** A null-terminated CharSequence. */ - private static class CharSequenceNT implements CharSequence { - - final CharSequence source; - - CharSequenceNT(CharSequence source) { - this.source = source; - } - - public int length() { - return source.length() + 1; - } - - public char charAt(final int index) { - return index == source.length() ? '\0' : source.charAt(index); - } - - public CharSequence subSequence(final int start, final int end) { - return new CharSequenceNT(source.subSequence(start, Math.min(end, source.length()))); - } - } - - interface Accessor { - - long getAddress(Buffer buffer); - } - - private static Accessor loadAccessor(final String className) throws Exception { - return (Accessor) Class.forName(className).newInstance(); - } - - /** Default implementation. */ - private static class AccessorJNI implements Accessor { - - public long getAddress(final Buffer buffer) { - return BufferUtils.getBufferAddress(buffer); - } - } - - /** Implementation using reflection on ByteBuffer. */ - private static class AccessorReflect implements Accessor { - - private final Field address; - - AccessorReflect() { - try { - address = getAddressField(); - } catch (NoSuchFieldException e) { - throw new UnsupportedOperationException(e); - } - address.setAccessible(true); - } - - public long getAddress(final Buffer buffer) { - try { - return address.getLong(buffer); - } catch (IllegalAccessException e) { - // cannot happen - return 0L; - } - } - } - - static Field getAddressField() throws NoSuchFieldException { - return getDeclaredFieldRecursive(ByteBuffer.class, "address"); - } - - private static Field getDeclaredFieldRecursive(final Class root, final String fieldName) - throws NoSuchFieldException { - Class type = root; - - do { - try { - return type.getDeclaredField(fieldName); - } catch (NoSuchFieldException e) { - type = type.getSuperclass(); - } - } while (type != null); - - throw new NoSuchFieldException( - fieldName + " does not exist in " + root.getSimpleName() + " or any of its superclasses."); + return org.lwjgl.system.MemoryUtil.memUTF16(buffer); } } diff --git a/src/main/java/org/lwjglx/PointerBuffer.java b/src/main/java/org/lwjglx/PointerBuffer.java index 68812a6..c60801d 100644 --- a/src/main/java/org/lwjglx/PointerBuffer.java +++ b/src/main/java/org/lwjglx/PointerBuffer.java @@ -31,897 +31,70 @@ */ package org.lwjglx; -import java.lang.reflect.Method; +import java.lang.reflect.Field; import java.nio.*; +import javax.annotation.Nullable; -/** - * This class is a container for architecture independent pointer data. - * The interface mirrors the NIO LongBuffer API for convenience. - * - * @author Spasi - */ -public class PointerBuffer implements Comparable { - - private static final boolean is64Bit; +public class PointerBuffer extends org.lwjgl.PointerBuffer { + private static final Field containerAccess; static { - // Use reflection so that we can compile this class for the Generator. - boolean is64 = false; + Class pbClass = org.lwjgl.PointerBuffer.class; try { - Method m = Class.forName("org.lwjgl.Sys").getDeclaredMethod("is64Bit", (Class[]) null); - is64 = (Boolean) m.invoke(null, (Object[]) null); - } catch (Throwable t) { - // ignore - } finally { - is64Bit = is64; + containerAccess = pbClass.getDeclaredField("container"); + } catch (NoSuchFieldException e) { + throw new RuntimeException(e); } } - protected final ByteBuffer pointers; - - protected final Buffer view; - protected final IntBuffer view32; - protected final LongBuffer view64; - - /** - * Creates a new PointerBuffer with the specified capacity. - * - * @param capacity the PointerBuffer size, in number of pointers - */ - public PointerBuffer(final int capacity) { - this(BufferUtils.createByteBuffer(capacity * getPointerSize())); - } - - /** - * Creates a new PointerBuffer using the specified ByteBuffer as its pointer - * data source. This is useful for users that do their own memory management - * over a big ByteBuffer, instead of allocating many small ones. - * - * @param source the source buffer - */ - public PointerBuffer(final ByteBuffer source) { - if (LWJGLUtil.CHECKS) checkSource(source); - - pointers = source.slice().order(source.order()); - - if (is64Bit) { - view32 = null; - view = view64 = pointers.asLongBuffer(); - } else { - view = view32 = pointers.asIntBuffer(); - view64 = null; + private static ByteBuffer getContainer(org.lwjgl.PointerBuffer buffer) { + try { + return (ByteBuffer) containerAccess.get(buffer); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); } } - private static void checkSource(final ByteBuffer source) { - if (!source.isDirect()) throw new IllegalArgumentException("The source buffer is not direct."); - - final int alignment = is64Bit ? 8 : 4; - if ((MemoryUtil.getAddress0(source) + source.position()) % alignment != 0 - || source.remaining() % alignment != 0) - throw new IllegalArgumentException("The source buffer is not aligned to " + alignment + " bytes."); + protected PointerBuffer( + long address, @Nullable ByteBuffer container, int mark, int position, int limit, int capacity) { + super(address, container, mark, position, limit, capacity); } - /** - * Returns the ByteBuffer that backs this PointerBuffer. - * - * @return the pointer ByteBuffer - */ - public ByteBuffer getBuffer() { - return pointers; - } - - /** Returns true if the underlying architecture is 64bit. */ - public static boolean is64Bit() { - return is64Bit; - } - - /** - * Returns the pointer size in bytes, based on the underlying architecture. - * - * @return The pointer size in bytes - */ - public static int getPointerSize() { - return is64Bit ? 8 : 4; - } - - /** - * Returns this buffer's capacity.

- * - * @return The capacity of this buffer - */ - public final int capacity() { - return view.capacity(); - } - - /** - * Returns this buffer's position.

- * - * @return The position of this buffer - */ - public final int position() { - return view.position(); - } - - /** - * Returns this buffer's position, in bytes.

- * - * @return The position of this buffer in bytes. - */ - public final int positionByte() { - return position() * getPointerSize(); - } - - /** - * Sets this buffer's position. If the mark is defined and larger than the - * new position then it is discarded.

- * - * @param newPosition The new position value; must be non-negative - * and no larger than the current limit - * - * @return This buffer - * - * @throws IllegalArgumentException If the preconditions on newPosition do not hold - */ - public final PointerBuffer position(int newPosition) { - view.position(newPosition); - return this; - } - - /** - * Returns this buffer's limit.

- * - * @return The limit of this buffer - */ - public final int limit() { - return view.limit(); - } - - /** - * Sets this buffer's limit. If the position is larger than the new limit - * then it is set to the new limit. If the mark is defined and larger than - * the new limit then it is discarded.

- * - * @param newLimit The new limit value; must be non-negative - * and no larger than this buffer's capacity - * - * @return This buffer - * - * @throws IllegalArgumentException If the preconditions on newLimit do not hold - */ - public final PointerBuffer limit(int newLimit) { - view.limit(newLimit); - return this; - } - - /** - * Sets this buffer's mark at its position.

- * - * @return This buffer - */ - public final PointerBuffer mark() { - view.mark(); - return this; - } - - /** - * Resets this buffer's position to the previously-marked position. - *

- *

Invoking this method neither changes nor discards the mark's - * value.

- * - * @return This buffer - * - * @throws java.nio.InvalidMarkException If the mark has not been set - */ - public final PointerBuffer reset() { - view.reset(); - return this; - } - - /** - * Clears this buffer. The position is set to zero, the limit is set to - * the capacity, and the mark is discarded. - *

- *

Invoke this method before using a sequence of channel-read or - * put operations to fill this buffer. For example: - *

- *

-     * buf.clear();     // Prepare buffer for reading
-     * in.read(buf);    // Read data
- *

- *

This method does not actually erase the data in the buffer, but it - * is named as if it did because it will most often be used in situations - * in which that might as well be the case.

- * - * @return This buffer - */ - public final PointerBuffer clear() { - view.clear(); - return this; - } - - /** - * Flips this buffer. The limit is set to the current position and then - * the position is set to zero. If the mark is defined then it is - * discarded. - *

- *

After a sequence of channel-read or put operations, invoke - * this method to prepare for a sequence of channel-write or relative - * get operations. For example: - *

- *

-     * buf.put(magic);    // Prepend header
-     * in.read(buf);      // Read data into rest of buffer
-     * buf.flip();        // Flip buffer
-     * out.write(buf);    // Write header + data to channel
- *

- *

This method is often used in conjunction with the {@link - * java.nio.ByteBuffer#compact compact} method when transferring data from - * one place to another.

- * - * @return This buffer - */ - public final PointerBuffer flip() { - view.flip(); - return this; - } - - /** - * Rewinds this buffer. The position is set to zero and the mark is - * discarded. - *

- *

Invoke this method before a sequence of channel-write or get - * operations, assuming that the limit has already been set - * appropriately. For example: - *

- *

-     * out.write(buf);    // Write remaining data
-     * buf.rewind();      // Rewind buffer
-     * buf.get(array);    // Copy data into array
- * - * @return This buffer - */ - public final PointerBuffer rewind() { - view.rewind(); - return this; - } - - /** - * Returns the number of elements between the current position and the - * limit.

- * - * @return The number of elements remaining in this buffer - */ - public final int remaining() { - return view.remaining(); - } - - /** - * Returns the number of bytes between the current position and the - * limit.

- * - * @return The number of bytes remaining in this buffer - */ - public final int remainingByte() { - return remaining() * getPointerSize(); - } - - /** - * Tells whether there are any elements between the current position and - * the limit.

- * - * @return true if, and only if, there is at least one element - * remaining in this buffer - */ - public final boolean hasRemaining() { - return view.hasRemaining(); + protected PointerBuffer(org.lwjgl.PointerBuffer ver3) { + super(ver3.address(), getContainer(ver3), ver3.position(), ver3.position(), ver3.limit(), ver3.capacity()); } /** * Allocates a new pointer buffer. - *

- *

The new buffer's position will be zero, its limit will be its - * capacity, and its mark will be undefined.

* - * @param capacity The new buffer's capacity, in pointers + *

The new buffer's position will be zero, its limit will be its capacity, and its mark will be undefined.

* - * @return The new pointer buffer + * @param capacity the new buffer's capacity, in pointers * - * @throws IllegalArgumentException If the capacity is a negative integer + * @return the new pointer buffer + * + * @throws IllegalArgumentException If the {@code capacity} is a negative integer */ public static PointerBuffer allocateDirect(int capacity) { - return new PointerBuffer(capacity); + return new PointerBuffer(org.lwjgl.PointerBuffer.allocateDirect(capacity)); } /** - * This method is used in slice and duplicate instead of normal object construction, - * so that subclasses can return themselves. + * Creates a new PointerBuffer that starts at the specified memory address and has the specified capacity. * - * @param source - * - * @return A new PointerBuffer instance + * @param address the starting memory address + * @param capacity the buffer capacity, in number of pointers */ - protected PointerBuffer newInstance(final ByteBuffer source) { - return new PointerBuffer(source); + public static PointerBuffer create(long address, int capacity) { + return new PointerBuffer(org.lwjgl.PointerBuffer.create(address, capacity)); } /** - * Creates a new pointer buffer whose content is a shared subsequence of - * this buffer's content. - *

- *

The content of the new buffer will start at this buffer's current - * position. Changes to this buffer's content will be visible in the new - * buffer, and vice versa; the two buffers' position, limit, and mark - * values will be independent. - *

- *

The new buffer's position will be zero, its capacity and its limit - * will be the number of longs remaining in this buffer, and its mark - * will be undefined. The new buffer will be direct if, and only if, this - * buffer is direct, and it will be read-only if, and only if, this buffer - * is read-only.

+ * Creates a new PointerBuffer using the specified ByteBuffer as its pointer data source. * - * @return The new pointer buffer + * @param source the source buffer */ - public PointerBuffer slice() { - final int pointerSize = getPointerSize(); - - pointers.position(view.position() * pointerSize); - pointers.limit(view.limit() * pointerSize); - - try { - // We're slicing in the constructor. - return newInstance(pointers); - } finally { - pointers.clear(); - } - } - - /** - * Creates a new pointer buffer that shares this buffer's content. - *

- *

The content of the new buffer will be that of this buffer. Changes - * to this buffer's content will be visible in the new buffer, and vice - * versa; the two buffers' position, limit, and mark values will be - * independent. - *

- *

The new buffer's capacity, limit and position will be - * identical to those of this buffer. The new buffer will be direct if, - * and only if, this buffer is direct, and it will be read-only if, and - * only if, this buffer is read-only.

- * - * @return The new pointer buffer - */ - public PointerBuffer duplicate() { - final PointerBuffer buffer = newInstance(pointers); - - buffer.position(view.position()); - buffer.limit(view.limit()); - - return buffer; - } - - /** - * Creates a new, read-only pointer buffer that shares this buffer's - * content. - *

- *

The content of the new buffer will be that of this buffer. Changes - * to this buffer's content will be visible in the new buffer; the new - * buffer itself, however, will be read-only and will not allow the shared - * content to be modified. The two buffers' position, limit, and mark - * values will be independent. - *

- *

The new buffer's capacity, limit and position will be - * identical to those of this buffer. - *

- *

If this buffer is itself read-only then this method behaves in - * exactly the same way as the {@link #duplicate duplicate} method.

- * - * @return The new, read-only pointer buffer - */ - public PointerBuffer asReadOnlyBuffer() { - final PointerBuffer buffer = new PointerBufferR(pointers); - - buffer.position(view.position()); - buffer.limit(view.limit()); - - return buffer; - } - - public boolean isReadOnly() { - return false; - } - - /** - * Relative get method. Reads the long at this buffer's - * current position, and then increments the position.

- * - * @return The long at the buffer's current position - * - * @throws BufferUnderflowException If the buffer's current position is not smaller than its limit - */ - public long get() { - if (is64Bit) return view64.get(); - else return view32.get() & 0x00000000FFFFFFFFL; - } - - /** - * Relative put method  (optional operation). - *

- *

Writes the given long into this buffer at the current - * position, and then increments the position.

- * - * @param l The long to be written - * - * @return This buffer - * - * @throws BufferOverflowException If this buffer's current position is not smaller than its limit - * @throws ReadOnlyBufferException If this buffer is read-only - */ - public PointerBuffer put(long l) { - if (is64Bit) view64.put(l); - else view32.put((int) l); - return this; - } - - /** - * Convenience put that accepts PointerWrapper objects. - * - * @see #put(long) - */ - public PointerBuffer put(final PointerWrapper pointer) { - return put(pointer.getPointer()); - } - - /** - * Convenience put on a target ByteBuffer. - * - * @param target the target ByteBuffer - * @param l the long value to be written - */ - public static void put(final ByteBuffer target, long l) { - if (is64Bit) target.putLong(l); - else target.putInt((int) l); - } - - /** - * Absolute get method. Reads the long at the given - * index.

- * - * @param index The index from which the long will be read - * - * @return The long at the given index - * - * @throws IndexOutOfBoundsException If index is negative - * or not smaller than the buffer's limit - */ - public long get(int index) { - if (is64Bit) return view64.get(index); - else return view32.get(index) & 0x00000000FFFFFFFFL; - } - - /** - * Absolute put method  (optional operation). - *

- *

Writes the given long into this buffer at the given - * index.

- * - * @param index The index at which the long will be written - * @param l The long value to be written - * - * @return This buffer - * - * @throws IndexOutOfBoundsException If index is negative - * or not smaller than the buffer's limit - * @throws ReadOnlyBufferException If this buffer is read-only - */ - public PointerBuffer put(int index, long l) { - if (is64Bit) view64.put(index, l); - else view32.put(index, (int) l); - return this; - } - - /** - * Convenience put that accepts PointerWrapper objects. - * - * @see #put(int, long) - */ - public PointerBuffer put(int index, PointerWrapper pointer) { - return put(index, pointer.getPointer()); - } - - /** - * Convenience put on a target ByteBuffer. - * - * @param target the target ByteBuffer - * @param index the index at which the long will be written - * @param l the long value to be written - */ - public static void put(final ByteBuffer target, int index, long l) { - if (is64Bit) target.putLong(index * 8, l); - else target.putInt(index * 4, (int) l); - } - - // -- Bulk get operations -- - - /** - * Relative bulk get method. - *

- *

This method transfers longs from this buffer into the given - * destination array. If there are fewer longs remaining in the - * buffer than are required to satisfy the request, that is, if - * length > remaining(), then no - * longs are transferred and a {@link BufferUnderflowException} is - * thrown. - *

- *

Otherwise, this method copies length longs from this - * buffer into the given array, starting at the current position of this - * buffer and at the given offset in the array. The position of this - * buffer is then incremented by length. - *

- *

In other words, an invocation of this method of the form - * src.get(dst, off, len) has exactly the same effect as - * the loop - *

- *

-     *     for (int i = off; i < off + len; i++)
-     *         dst[i] = src.get(); 
- *

- * except that it first checks that there are sufficient longs in - * this buffer and it is potentially much more efficient.

- * - * @param dst The array into which longs are to be written - * @param offset The offset within the array of the first long to be - * written; must be non-negative and no larger than - * dst.length - * @param length The maximum number of longs to be written to the given - * array; must be non-negative and no larger than - * dst.length - offset - * - * @return This buffer - * - * @throws BufferUnderflowException If there are fewer than length longs - * remaining in this buffer - * @throws IndexOutOfBoundsException If the preconditions on the offset and length - * parameters do not hold - */ - public PointerBuffer get(long[] dst, int offset, int length) { - if (is64Bit) view64.get(dst, offset, length); - else { - checkBounds(offset, length, dst.length); - if (length > view32.remaining()) throw new BufferUnderflowException(); - int end = offset + length; - for (int i = offset; i < end; i++) dst[i] = view32.get() & 0x00000000FFFFFFFFL; - } - - return this; - } - - /** - * Relative bulk get method. - *

- *

This method transfers longs from this buffer into the given - * destination array. An invocation of this method of the form - * src.get(a) behaves in exactly the same way as the invocation - *

- *

-     *     src.get(a, 0, a.length) 
- * - * @return This buffer - * - * @throws BufferUnderflowException If there are fewer than length longs - * remaining in this buffer - */ - public PointerBuffer get(long[] dst) { - return get(dst, 0, dst.length); - } - - /** - * Relative bulk put method  (optional operation). - *

- *

This method transfers the longs remaining in the given source - * buffer into this buffer. If there are more longs remaining in the - * source buffer than in this buffer, that is, if - * src.remaining() > remaining(), - * then no longs are transferred and a {@link - * BufferOverflowException} is thrown. - *

- *

Otherwise, this method copies - * n = src.remaining() longs from the given - * buffer into this buffer, starting at each buffer's current position. - * The positions of both buffers are then incremented by n. - *

- *

In other words, an invocation of this method of the form - * dst.put(src) has exactly the same effect as the loop - *

- *

-     *     while (src.hasRemaining())
-     *         dst.put(src.get()); 
- *

- * except that it first checks that there is sufficient space in this - * buffer and it is potentially much more efficient.

- * - * @param src The source buffer from which longs are to be read; - * must not be this buffer - * - * @return This buffer - * - * @throws BufferOverflowException If there is insufficient space in this buffer - * for the remaining longs in the source buffer - * @throws IllegalArgumentException If the source buffer is this buffer - * @throws ReadOnlyBufferException If this buffer is read-only - */ - public PointerBuffer put(PointerBuffer src) { - if (is64Bit) view64.put(src.view64); - else view32.put(src.view32); - return this; - } - - /** - * Relative bulk put method  (optional operation). - *

- *

This method transfers longs into this buffer from the given - * source array. If there are more longs to be copied from the array - * than remain in this buffer, that is, if - * length > remaining(), then no - * longs are transferred and a {@link BufferOverflowException} is - * thrown. - *

- *

Otherwise, this method copies length longs from the - * given array into this buffer, starting at the given offset in the array - * and at the current position of this buffer. The position of this buffer - * is then incremented by length. - *

- *

In other words, an invocation of this method of the form - * dst.put(src, off, len) has exactly the same effect as - * the loop - *

- *

-     *     for (int i = off; i < off + len; i++)
-     *         dst.put(a[i]); 
- *

- * except that it first checks that there is sufficient space in this - * buffer and it is potentially much more efficient.

- * - * @param src The array from which longs are to be read - * @param offset The offset within the array of the first long to be read; - * must be non-negative and no larger than array.length - * @param length The number of longs to be read from the given array; - * must be non-negative and no larger than - * array.length - offset - * - * @return This buffer - * - * @throws BufferOverflowException If there is insufficient space in this buffer - * @throws IndexOutOfBoundsException If the preconditions on the offset and length - * parameters do not hold - * @throws ReadOnlyBufferException If this buffer is read-only - */ - public PointerBuffer put(long[] src, int offset, int length) { - if (is64Bit) view64.put(src, offset, length); - else { - checkBounds(offset, length, src.length); - if (length > view32.remaining()) throw new BufferOverflowException(); - int end = offset + length; - for (int i = offset; i < end; i++) view32.put((int) src[i]); - } - - return this; - } - - /** - * Relative bulk put method  (optional operation). - *

- *

This method transfers the entire content of the given source - * long array into this buffer. An invocation of this method of the - * form dst.put(a) behaves in exactly the same way as the - * invocation - *

- *

-     *     dst.put(a, 0, a.length) 
- * - * @return This buffer - * - * @throws BufferOverflowException If there is insufficient space in this buffer - * @throws ReadOnlyBufferException If this buffer is read-only - */ - public final PointerBuffer put(long[] src) { - return put(src, 0, src.length); - } - - /** - * Compacts this buffer  (optional operation). - *

- *

The longs between the buffer's current position and its limit, - * if any, are copied to the beginning of the buffer. That is, the - * long at index p = position() is copied - * to index zero, the long at index p + 1 is copied - * to index one, and so forth until the long at index - * limit() - 1 is copied to index - * n = limit() - 1 - p. - * The buffer's position is then set to n+1 and its limit is set to - * its capacity. The mark, if defined, is discarded. - *

- *

The buffer's position is set to the number of longs copied, - * rather than to zero, so that an invocation of this method can be - * followed immediately by an invocation of another relative put - * method.

- * - * @return This buffer - * - * @throws ReadOnlyBufferException If this buffer is read-only - */ - public PointerBuffer compact() { - if (is64Bit) view64.compact(); - else view32.compact(); - return this; - } - - /** - * Retrieves this buffer's byte order. - *

- *

The byte order of a pointer buffer created by allocation or by - * wrapping an existing long array is the {@link - * ByteOrder#nativeOrder native order} of the underlying - * hardware. The byte order of a pointer buffer created as a view of a byte buffer is that of the - * byte buffer at the moment that the view is created.

- * - * @return This buffer's byte order - */ - public ByteOrder order() { - if (is64Bit) return view64.order(); - else return view32.order(); - } - - /** - * Returns a string summarizing the state of this buffer.

- * - * @return A summary string - */ - public String toString() { - StringBuilder sb = new StringBuilder(48); - sb.append(getClass().getName()); - sb.append("[pos="); - sb.append(position()); - sb.append(" lim="); - sb.append(limit()); - sb.append(" cap="); - sb.append(capacity()); - sb.append("]"); - return sb.toString(); - } - - /** - * Returns the current hash code of this buffer. - *

- *

The hash code of a pointer buffer depends only upon its remaining - * elements; that is, upon the elements from position() up to, and - * including, the element at limit() - 1. - *

- *

Because buffer hash codes are content-dependent, it is inadvisable - * to use buffers as keys in hash maps or similar data structures unless it - * is known that their contents will not change.

- * - * @return The current hash code of this buffer - */ - public int hashCode() { - int h = 1; - int p = position(); - for (int i = limit() - 1; i >= p; i--) h = 31 * h + (int) get(i); - return h; - } - - /** - * Tells whether or not this buffer is equal to another object. - *

- *

Two pointer buffers are equal if, and only if, - *

- *

    - *

    - *

  1. They have the same element type,

  2. - *

    - *

  3. They have the same number of remaining elements, and - *

  4. - *

    - *

  5. The two sequences of remaining elements, considered - * independently of their starting positions, are pointwise equal. - *

  6. - *

    - *

- *

- *

A pointer buffer is not equal to any other type of object.

- * - * @param ob The object to which this buffer is to be compared - * - * @return true if, and only if, this buffer is equal to the - * given object - */ - public boolean equals(Object ob) { - if (!(ob instanceof PointerBuffer)) return false; - PointerBuffer that = (PointerBuffer) ob; - if (this.remaining() != that.remaining()) return false; - int p = this.position(); - for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) { - long v1 = this.get(i); - long v2 = that.get(j); - if (v1 != v2) { - return false; - } - } - return true; - } - - /** - * Compares this buffer to another. - *

- *

Two pointer buffers are compared by comparing their sequences of - * remaining elements lexicographically, without regard to the starting - * position of each sequence within its corresponding buffer. - *

- *

A pointer buffer is not comparable to any other type of object. - * - * @return A negative integer, zero, or a positive integer as this buffer - * is less than, equal to, or greater than the given buffer - */ - public int compareTo(Object o) { - final PointerBuffer that = (PointerBuffer) o; - int n = this.position() + Math.min(this.remaining(), that.remaining()); - for (int i = this.position(), j = that.position(); i < n; i++, j++) { - long v1 = this.get(i); - long v2 = that.get(j); - if (v1 == v2) continue; - if (v1 < v2) return -1; - return +1; - } - return this.remaining() - that.remaining(); - } - - private static void checkBounds(int off, int len, int size) { - if ((off | len | (off + len) | (size - (off + len))) < 0) throw new IndexOutOfBoundsException(); - } - - /** - * Read-only version of PointerBuffer. - * - * @author Spasi - */ - private static final class PointerBufferR extends PointerBuffer { - - PointerBufferR(final ByteBuffer source) { - super(source); - } - - public boolean isReadOnly() { - return true; - } - - protected PointerBuffer newInstance(final ByteBuffer source) { - return new PointerBufferR(source); - } - - public PointerBuffer asReadOnlyBuffer() { - return duplicate(); - } - - public PointerBuffer put(final long l) { - throw new ReadOnlyBufferException(); - } - - public PointerBuffer put(final int index, final long l) { - throw new ReadOnlyBufferException(); - } - - public PointerBuffer put(final PointerBuffer src) { - throw new ReadOnlyBufferException(); - } - - public PointerBuffer put(final long[] src, final int offset, final int length) { - throw new ReadOnlyBufferException(); - } - - public PointerBuffer compact() { - throw new ReadOnlyBufferException(); - } + public static PointerBuffer create(ByteBuffer source) { + return new PointerBuffer(org.lwjgl.PointerBuffer.create(source)); } } diff --git a/src/main/java/org/lwjglx/PointerWrapper.java b/src/main/java/org/lwjglx/PointerWrapper.java index 566fa6e..f367c53 100644 --- a/src/main/java/org/lwjglx/PointerWrapper.java +++ b/src/main/java/org/lwjglx/PointerWrapper.java @@ -31,12 +31,19 @@ */ package org.lwjglx; +import org.lwjgl.system.Pointer; + /** * A common interface for classes that wrap pointer addresses. * * @author Spasi */ -public interface PointerWrapper { +public interface PointerWrapper extends Pointer { long getPointer(); + + @Override + default long address() { + return getPointer(); + } } diff --git a/src/main/java/org/lwjglx/Sys.java b/src/main/java/org/lwjglx/Sys.java index 9ba6796..f417a37 100644 --- a/src/main/java/org/lwjglx/Sys.java +++ b/src/main/java/org/lwjglx/Sys.java @@ -8,11 +8,13 @@ import javax.swing.JOptionPane; import javax.swing.UIManager; import org.lwjgl.Version; import org.lwjgl.glfw.GLFW; +import org.lwjgl.system.Platform; +import org.lwjglx.opengl.Display; public class Sys { static { - if (glfwInit() != true) throw new IllegalStateException("Unable to initialize glfw"); + if (!glfwInit()) throw new IllegalStateException("Unable to initialize glfw"); } public static void initialize() {} @@ -69,4 +71,12 @@ public class Sys { } JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE); } + + public static boolean is64Bit() { + return Platform.getArchitecture().toString().endsWith("64"); + } + + public String getClipboard() { + return GLFW.glfwGetClipboardString(Display.getWindow()); + } }