Search in sources :

Example 26 with ShortBuffer

use of java.nio.ShortBuffer in project UltimateAndroid by cymcsg.

the class TextureUtils method toShortBuffer.

public static ShortBuffer toShortBuffer(short[] v) {
    ByteBuffer buf = ByteBuffer.allocateDirect(v.length * 2);
    buf.order(ByteOrder.nativeOrder());
    ShortBuffer buffer = buf.asShortBuffer();
    buffer.put(v);
    buffer.position(0);
    return buffer;
}
Also used : ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 27 with ShortBuffer

use of java.nio.ShortBuffer in project android_frameworks_base by DirtyUnicorns.

the class DhcpPacket 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 int checksum(ByteBuffer buf, int seed, int start, int end) {
    int sum = seed;
    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);
    short[] shortArray = new short[(end - start) / 2];
    shortBuf.get(shortArray);
    for (short s : shortArray) {
        sum += intAbs(s);
    }
    start += shortArray.length * 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);
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 28 with ShortBuffer

use of java.nio.ShortBuffer in project jmonkeyengine by jMonkeyEngine.

the class LodGenerator method makeLod.

private VertexBuffer makeLod(Mesh mesh) {
    VertexBuffer indexBuffer = mesh.getBuffer(VertexBuffer.Type.Index);
    boolean isShortBuffer = indexBuffer.getFormat() == VertexBuffer.Format.UnsignedShort;
    // Create buffers.	
    VertexBuffer lodBuffer = new VertexBuffer(VertexBuffer.Type.Index);
    int bufsize = indexCount == 0 ? 3 : indexCount;
    if (isShortBuffer) {
        lodBuffer.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.UnsignedShort, BufferUtils.createShortBuffer(bufsize));
    } else {
        lodBuffer.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.UnsignedInt, BufferUtils.createIntBuffer(bufsize));
    }
    lodBuffer.getData().rewind();
    //Check if we should fill it with a "dummy" triangle.
    if (indexCount == 0) {
        if (isShortBuffer) {
            for (int m = 0; m < 3; m++) {
                ((ShortBuffer) lodBuffer.getData()).put((short) 0);
            }
        } else {
            for (int m = 0; m < 3; m++) {
                ((IntBuffer) lodBuffer.getData()).put(0);
            }
        }
    }
    // Fill buffers.       
    Buffer buf = lodBuffer.getData();
    buf.rewind();
    for (Triangle triangle : triangleList) {
        if (!triangle.isRemoved) {
            //    assert (indexCount != 0);
            if (isShortBuffer) {
                for (int m = 0; m < 3; m++) {
                    ((ShortBuffer) buf).put((short) triangle.vertexId[m]);
                }
            } else {
                for (int m = 0; m < 3; m++) {
                    ((IntBuffer) buf).put(triangle.vertexId[m]);
                }
            }
        }
    }
    buf.clear();
    lodBuffer.updateData(buf);
    return lodBuffer;
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) VertexBuffer(com.jme3.scene.VertexBuffer) IntBuffer(java.nio.IntBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 29 with ShortBuffer

use of java.nio.ShortBuffer in project android_frameworks_base by AOSPA.

the class GLLogWrapper method toByteBuffer.

private ByteBuffer toByteBuffer(int byteCount, Buffer input) {
    ByteBuffer result = null;
    boolean convertWholeBuffer = (byteCount < 0);
    if (input instanceof ByteBuffer) {
        ByteBuffer input2 = (ByteBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = input2.limit() - position;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        for (int i = 0; i < byteCount; i++) {
            result.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof CharBuffer) {
        CharBuffer input2 = (CharBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 2;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        CharBuffer result2 = result.asCharBuffer();
        for (int i = 0; i < byteCount / 2; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof ShortBuffer) {
        ShortBuffer input2 = (ShortBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 2;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        ShortBuffer result2 = result.asShortBuffer();
        for (int i = 0; i < byteCount / 2; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof IntBuffer) {
        IntBuffer input2 = (IntBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 4;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        IntBuffer result2 = result.asIntBuffer();
        for (int i = 0; i < byteCount / 4; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof FloatBuffer) {
        FloatBuffer input2 = (FloatBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 4;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        FloatBuffer result2 = result.asFloatBuffer();
        for (int i = 0; i < byteCount / 4; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof DoubleBuffer) {
        DoubleBuffer input2 = (DoubleBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 8;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        DoubleBuffer result2 = result.asDoubleBuffer();
        for (int i = 0; i < byteCount / 8; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof LongBuffer) {
        LongBuffer input2 = (LongBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 8;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        LongBuffer result2 = result.asLongBuffer();
        for (int i = 0; i < byteCount / 8; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else {
        throw new RuntimeException("Unimplemented Buffer subclass.");
    }
    result.rewind();
    // The OpenGL API will interpret the result in hardware byte order,
    // so we better do that as well:
    result.order(ByteOrder.nativeOrder());
    return result;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) IntBuffer(java.nio.IntBuffer) CharBuffer(java.nio.CharBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 30 with ShortBuffer

use of java.nio.ShortBuffer in project geode by apache.

the class ByteSourceJUnitTest method testGetShortInt.

@Test
public void testGetShortInt() {
    ByteBuffer bb = ByteBuffer.allocate(10);
    ShortBuffer sb = bb.asShortBuffer();
    sb.put((short) 0x1110);
    sb.put((short) 0x2220);
    sb.put((short) 0x3330);
    sb.put((short) 0x4440);
    sb.put((short) 0x5550);
    byte[] bytes = bb.array();
    ByteSource bs = createByteSource(bytes);
    bs.position(3);
    short s = bs.getShort(0);
    assertEquals(0x1110, s);
    assertEquals(3, bs.position());
    s = bs.getShort(2);
    assertEquals(0x2220, s);
    assertEquals(3, bs.position());
    s = bs.getShort(8);
    assertEquals(0x5550, s);
    assertEquals(3, bs.position());
    try {
        bs.getShort(9);
        fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException expected) {
    }
}
Also used : ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

ShortBuffer (java.nio.ShortBuffer)227 ByteBuffer (java.nio.ByteBuffer)78 FloatBuffer (java.nio.FloatBuffer)54 IntBuffer (java.nio.IntBuffer)45 DoubleBuffer (java.nio.DoubleBuffer)23 LongBuffer (java.nio.LongBuffer)16 Test (org.junit.Test)14 Buffer (java.nio.Buffer)11 BufferOverflowException (java.nio.BufferOverflowException)11 CharBuffer (java.nio.CharBuffer)11 VertexBuffer (com.jme3.scene.VertexBuffer)8 BufferUnderflowException (java.nio.BufferUnderflowException)7 BytePointer (org.bytedeco.javacpp.BytePointer)7 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)6 IOException (java.io.IOException)5 Vector3f (com.jme3.math.Vector3f)4 ArrayList (java.util.ArrayList)4 Bitmap (android.graphics.Bitmap)3 Mesh (com.jme3.scene.Mesh)3 InvalidMarkException (java.nio.InvalidMarkException)3