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

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

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

ByteBuffer.allocateDirect介绍

[英]Creates a direct byte buffer based on a newly allocated memory block.
[中]基于新分配的内存块创建直接字节缓冲区。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private static ByteBuffer allocate(int capacity, boolean direct) {
  return (direct ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity));
}

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

public static ByteBuffer newByteBuffer (int numBytes) {
  ByteBuffer buffer = ByteBuffer.allocateDirect(numBytes);
  buffer.order(ByteOrder.nativeOrder());
  return buffer;
}

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

/**
 * Returns a new {@link ByteBuffer} which has the same {@link ByteOrder} as the native order of the machine.
 */
public static ByteBuffer allocateDirectWithNativeOrder(int capacity) {
  return ByteBuffer.allocateDirect(capacity).order(
      PlatformDependent.BIG_ENDIAN_NATIVE_ORDER ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
}

代码示例来源: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: apache/ignite

/** {@inheritDoc} */
@Override public ByteBuffer encode(GridNioSession ses, Object msg) throws IOException, IgniteCheckedException {
  byte[] msg0 = (byte[])msg;
  int cap = msg0.length + delim.length;
  ByteBuffer res = directBuf ? ByteBuffer.allocateDirect(cap) : ByteBuffer.allocate(cap);
  res.put(msg0);
  res.put(delim);
  res.flip();
  return res;
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Clone an input bytes array as direct ByteBuffer.
 */
static ByteBuffer cloneAsDirectByteBuffer(byte[] input, int offset, int len) {
 if (input == null) { // an input can be null, if erased or not to read
  return null;
 }
 ByteBuffer directBuffer = ByteBuffer.allocateDirect(len);
 directBuffer.put(input, offset, len);
 directBuffer.flip();
 return directBuffer;
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public ChannelBuffer copy(int index, int length) {
  ByteBuffer src;
  try {
    src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
  } catch (IllegalArgumentException e) {
    throw new IndexOutOfBoundsException();
  }
  ByteBuffer dst = buffer.isDirect()
      ? ByteBuffer.allocateDirect(length)
      : ByteBuffer.allocate(length);
  dst.put(src);
  dst.clear();
  return new ByteBufferBackedChannelBuffer(dst);
}

代码示例来源:origin: pentaho/pentaho-kettle

private void resizeByteBuffer( int newSize ) {
 ByteBuffer newBuffer = ByteBuffer.allocateDirect( newSize ); // Increase by 50%
 newBuffer.position( 0 );
 newBuffer.put( bb );
 bb = newBuffer;
}

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

@Override
public void queueInput(ByteBuffer inputBuffer) {
 Assertions.checkState(outputChannels != null);
 int position = inputBuffer.position();
 int limit = inputBuffer.limit();
 int frameCount = (limit - position) / (2 * channelCount);
 int outputSize = frameCount * outputChannels.length * 2;
 if (buffer.capacity() < outputSize) {
  buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
 } else {
  buffer.clear();
 }
 while (position < limit) {
  for (int channelIndex : outputChannels) {
   buffer.putShort(inputBuffer.getShort(position + 2 * channelIndex));
  }
  position += channelCount * 2;
 }
 inputBuffer.position(limit);
 buffer.flip();
 outputBuffer = buffer;
}

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

throw new IOException("Unable to read PKM file header.");
final ByteBuffer headerBuffer = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE)
  .order(ByteOrder.BIG_ENDIAN);
headerBuffer.put(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE).position(0);
if (!ETC2.isValid(headerBuffer)) {
  throw new IOException("Not a PKM file.");
format = ETC2.getETC2CompressionType(headerBuffer);
final int encodedSize = ETC2.getEncodedDataSize(width, height);
final ByteBuffer dataBuffer = ByteBuffer.allocateDirect(encodedSize).order(ByteOrder.BIG_ENDIAN);
for (int i = 0; i < encodedSize; ) {
  int chunkSize = Math.min(ioBuffer.length, encodedSize - i);
    throw new IOException("Unable to read PKM file data.");
  dataBuffer.put(ioBuffer, 0, chunkSize);
  i += chunkSize;

代码示例来源:origin: org.apache.spark/spark-core_2.11

public NioBufferedFileInputStream(File file, int bufferSizeInBytes) throws IOException {
 byteBuffer = ByteBuffer.allocateDirect(bufferSizeInBytes);
 fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
 byteBuffer.flip();
}

代码示例来源: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: apache/ignite

/** {@inheritDoc} */
@Override public ByteBuffer encode(GridNioSession ses, Object msg) throws IOException, IgniteCheckedException {
  byte[] msg0 = (byte[])msg;
  ByteBuffer res = directBuf ? ByteBuffer.allocateDirect(msg0.length + 4) : ByteBuffer.allocate(msg0.length + 4);
  //res.order(order); // TODO: GG-6460
  res.putInt(msg0.length);
  res.put(msg0);
  res.flip();
  return res;
}

代码示例来源:origin: daniulive/SmarterStreaming

private ByteBuffer deepCopy(ByteBuffer source) {
  int sourceP = source.position();
  int sourceL = source.limit();
  ByteBuffer target = ByteBuffer.allocateDirect(source.remaining());
  target.put(source);
  target.flip();
  source.position(sourceP);
  source.limit(sourceL);
  return target;
}

代码示例来源:origin: apache/kafka

public ByteBufferOutputStream(int initialCapacity, boolean directBuffer) {
  this(directBuffer ? ByteBuffer.allocateDirect(initialCapacity) : ByteBuffer.allocate(initialCapacity));
}

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

public static ByteBuffer newByteBuffer (int numBytes) {
  ByteBuffer buffer = ByteBuffer.allocateDirect(numBytes);
  buffer.order(ByteOrder.nativeOrder());
  return buffer;
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public ChannelBuffer copy(int index, int length) {
  ByteBuffer src;
  try {
    src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
  } catch (IllegalArgumentException e) {
    throw new IndexOutOfBoundsException();
  }
  ByteBuffer dst = buffer.isDirect()
      ? ByteBuffer.allocateDirect(length)
      : ByteBuffer.allocate(length);
  dst.put(src);
  dst.clear();
  return new ByteBufferBackedChannelBuffer(dst);
}

代码示例来源:origin: bumptech/glide

@NonNull
public static ByteBuffer fromStream(@NonNull InputStream stream) throws IOException {
 ByteArrayOutputStream outStream = new ByteArrayOutputStream(BUFFER_SIZE);
 byte[] buffer = BUFFER_REF.getAndSet(null);
 if (buffer == null) {
  buffer = new byte[BUFFER_SIZE];
 }
 int n;
 while ((n = stream.read(buffer)) >= 0) {
  outStream.write(buffer, 0, n);
 }
 BUFFER_REF.set(buffer);
 byte[] bytes = outStream.toByteArray();
 // Some resource decoders require a direct byte buffer. Prefer allocateDirect() over wrap()
 return (ByteBuffer) ByteBuffer.allocateDirect(bytes.length).put(bytes).position(0);
}

代码示例来源:origin: qunarcorp/qmq

AbstractLogVisitor(long from, FileChannel fileChannel, int workingSize) {
  this.fileChannel = fileChannel;
  this.visited = new AtomicLong(from);
  this.visitedSnapshot = new AtomicLong(from);
  buffer = ByteBuffer.allocateDirect(workingSize);
  try {
    fileChannel.read(buffer, visited.get());
    buffer.flip();
  } catch (IOException e) {
    LOGGER.error("load dispatch log visitor error", e);
  }
}

代码示例来源: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);
  }
}

相关文章

微信公众号

最新文章

更多