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

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

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

ByteBuffer.hasArray介绍

[英]Indicates whether this buffer is based on a byte array and provides read/write access.
[中]指示此缓冲区是否基于字节数组并提供读/写访问。

代码示例

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

@Override
public Hasher putBytes(ByteBuffer b) {
 if (b.hasArray()) {
  putBytes(b.array(), b.arrayOffset() + b.position(), b.remaining());
  b.position(b.limit());
 } else {
  for (int remaining = b.remaining(); remaining > 0; remaining--) {
   putByte(b.get());
  }
 }
 return this;
}

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

@Nullable
private static SafeArray getSafeArray(@NonNull ByteBuffer byteBuffer) {
 if (!byteBuffer.isReadOnly() && byteBuffer.hasArray()) {
  return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
 }
 return null;
}

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

@Override
public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
  if (nioBuffer.hasArray()) {
    return ChannelBuffers.wrappedBuffer(nioBuffer);
  }
  ChannelBuffer buf = getBuffer(nioBuffer.remaining());
  int pos = nioBuffer.position();
  buf.writeBytes(nioBuffer);
  nioBuffer.position(pos);
  return buf;
}

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

private static ClassNode classNode( ByteBuffer bytecode )
{
  byte[] bytes;
  if ( bytecode.hasArray() )
  {
    bytes = bytecode.array();
  }
  else
  {
    bytes = new byte[bytecode.limit()];
    bytecode.get( bytes );
  }
  ClassNode classNode = new ClassNode();
  new ClassReader( bytes ).accept( new CheckClassAdapter( classNode, false ), SKIP_DEBUG );
  return classNode;
}

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

/** Updates this hasher with bytes from the given buffer. */
protected void update(ByteBuffer b) {
 if (b.hasArray()) {
  update(b.array(), b.arrayOffset() + b.position(), b.remaining());
  b.position(b.limit());
 } else {
  for (int remaining = b.remaining(); remaining > 0; remaining--) {
   update(b.get());
  }
 }
}

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

@Override
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
  if (dst instanceof ByteBufferBackedChannelBuffer) {
    ByteBufferBackedChannelBuffer bbdst = (ByteBufferBackedChannelBuffer) dst;
    ByteBuffer data = bbdst.buffer.duplicate();
    data.limit(dstIndex + length).position(dstIndex);
    getBytes(index, data);
  } else if (buffer.hasArray()) {
    dst.setBytes(dstIndex, buffer.array(), index + buffer.arrayOffset(), length);
  } else {
    dst.setBytes(dstIndex, this, index, length);
  }
}

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

@Override
public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
  if (nioBuffer.hasArray()) {
    return ChannelBuffers.wrappedBuffer(nioBuffer);
  }
  ChannelBuffer buf = getBuffer(nioBuffer.remaining());
  int pos = nioBuffer.position();
  buf.writeBytes(nioBuffer);
  nioBuffer.position(pos);
  return buf;
}

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

@Override
public void getBytes(int index, OutputStream out, int length) throws IOException {
  if (length == 0) {
    return;
  }
  if (buffer.hasArray()) {
    out.write(
        buffer.array(),
        index + buffer.arrayOffset(),
        length);
  } else {
    byte[] tmp = new byte[length];
    ((ByteBuffer) buffer.duplicate().position(index)).get(tmp);
    out.write(tmp);
  }
}

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

@Override
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
  if (dst instanceof ByteBufferBackedChannelBuffer) {
    ByteBufferBackedChannelBuffer bbdst = (ByteBufferBackedChannelBuffer) dst;
    ByteBuffer data = bbdst.buffer.duplicate();
    data.limit(dstIndex + length).position(dstIndex);
    getBytes(index, data);
  } else if (buffer.hasArray()) {
    dst.setBytes(dstIndex, buffer.array(), index + buffer.arrayOffset(), length);
  } else {
    dst.setBytes(dstIndex, this, index, length);
  }
}

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

@Override
public void getBytes(int index, OutputStream out, int length) throws IOException {
  if (length == 0) {
    return;
  }
  if (buffer.hasArray()) {
    out.write(
        buffer.array(),
        index + buffer.arrayOffset(),
        length);
  } else {
    byte[] tmp = new byte[length];
    ((ByteBuffer) buffer.duplicate().position(index)).get(tmp);
    out.write(tmp);
  }
}

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

