java.nio.ByteBuffer.asShortBuffer()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中java.nio.ByteBuffer.asShortBuffer()方法的一些代码示例,展示了ByteBuffer.asShortBuffer()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.asShortBuffer()方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:asShortBuffer

ByteBuffer.asShortBuffer介绍

[英]Returns a short buffer which is based on the remaining content of this byte buffer.

The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by two, and its mark is not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this byte buffer is direct.

The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
[中]返回基于此字节缓冲区剩余内容的短缓冲区。
新缓冲区的位置为零,其限制和容量为剩余字节数除以2,其标记未设置。新缓冲区的只读属性和字节顺序与此缓冲区的相同。如果此字节缓冲区是直接的,则新缓冲区是直接的。
新缓冲区与此缓冲区共享其内容,这意味着任何一个缓冲区的内容更改都将对另一个缓冲区可见。两个缓冲器的位置、极限和标记是独立的。

代码示例

代码示例来源:origin: libgdx/libgdx

public static ShortBuffer newShortBuffer (int numShorts) {
  ByteBuffer buffer = ByteBuffer.allocateDirect(numShorts * 2);
  buffer.order(ByteOrder.nativeOrder());
  return buffer.asShortBuffer();
}

代码示例来源:origin: libgdx/libgdx

public static ShortBuffer newShortBuffer (int numShorts) {
  ByteBuffer buffer = ByteBuffer.allocateDirect(numShorts * 2);
  buffer.order(ByteOrder.nativeOrder());
  return buffer.asShortBuffer();
}

代码示例来源:origin: stackoverflow.com

byte[] bytes = {};
short[] shorts = new short[bytes.length/2];
// to turn bytes to shorts as either big endian or little endian. 
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);

// to turn shorts back to bytes.
byte[] bytes2 = new byte[shortsA.length * 2];
ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shortsA);

代码示例来源:origin: libgdx/libgdx

void setup (byte[] pcm, int channels, int sampleRate) {
  int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2));
  int samples = bytes / (2 * channels);
  duration = samples / (float)sampleRate;
  ByteBuffer buffer = ByteBuffer.allocateDirect(bytes);
  buffer.order(ByteOrder.nativeOrder());
  buffer.put(pcm, 0, bytes);
  buffer.flip();
  if (bufferID == -1) {
    bufferID = alGenBuffers();
    alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate);
  }
}

代码示例来源:origin: libgdx/libgdx

void setup (byte[] pcm, int channels, int sampleRate) {
  int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2));
  int samples = bytes / (2 * channels);
  duration = samples / (float)sampleRate;
  ByteBuffer buffer = ByteBuffer.allocateDirect(bytes);
  buffer.order(ByteOrder.nativeOrder());
  buffer.put(pcm, 0, bytes);
  buffer.flip();
  if (bufferID == -1) {
    bufferID = alGenBuffers();
    alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate);
  }
}

代码示例来源:origin: libgdx/libgdx

void setup (byte[] pcm, int channels, int sampleRate) {
  int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2));
  int samples = bytes / (2 * channels);
  duration = samples / (float)sampleRate;
  ByteBuffer buffer = ByteBuffer.allocateDirect(bytes);
  buffer.order(ByteOrder.nativeOrder());
  buffer.put(pcm, 0, bytes);
  buffer.flip();
  if (bufferID == -1) {
    bufferID = alGenBuffers();
    alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate);
  }
}

代码示例来源:origin: libgdx/libgdx

void setup (byte[] pcm, int channels, int sampleRate) {
  int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2));
  int samples = bytes / (2 * channels);
  duration = samples / (float)sampleRate;
  ByteBuffer buffer = ByteBuffer.allocateDirect(bytes);
  buffer.order(ByteOrder.nativeOrder());
  buffer.put(pcm, 0, bytes);
  buffer.flip();
  if (bufferID == -1) {
    bufferID = alGenBuffers();
    alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate);
  }
}

代码示例来源:origin: robovm/robovm

@MarshalsArray
public static ShortBuffer toShortBuffer(Class<?> cls, long handle, long flags, int d1) {
  return VM.newDirectByteBuffer(handle, d1 << 1).order(ByteOrder.nativeOrder()).asShortBuffer();
}
@MarshalsArray

代码示例来源:origin: robovm/robovm

/**
 * Returns a {@link ShortBuffer} which reads and writes to the same memory
 * location pointed to by this {@link ShortPtr}.
 * 
 * @param n the maximum number of shorts the {@link ShortBuffer} can 
 *        read/write. This will be the {@link ShortBuffer}'s 
 *        <code>capacity</code>.
 * @return the {@link ShortBuffer}.
 */
