下面列出了java.nio.ShortBuffer#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(dataProvider = "shortViewProvider")
public void testShortGet(String desc, IntFunction<ByteBuffer> fbb,
Function<ByteBuffer, ShortBuffer> fbi) {
ByteBuffer bb = allocate(fbb);
ShortBuffer vb = fbi.apply(bb);
int o = bb.position();
for (int i = 0; i < vb.limit(); i++) {
short fromBytes = getShortFromBytes(bb, o + i * 2);
short fromMethodView = bb.getShort(o + i * 2);
assertValues(i, fromBytes, fromMethodView, bb);
short fromBufferView = vb.get(i);
assertValues(i, fromMethodView, fromBufferView, bb, vb);
}
for (int i = 0; i < vb.limit(); i++) {
short v = getShortFromBytes(bb, o + i * 2);
short a = bb.getShort();
assertValues(i, v, a, bb);
short b = vb.get();
assertValues(i, a, b, bb, vb);
}
}
public void getTriangle(int index, Vector3f v1, Vector3f v2, Vector3f v3) {
VertexBuffer pb = getBuffer(Type.Position);
VertexBuffer ib = getBuffer(Type.Index);
if (pb.getFormat() == Format.Float) {
FloatBuffer fpb = (FloatBuffer) pb.getData();
if (ib.getFormat() == Format.UnsignedShort) {
// accepted format for buffers
ShortBuffer sib = (ShortBuffer) ib.getData();
// aquire triangle's vertex indices
int vertIndex = index * 3;
int vert1 = sib.get(vertIndex);
int vert2 = sib.get(vertIndex + 1);
int vert3 = sib.get(vertIndex + 2);
BufferUtils.populateFromBuffer(v1, fpb, vert1);
BufferUtils.populateFromBuffer(v2, fpb, vert2);
BufferUtils.populateFromBuffer(v3, fpb, vert3);
}
}
}
/**
* Queues remaining data from {@code buffer}, and advances its position by the number of bytes
* consumed.
*
* @param buffer A {@link ShortBuffer} containing input data between its position and limit.
*/
public void queueInput(ShortBuffer buffer) {
int framesToWrite = buffer.remaining() / channelCount;
int bytesToWrite = framesToWrite * channelCount * 2;
inputBuffer = ensureSpaceForAdditionalFrames(inputBuffer, inputFrameCount, framesToWrite);
buffer.get(inputBuffer, inputFrameCount * channelCount, bytesToWrite / 2);
inputFrameCount += framesToWrite;
processStreamInput();
}
private void setStrikethroughMetrics(ByteBuffer os_2Table, int upem) {
if (os_2Table == null || os_2Table.capacity() < 30 || upem < 0) {
stSize = .05f;
stPos = -.4f;
return;
}
ShortBuffer sb = os_2Table.asShortBuffer();
stSize = sb.get(13) / (float)upem;
stPos = -sb.get(14) / (float)upem;
}
private void unapplyCurrentVolume(ShortBuffer buffer) {
if (integerMultiplier == 0) {
return;
}
int endOffset = buffer.limit();
for (int i = buffer.position(); i < endOffset; i++) {
int value = buffer.get(i) * 10000 / integerMultiplier;
buffer.put(i, (short) Math.max(-32767, Math.min(32767, value)));
}
}
private void processMonoToStereo(ShortBuffer buffer) throws InterruptedException {
while (buffer.hasRemaining()) {
short sample = buffer.get();
outputBuffer.put(sample);
outputBuffer.put(sample);
if (!outputBuffer.hasRemaining()) {
outputBuffer.flip();
downstream.process(outputBuffer);
outputBuffer.clear();
}
}
}
@Override
public void remix(final ShortBuffer inSBuff, final ShortBuffer outSBuff) {
// Up-mix mono to stereo
final int inRemaining = inSBuff.remaining();
final int outSpace = outSBuff.remaining() / 2;
final int samplesToBeProcessed = Math.min(inRemaining, outSpace);
for (int i = 0; i < samplesToBeProcessed; ++i) {
final short inSample = inSBuff.get();
outSBuff.put(inSample);
outSBuff.put(inSample);
}
}
protected void initAllNames(int requestedID, HashSet names) {
byte[] name = new byte[256];
ByteBuffer buffer = getTableBuffer(nameTag);
if (buffer != null) {
ShortBuffer sbuffer = buffer.asShortBuffer();
sbuffer.get(); // format - not needed.
short numRecords = sbuffer.get();
/* The name table uses unsigned shorts. Many of these
* are known small values that fit in a short.
* The values that are sizes or offsets into the table could be
* greater than 32767, so read and store those as ints
*/
int stringPtr = ((int) sbuffer.get()) & 0xffff;
for (int i=0; i<numRecords; i++) {
short platformID = sbuffer.get();
if (platformID != MS_PLATFORM_ID) {
sbuffer.position(sbuffer.position()+5);
continue; // skip over this record.
}
short encodingID = sbuffer.get();
short langID = sbuffer.get();
short nameID = sbuffer.get();
int nameLen = ((int) sbuffer.get()) & 0xffff;
int namePtr = (((int) sbuffer.get()) & 0xffff) + stringPtr;
if (nameID == requestedID) {
buffer.position(namePtr);
buffer.get(name, 0, nameLen);
names.add(makeString(name, nameLen, encodingID));
}
}
}
}
/**
* Queues remaining data from {@code buffer}, and advances its position by the number of bytes
* consumed.
*
* @param buffer A {@link ShortBuffer} containing input data between its position and limit.
*/
public void queueInput(ShortBuffer buffer) {
int framesToWrite = buffer.remaining() / channelCount;
int bytesToWrite = framesToWrite * channelCount * 2;
inputBuffer = ensureSpaceForAdditionalFrames(inputBuffer, inputFrameCount, framesToWrite);
buffer.get(inputBuffer, inputFrameCount * channelCount, bytesToWrite / 2);
inputFrameCount += framesToWrite;
processStreamInput();
}
private void setUnderlineMetrics(ByteBuffer postTable, int upem) {
if (postTable == null || postTable.capacity() < 12 || upem < 0) {
ulSize = .05f;
ulPos = .1f;
return;
}
ShortBuffer sb = postTable.asShortBuffer();
ulSize = sb.get(5) / (float)upem;
ulPos = -sb.get(4) / (float)upem;
}
private static void fillData(Object data, int[] dims, ShortBuffer buffer) {
if (dims.length > 1) {
for (int i = 0; i < dims[0]; i++) {
Object newArray = Array.get(data, i);
fillData(newArray, stripLeadingIndex(dims), buffer);
}
} else {
buffer.get((short[]) data);
}
}
@Override
public void getStyleMetrics(float pointSize, float[] metrics, int offset) {
if (ulSize == 0f && ulPos == 0f) {
ByteBuffer head_Table = getTableBuffer(headTag);
int upem = -1;
if (head_Table != null && head_Table.capacity() >= 18) {
ShortBuffer sb = head_Table.asShortBuffer();
upem = sb.get(9) & 0xffff;
if (upem < 16 || upem > 16384) {
upem = 2048;
}
}
ByteBuffer os2_Table = getTableBuffer(os_2Tag);
setStrikethroughMetrics(os2_Table, upem);
ByteBuffer post_Table = getTableBuffer(postTag);
setUnderlineMetrics(post_Table, upem);
}
metrics[offset] = stPos * pointSize;
metrics[offset+1] = stSize * pointSize;
metrics[offset+2] = ulPos * pointSize;
metrics[offset+3] = ulSize * pointSize;
}
@Override
public void getStyleMetrics(float pointSize, float[] metrics, int offset) {
if (ulSize == 0f && ulPos == 0f) {
ByteBuffer head_Table = getTableBuffer(headTag);
int upem = -1;
if (head_Table != null && head_Table.capacity() >= 18) {
ShortBuffer sb = head_Table.asShortBuffer();
upem = sb.get(9) & 0xffff;
if (upem < 16 || upem > 16384) {
upem = 2048;
}
}
ByteBuffer os2_Table = getTableBuffer(os_2Tag);
setStrikethroughMetrics(os2_Table, upem);
ByteBuffer post_Table = getTableBuffer(postTag);
setUnderlineMetrics(post_Table, upem);
}
metrics[offset] = stPos * pointSize;
metrics[offset+1] = stSize * pointSize;
metrics[offset+2] = ulPos * pointSize;
metrics[offset+3] = ulSize * pointSize;
}
private static void fillData(Object data, int[] dims, ShortBuffer buffer) {
if (dims.length > 1) {
for (int i = 0; i < dims[0]; i++) {
Object newArray = Array.get(data, i);
fillData(newArray, stripLeadingIndex(dims), buffer);
}
} else {
float[] floatData = (float[]) data;
for (int i = 0; i < dims[0]; i++) {
short element = buffer.get();
floatData[i] = toFloat(element);
}
}
}
@Override
public void remix(final ShortBuffer inSBuff, final ShortBuffer outSBuff) {
// Down-mix stereo to mono
// Viktor Toth's algorithm -
// See: http://www.vttoth.com/CMS/index.php/technical-notes/68
// http://stackoverflow.com/a/25102339
final int inRemaining = inSBuff.remaining() / 2;
final int outSpace = outSBuff.remaining();
final int samplesToBeProcessed = Math.min(inRemaining, outSpace);
for (int i = 0; i < samplesToBeProcessed; ++i) {
// Convert to unsigned
final int a = inSBuff.get() + SIGNED_SHORT_LIMIT;
final int b = inSBuff.get() + SIGNED_SHORT_LIMIT;
int m;
// Pick the equation
if ((a < SIGNED_SHORT_LIMIT) || (b < SIGNED_SHORT_LIMIT)) {
// Viktor's first equation when both sources are "quiet"
// (i.e. less than middle of the dynamic range)
m = a * b / SIGNED_SHORT_LIMIT;
} else {
// Viktor's second equation when one or both sources are loud
m = 2 * (a + b) - (a * b) / SIGNED_SHORT_LIMIT - UNSIGNED_SHORT_MAX;
}
// Convert output back to signed short
if (m == UNSIGNED_SHORT_MAX + 1) m = UNSIGNED_SHORT_MAX;
outSBuff.put((short) (m - SIGNED_SHORT_LIMIT));
}
}
/**
* Queues remaining data from {@code buffer}, and advances its position by the number of bytes
* consumed.
*
* @param buffer A {@link ShortBuffer} containing input data between its position and limit.
*/
public void queueInput(ShortBuffer buffer) {
int framesToWrite = buffer.remaining() / channelCount;
int bytesToWrite = framesToWrite * channelCount * 2;
inputBuffer = ensureSpaceForAdditionalFrames(inputBuffer, inputFrameCount, framesToWrite);
buffer.get(inputBuffer, inputFrameCount * channelCount, bytesToWrite / 2);
inputFrameCount += framesToWrite;
processStreamInput();
}
private void setUnderlineMetrics(ByteBuffer postTable, int upem) {
if (postTable == null || postTable.capacity() < 12 || upem < 0) {
ulSize = .05f;
ulPos = .1f;
return;
}
ShortBuffer sb = postTable.asShortBuffer();
ulSize = sb.get(5) / (float)upem;
ulPos = -sb.get(4) / (float)upem;
}
@Override
public void getStyleMetrics(float pointSize, float[] metrics, int offset) {
if (ulSize == 0f && ulPos == 0f) {
ByteBuffer head_Table = getTableBuffer(headTag);
int upem = -1;
if (head_Table != null && head_Table.capacity() >= 18) {
ShortBuffer sb = head_Table.asShortBuffer();
upem = sb.get(9) & 0xffff;
if (upem < 16 || upem > 16384) {
upem = 2048;
}
}
ByteBuffer os2_Table = getTableBuffer(os_2Tag);
setStrikethroughMetrics(os2_Table, upem);
ByteBuffer post_Table = getTableBuffer(postTag);
setUnderlineMetrics(post_Table, upem);
}
metrics[offset] = stPos * pointSize;
metrics[offset+1] = stSize * pointSize;
metrics[offset+2] = ulPos * pointSize;
metrics[offset+3] = ulSize * pointSize;
}
protected String lookupName(short findLocaleID, int findNameID) {
String foundName = null;
byte[] name = new byte[1024];
ByteBuffer buffer = getTableBuffer(nameTag);
if (buffer != null) {
ShortBuffer sbuffer = buffer.asShortBuffer();
sbuffer.get(); // format - not needed.
short numRecords = sbuffer.get();
/* The name table uses unsigned shorts. Many of these
* are known small values that fit in a short.
* The values that are sizes or offsets into the table could be
* greater than 32767, so read and store those as ints
*/
int stringPtr = ((int) sbuffer.get()) & 0xffff;
for (int i=0; i<numRecords; i++) {
short platformID = sbuffer.get();
if (platformID != MS_PLATFORM_ID) {
sbuffer.position(sbuffer.position()+5);
continue; // skip over this record.
}
short encodingID = sbuffer.get();
short langID = sbuffer.get();
short nameID = sbuffer.get();
int nameLen = ((int) sbuffer.get()) & 0xffff;
int namePtr = (((int) sbuffer.get()) & 0xffff) + stringPtr;
if (nameID == findNameID &&
((foundName == null && langID == ENGLISH_LOCALE_ID)
|| langID == findLocaleID)) {
buffer.position(namePtr);
buffer.get(name, 0, nameLen);
foundName = makeString(name, nameLen, encodingID);
if (langID == findLocaleID) {
return foundName;
}
}
}
}
return foundName;
}
protected String lookupName(short findLocaleID, int findNameID) {
String foundName = null;
byte[] name = new byte[1024];
ByteBuffer buffer = getTableBuffer(nameTag);
if (buffer != null) {
ShortBuffer sbuffer = buffer.asShortBuffer();
sbuffer.get(); // format - not needed.
short numRecords = sbuffer.get();
/* The name table uses unsigned shorts. Many of these
* are known small values that fit in a short.
* The values that are sizes or offsets into the table could be
* greater than 32767, so read and store those as ints
*/
int stringPtr = ((int) sbuffer.get()) & 0xffff;
for (int i=0; i<numRecords; i++) {
short platformID = sbuffer.get();
if (platformID != MS_PLATFORM_ID) {
sbuffer.position(sbuffer.position()+5);
continue; // skip over this record.
}
short encodingID = sbuffer.get();
short langID = sbuffer.get();
short nameID = sbuffer.get();
int nameLen = ((int) sbuffer.get()) & 0xffff;
int namePtr = (((int) sbuffer.get()) & 0xffff) + stringPtr;
if (nameID == findNameID &&
((foundName == null && langID == ENGLISH_LOCALE_ID)
|| langID == findLocaleID)) {
buffer.position(namePtr);
buffer.get(name, 0, nameLen);
foundName = makeString(name, nameLen, platformID, encodingID);
if (langID == findLocaleID) {
return foundName;
}
}
}
}
return foundName;
}