@Override
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
  if (src instanceof ByteBufferBackedChannelBuffer) {
    ByteBufferBackedChannelBuffer bbsrc = (ByteBufferBackedChannelBuffer) src;
    ByteBuffer data = bbsrc.buffer.duplicate();
    data.limit(srcIndex + length).position(srcIndex);
    setBytes(index, data);
  } else if (buffer.hasArray()) {
    src.getBytes(srcIndex, buffer.array(), index + buffer.arrayOffset(), length);
  } else {
    src.getBytes(srcIndex, this, index, length);
  }
}

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

public static void update(Checksum checksum, ByteBuffer buffer, int offset, int length) {
  if (buffer.hasArray()) {
    checksum.update(buffer.array(), buffer.position() + buffer.arrayOffset() + offset, length);
  } else {
    int start = buffer.position() + offset;
    for (int i = start; i < start + length; i++)
      checksum.update(buffer.get(i));
  }
}

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

@Override
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
  if (src instanceof ByteBufferBackedChannelBuffer) {
    ByteBufferBackedChannelBuffer bbsrc = (ByteBufferBackedChannelBuffer) src;
    ByteBuffer data = bbsrc.buffer.duplicate();
    data.limit(srcIndex + length).position(srcIndex);
    setBytes(index, data);
  } else if (buffer.hasArray()) {
    src.getBytes(srcIndex, buffer.array(), index + buffer.arrayOffset(), length);
  } else {
    src.getBytes(srcIndex, this, index, length);
  }
}

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

public static ChannelBuffer wrappedBuffer(ByteBuffer buffer) {
  if (!buffer.hasRemaining()) {
    return EMPTY_BUFFER;
  }
  if (buffer.hasArray()) {
    return wrappedBuffer(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
  } else {
    return new ByteBufferBackedChannelBuffer(buffer);
  }
}

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

@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
  ensureAccessible();
  if (buffer.hasArray()) {
    return in.read(buffer.array(), buffer.arrayOffset() + index, length);
  } else {
    byte[] tmp = ByteBufUtil.threadLocalTempArray(length);
    int readBytes = in.read(tmp, 0, length);
    if (readBytes <= 0) {
      return readBytes;
    }
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index);
    tmpBuf.put(tmp, 0, readBytes);
    return readBytes;
  }
}

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

public static ChannelBuffer wrappedBuffer(ByteBuffer buffer) {
  if (!buffer.hasRemaining()) {
    return EMPTY_BUFFER;
  }
  if (buffer.hasArray()) {
    return wrappedBuffer(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
  } else {
    return new ByteBufferBackedChannelBuffer(buffer);
  }
}

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

public byte[] serialize(String topic, ByteBuffer data) {
  if (data == null)
    return null;
  data.rewind();
  if (data.hasArray()) {
    byte[] arr = data.array();
    if (data.arrayOffset() == 0 && arr.length == data.remaining()) {
      return arr;
    }
  }
  byte[] ret = new byte[data.remaining()];
  data.get(ret, 0, ret.length);
  data.rewind();
  return ret;
}

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

/**
 * Read a buffer into a Byte array for the given offset and length
 */
public static byte[] readBytes(ByteBuffer buffer, int offset, int length) {
  byte[] dest = new byte[length];
  if (buffer.hasArray()) {
    System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, length);
  } else {
    buffer.mark();
    buffer.position(offset);
    buffer.get(dest, 0, length);
    buffer.reset();
  }
  return dest;
}

代码示例来源:origin: thinkaurelius/titan

@Override
  public void copy(ByteBuffer data, byte[] dest, int destOffset) {
    if (data.hasArray()) {
      System.arraycopy(data.array(),data.arrayOffset()+data.position(),dest,destOffset,data.remaining());
    } else {
      data.mark();
      data.get(dest,destOffset,data.remaining());
      data.reset();
    }
  }
}

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

/**
 * Write the contents of a buffer to an output stream. The bytes are copied from the current position
 * in the buffer.
 * @param out The output to write to
 * @param buffer The buffer to write from
 * @param length The number of bytes to write
 * @throws IOException For any errors writing to the output
 */
public static void writeTo(DataOutput out, ByteBuffer buffer, int length) throws IOException {
  if (buffer.hasArray()) {
    out.write(buffer.array(), buffer.position() + buffer.arrayOffset(), length);
  } else {
    int pos = buffer.position();
    for (int i = pos; i < length + pos; i++)
      out.writeByte(buffer.get(i));
  }
}

相关文章