public ShortBuffer asShortBuffer(int n) {
  return as(BytePtr.class).asByteBuffer(n << 1).order(ByteOrder.nativeOrder()).asShortBuffer();
}

代码示例来源:origin: google/ExoPlayer

@Override
public void queueInput(ByteBuffer inputBuffer) {
 Assertions.checkState(sonic != null);
 if (inputBuffer.hasRemaining()) {
  ShortBuffer shortBuffer = inputBuffer.asShortBuffer();
  int inputSize = inputBuffer.remaining();
  inputBytes += inputSize;
  sonic.queueInput(shortBuffer);
  inputBuffer.position(inputBuffer.position() + inputSize);
 }
 int outputSize = sonic.getFramesAvailable() * channelCount * 2;
 if (outputSize > 0) {
  if (buffer.capacity() < outputSize) {
   buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
   shortBuffer = buffer.asShortBuffer();
  } else {
   buffer.clear();
   shortBuffer.clear();
  }
  sonic.getOutput(shortBuffer);
  outputBytes += outputSize;
  buffer.limit(outputSize);
  outputBuffer = buffer;
 }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

/**
 * Create a new ShortBuffer of the specified size.
 * 
 * @param size
 *            required number of shorts to store.
 * @return the new ShortBuffer
 */
public static ShortBuffer createShortBuffer(int size) {
  ShortBuffer buf = allocator.allocate(2 * size).order(ByteOrder.nativeOrder()).asShortBuffer();
  buf.clear();
  onBufferAllocated(buf);
  return buf;
}

代码示例来源:origin: robovm/robovm

@SuppressWarnings("unchecked")
public <T extends Buffer> T getAudioDataAsBuffer(Class<T> bufferType) {
  long dataPointer = getDataPointer();
  if (bufferType == ByteBuffer.class) {
    return (T) VM.newDirectByteBuffer(dataPointer, getAudioDataByteSize());
  } else if (bufferType == ShortBuffer.class) {
    return (T) VM.newDirectByteBuffer(dataPointer, getAudioDataByteSize() << 1).order(ByteOrder.nativeOrder()).asShortBuffer();
  } else if (bufferType == IntBuffer.class) {
    return (T) VM.newDirectByteBuffer(dataPointer, getAudioDataByteSize() << 2).order(ByteOrder.nativeOrder()).asIntBuffer();
  } else if (bufferType == FloatBuffer.class) {
    return (T) VM.newDirectByteBuffer(dataPointer, getAudioDataByteSize() << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();
  } else {
    throw new UnsupportedOperationException("Buffer type not supported: " + bufferType);
  }
}

代码示例来源:origin: robovm/robovm

@SuppressWarnings("unchecked")
public <T extends Buffer> T getDataAsBuffer(Class<T> bufferType) {
  long dataPointer = getDataPointer();
  if (bufferType == ByteBuffer.class) {
    return (T) VM.newDirectByteBuffer(dataPointer, getDataByteSize());
  } else if (bufferType == ShortBuffer.class) {
    return (T) VM.newDirectByteBuffer(dataPointer, getDataByteSize() << 1).order(ByteOrder.nativeOrder()).asShortBuffer();
  } else if (bufferType == IntBuffer.class) {
    return (T) VM.newDirectByteBuffer(dataPointer, getDataByteSize() << 2).order(ByteOrder.nativeOrder()).asIntBuffer();
  } else if (bufferType == FloatBuffer.class) {
    return (T) VM.newDirectByteBuffer(dataPointer, getDataByteSize() << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();
  } else {
    throw new UnsupportedOperationException("Buffer type not supported: " + bufferType);
  }
}

代码示例来源:origin: google/ExoPlayer

/** Returns the next buffer with size up to {@code sizeBytes}. */
public ByteBuffer getNextInputBuffer(int sizeBytes) {
 ByteBuffer inputBuffer = ByteBuffer.allocate(sizeBytes).order(ByteOrder.nativeOrder());
 ShortBuffer inputBufferAsShortBuffer = inputBuffer.asShortBuffer();
 int limit = buffer.limit();
 buffer.limit(Math.min(buffer.position() + sizeBytes / 2, limit));
 inputBufferAsShortBuffer.put(buffer);
 buffer.limit(limit);
 inputBuffer.limit(inputBufferAsShortBuffer.position() * 2);
 return inputBuffer;
}

代码示例来源:origin: RoaringBitmap/RoaringBitmap

@Override
public MappeableContainer getContainerAtIndex(int i) {
 ByteBuffer tmp = buffer.duplicate();// sad but ByteBuffer is not thread-safe so it is either a
                   // duplicate or a lock
 // note that tmp will indeed be garbage-collected some time after the end of this function
 tmp.order(buffer.order());
 tmp.position(getOffsetContainer(i));
 boolean hasrun = hasRunCompression();
 if (isRunContainer(i, hasrun)) {
  // first, we have a short giving the number of runs
  int nbrruns = BufferUtil.toIntUnsigned(tmp.getShort());
  final ShortBuffer shortArray = tmp.asShortBuffer();
  shortArray.limit(2 * nbrruns);
  return new MappeableRunContainer(shortArray, nbrruns);
 }
 int cardinality = getCardinality(i);
 final boolean isBitmap = cardinality > MappeableArrayContainer.DEFAULT_MAX_SIZE; // if not a
                                       // runcontainer
 if (isBitmap) {
  final LongBuffer bitmapArray = tmp.asLongBuffer();
  bitmapArray.limit(MappeableBitmapContainer.MAX_CAPACITY / 64);
  return new MappeableBitmapContainer(bitmapArray, cardinality);
 } else {
  final ShortBuffer shortArray = tmp.asShortBuffer();
  shortArray.limit(cardinality);
  return new MappeableArrayContainer(shortArray, cardinality);
 }
}

代码示例来源:origin: deeplearning4j/nd4j

switch (_dtype) {
  case DOUBLE: {
    val db = bb.order(_order).asDoubleBuffer();
    for (int e = 0; e < prod; e++)
      doubles[e] = db.get(e);
    val fb = bb.order(_order).asFloatBuffer();
    for (int e = 0; e < prod; e++)
      doubles[e] = (double) fb.get(e);
    val sb = bb.order(_order).asShortBuffer();
    for (int e = 0; e < prod; e++)
      doubles[e] = (double) HalfIndexer.toFloat((int) sb.get(e));

代码示例来源:origin: greenrobot/essentials

@Test
public void testUpdateShortArray() throws Exception {
  for (int i = 0; i < 16; i++) {
    ByteBuffer byteBuffer = prepareByteBufferLE(i, 16);
    byteBuffer.put(INPUT16);
    long expected = getHash(byteBuffer);
    prepareMurmur3aChecksum(i);
    short[] array = new short[8];
    ByteBuffer.wrap(INPUT16).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(array);
    murmur3a.updateShort(array);
    Assert.assertEquals("Alignment " + i, expected, murmur3a.getValue());
  }
}

代码示例来源:origin: opensourceBIM/BIMserver

public static byte[] shortToByteArray(short inShort) {
  byte[] bArray = new byte[2];
  ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
  bBuffer.order(ByteOrder.LITTLE_ENDIAN);
  ShortBuffer lBuffer = bBuffer.asShortBuffer();
  lBuffer.put(inShort);
  return bArray;
}

代码示例来源:origin: Cleveroad/WaveInApp

private void initIndices() {
  short[] indices = new short[(POINTS_PER_WAVE + ADDITIONAL_POINTS - 2) * COORDS_PER_VERTEX];
  for (int i = 0; i < indices.length / COORDS_PER_VERTEX; i++) {
    indices[COORDS_PER_VERTEX * i] = 0;
    indices[COORDS_PER_VERTEX * i + 1] = (short) (i + 1);
    indices[COORDS_PER_VERTEX * i + 2] = (short) (i + 2);
  }
  ByteBuffer indicesByteBuffer = ByteBuffer.allocateDirect(indices.length * SIZE_OF_SHORT);
  indicesByteBuffer.order(ByteOrder.nativeOrder());
  shortBuffer = indicesByteBuffer.asShortBuffer();
  shortBuffer.put(indices);
  shortBuffer.position(0);
}

代码示例来源:origin: doggycoder/AndroidOpenGLDemo

private void initData(){
  ByteBuffer a=ByteBuffer.allocateDirect(cubePositions.length*4);
  a.order(ByteOrder.nativeOrder());
  vertexBuf=a.asFloatBuffer();
  vertexBuf.put(cubePositions);
  vertexBuf.position(0);
  ByteBuffer b=ByteBuffer.allocateDirect(color.length*4);
  b.order(ByteOrder.nativeOrder());
  colorBuf=b.asFloatBuffer();
  colorBuf.put(color);
  colorBuf.position(0);
  ByteBuffer c=ByteBuffer.allocateDirect(index.length*2);
  c.order(ByteOrder.nativeOrder());
  indexBuf=c.asShortBuffer();
  indexBuf.put(index);
  indexBuf.position(0);
}

相关文章

微信公众号

最新文章

更多