use of java.nio.ShortBuffer in project robovm by robovm.
the class ShortBufferTest method testEquals.
public void testEquals() {
// equal to self
assertTrue(buf.equals(buf));
ShortBuffer readonly = buf.asReadOnlyBuffer();
assertTrue(buf.equals(readonly));
ShortBuffer duplicate = buf.duplicate();
assertTrue(buf.equals(duplicate));
// always false, if type mismatch
assertFalse(buf.equals(Boolean.TRUE));
assertTrue(buf.capacity() > 5);
buf.limit(buf.capacity()).position(0);
readonly.limit(readonly.capacity()).position(1);
assertFalse(buf.equals(readonly));
buf.limit(buf.capacity() - 1).position(0);
duplicate.limit(duplicate.capacity()).position(0);
assertFalse(buf.equals(duplicate));
}
use of java.nio.ShortBuffer in project robovm by robovm.
the class ReadOnlyShortBufferTest method testPutShortBuffer.
public void testPutShortBuffer() {
ShortBuffer other = ShortBuffer.allocate(1);
try {
buf.put(other);
//$NON-NLS-1$
fail("Should throw ReadOnlyBufferException");
} catch (ReadOnlyBufferException e) {
// expected
}
try {
buf.put((ShortBuffer) null);
//$NON-NLS-1$
fail("Should throw ReadOnlyBufferException");
} catch (ReadOnlyBufferException e) {
// expected
}
try {
buf.put(buf);
//$NON-NLS-1$
fail("Should throw ReadOnlyBufferException");
} catch (ReadOnlyBufferException e) {
// expected
}
}
use of java.nio.ShortBuffer in project robovm by robovm.
the class ReadOnlyShortBufferTest method testHashCode.
public void testHashCode() {
ShortBuffer duplicate = buf.duplicate();
assertEquals(buf.hashCode(), duplicate.hashCode());
}
use of java.nio.ShortBuffer in project platform_frameworks_base by android.
the class IpUtils method checksum.
/**
* Performs an IP checksum (used in IP header and across UDP
* payload) on the specified portion of a ByteBuffer. The seed
* allows the checksum to commence with a specified value.
*/
private static int checksum(ByteBuffer buf, int seed, int start, int end) {
int sum = seed;
final int bufPosition = buf.position();
// set position of original ByteBuffer, so that the ShortBuffer
// will be correctly initialized
buf.position(start);
ShortBuffer shortBuf = buf.asShortBuffer();
// re-set ByteBuffer position
buf.position(bufPosition);
final int numShorts = (end - start) / 2;
for (int i = 0; i < numShorts; i++) {
sum += intAbs(shortBuf.get(i));
}
start += numShorts * 2;
// see if a singleton byte remains
if (end != start) {
short b = buf.get(start);
// make it unsigned
if (b < 0) {
b += 256;
}
sum += b * 256;
}
sum = ((sum >> 16) & 0xFFFF) + (sum & 0xFFFF);
sum = ((sum + ((sum >> 16) & 0xFFFF)) & 0xFFFF);
int negated = ~sum;
return intAbs((short) negated);
}
use of java.nio.ShortBuffer in project libgdx by libgdx.
the class Mesh method calculateRadiusSquared.
/** Calculates the squared radius of the bounding sphere around the specified center for the specified part.
* @param centerX The X coordinate of the center of the bounding sphere
* @param centerY The Y coordinate of the center of the bounding sphere
* @param centerZ The Z coordinate of the center of the bounding sphere
* @param offset the start index of the part.
* @param count the amount of indices the part contains.
* @return the squared radius of the bounding sphere. */
public float calculateRadiusSquared(final float centerX, final float centerY, final float centerZ, int offset, int count, final Matrix4 transform) {
int numIndices = getNumIndices();
if (offset < 0 || count < 1 || offset + count > numIndices)
throw new GdxRuntimeException("Not enough indices");
final FloatBuffer verts = vertices.getBuffer();
final ShortBuffer index = indices.getBuffer();
final VertexAttribute posAttrib = getVertexAttribute(Usage.Position);
final int posoff = posAttrib.offset / 4;
final int vertexSize = vertices.getAttributes().vertexSize / 4;
final int end = offset + count;
float result = 0;
switch(posAttrib.numComponents) {
case 1:
for (int i = offset; i < end; i++) {
final int idx = index.get(i) * vertexSize + posoff;
tmpV.set(verts.get(idx), 0, 0);
if (transform != null)
tmpV.mul(transform);
final float r = tmpV.sub(centerX, centerY, centerZ).len2();
if (r > result)
result = r;
}
break;
case 2:
for (int i = offset; i < end; i++) {
final int idx = index.get(i) * vertexSize + posoff;
tmpV.set(verts.get(idx), verts.get(idx + 1), 0);
if (transform != null)
tmpV.mul(transform);
final float r = tmpV.sub(centerX, centerY, centerZ).len2();
if (r > result)
result = r;
}
break;
case 3:
for (int i = offset; i < end; i++) {
final int idx = index.get(i) * vertexSize + posoff;
tmpV.set(verts.get(idx), verts.get(idx + 1), verts.get(idx + 2));
if (transform != null)
tmpV.mul(transform);
final float r = tmpV.sub(centerX, centerY, centerZ).len2();
if (r > result)
result = r;
}
break;
}
return result;
}
Aggregations