use of java.nio.ShortBuffer in project android_frameworks_base by crdroidandroid.
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);
}
use of java.nio.ShortBuffer in project android_frameworks_base by crdroidandroid.
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 jwt by emweb.
the class WServerGLWidget method bufferDataiv.
@Override
public void bufferDataiv(WGLWidget.GLenum target, java.nio.IntBuffer buffer, WGLWidget.GLenum usage, WGLWidget.GLenum type) {
buffer.rewind();
ShortBuffer sb = ShortBuffer.allocate(buffer.capacity());
for (int i = 0; i < buffer.capacity(); i++) {
sb.put((short) buffer.get(i));
}
sb.rewind();
glCtx_.glBufferData(serverGLenum(target), sb.capacity() * 2, sb, serverGLenum(usage));
if (debug_)
System.out.println(glCtx_.glGetError());
}
use of java.nio.ShortBuffer in project lavaplayer by sedmelluq.
the class FinalPcmAudioFilter method process.
@Override
public void process(ShortBuffer buffer) throws InterruptedException {
if (ignoredFrames > 0) {
long skipped = Math.min(buffer.remaining(), ignoredFrames);
buffer.position(buffer.position() + (int) skipped);
ignoredFrames -= skipped;
}
ShortBuffer local = buffer.duplicate();
while (buffer.remaining() > 0) {
int chunk = Math.min(buffer.remaining(), frameBuffer.remaining());
local.position(buffer.position());
local.limit(local.position() + chunk);
frameBuffer.put(local);
dispatch();
buffer.position(buffer.position() + chunk);
}
}
use of java.nio.ShortBuffer in project twister2 by DSC-SPIDAL.
the class TestBinaryFileFormatter method main.
public static void main(String[] args) {
Config.Builder builder = new Config.Builder();
builder.put("input.file.path", "/home/pulasthi/git/twister2/twister2/" + "data/src/test/resources/2000.bin");
Config txtFileConf = builder.build();
Path path = new Path("/home/pulasthi/git/twister2/twister2/data/src/test/resources/2000.bin");
InputFormat binaryInputFormatter = new BinaryInputFormatter(path, 2000 * Short.BYTES);
binaryInputFormatter.configure(txtFileConf);
int minSplits = 8;
double expectedSum = 1.97973979E8;
double newSum = 0.0;
int count = 0;
Buffer buffer = null;
try {
InputSplit[] inputSplits = binaryInputFormatter.createInputSplits(minSplits);
InputSplitAssigner inputSplitAssigner = binaryInputFormatter.getInputSplitAssigner(inputSplits);
InputSplit currentSplit;
byte[] line = new byte[4000];
ByteBuffer byteBuffer = ByteBuffer.allocate(4000);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
while ((currentSplit = inputSplitAssigner.getNextInputSplit(null, 0)) != null) {
binaryInputFormatter.open(currentSplit);
while (binaryInputFormatter.nextRecord(line) != null) {
byteBuffer.clear();
byteBuffer.put(line);
byteBuffer.flip();
buffer = byteBuffer.asShortBuffer();
short[] shortArray = new short[2000];
((ShortBuffer) buffer).get(shortArray);
for (short i : shortArray) {
newSum += i;
count++;
}
}
}
System.out.println(newSum);
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations