From c70c730b22c847668cf475dc6f841b85297ac3ab Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 21 Feb 2014 07:31:12 +0100 Subject: Bitstream: Add static 'long toUint32Long(int)' and 'int toUint32Int(int)' conversion functions --- src/java/com/jogamp/common/util/Bitstream.java | 42 ++++++++++++++++++++------ 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'src/java') diff --git a/src/java/com/jogamp/common/util/Bitstream.java b/src/java/com/jogamp/common/util/Bitstream.java index 7bf0c16..419b356 100644 --- a/src/java/com/jogamp/common/util/Bitstream.java +++ b/src/java/com/jogamp/common/util/Bitstream.java @@ -1166,11 +1166,9 @@ public class Bitstream { * Return incoming int32 as read via {@link #readBits31(boolean, int)} * and swap bytes if !bigEndian. *

- * In case the returned value shall be interpreted as unsigned, - * it shall be cast to long as follows: - *

-     *   final long l = 0xffffffffL & int32;
-     * 
+ * In case the returned value shall be interpreted as uint32_t + * utilize {@link #toUint32Long(int)} or {@link #toUint32Int(int)} for + * an appropriate conversion. *

* @param msbFirst if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB. * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as little-endian. @@ -1216,11 +1214,9 @@ public class Bitstream { * Return incoming int32 as read via {@link #readBits31(boolean, int)} * and swap bytes if !bigEndian. *

- * In case the returned value shall be interpreted as unsigned, - * it shall be cast to long as follows: - *

-     *   final long l = 0xffffffffL & int32;
-     * 
+ * In case the returned value shall be interpreted as uint32_t + * utilize {@link #toUint32Long(int)} or {@link #toUint32Int(int)} for + * an appropriate conversion. *

* @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as little-endian. * @return the 32bit value, which might be unsigned or 2-complement signed value. @@ -1303,6 +1299,32 @@ public class Bitstream { } } + /** + * Reinterpret the given int32_t value as uint32_t, + * i.e. perform the following cast to long: + *
+     *   final long l = 0xffffffffL & int32;
+     * 
+ */ + public static final long toUint32Long(final int val) { + return 0xffffffffL & val; + } + + /** + * Returns the reinterpreted given int32_t value + * as uint32_t if < {@link Integer#MAX_VALUE} + * as within an int storage. + * Otherwise return -1. + */ + public static final int toUint32Int(final int val) { + final long v = toUint32Long(val); + if( v > Integer.MAX_VALUE ) { + return -1; + } else { + return (int)v; + } + } + public String toString() { return String.format("Bitstream[%s]", toStringImpl()); } -- cgit v1.2.3