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

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

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

ByteBuffer.isReadOnly介绍

暂无

代码示例

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

@Override
public CharBuffer compact () {
  if (byteBuffer.isReadOnly()) {
    throw new ReadOnlyBufferException();
  }
  byteBuffer.limit(limit << 1);
  byteBuffer.position(position << 1);
  byteBuffer.compact();
  byteBuffer.clear();
  position = limit - position;
  limit = capacity;
  mark = UNSET_MARK;
  return this;
}

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

ReadOnlyByteBufferBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
  super(buffer.remaining());
  if (!buffer.isReadOnly()) {
    throw new IllegalArgumentException("must be a readonly buffer: " + StringUtil.simpleClassName(buffer));
  }
  this.allocator = allocator;
  this.buffer = buffer.slice().order(ByteOrder.BIG_ENDIAN);
  writerIndex(this.buffer.limit());
}

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

public void setBuffer(@Nonnull ByteBuffer buffer) {
  if (buffer.hasArray()) {
    this.buffer = buffer.array();
    this.position = buffer.arrayOffset() + buffer.position();
    this.end = this.position + buffer.remaining();
  } else if (buffer.isDirect() || buffer.isReadOnly()) {
    // TODO: FLINK-8585 handle readonly and other non array based buffers more efficiently without data copy
    this.buffer = new byte[buffer.remaining()];
    this.position = 0;
    this.end = this.buffer.length;
    buffer.get(this.buffer);
  } else {
    throw new IllegalArgumentException("The given buffer is neither an array-backed heap ByteBuffer, nor a direct ByteBuffer.");
  }
}

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

/** Simply creates an array containing only all the red components of the data. */
  public static float[] heightColorsToMap (final ByteBuffer data, final Pixmap.Format format, int width, int height) {
    final int bytesPerColor = (format == Format.RGB888 ? 3 : (format == Format.RGBA8888 ? 4 : 0));
    if (bytesPerColor == 0) throw new GdxRuntimeException("Unsupported format, should be either RGB8 or RGBA8");
    if (data.remaining() < (width * height * bytesPerColor)) throw new GdxRuntimeException("Incorrect map size");

    final int startPos = data.position();
    byte[] source = null;
    int sourceOffset = 0;
    if (data.hasArray() && !data.isReadOnly()) {
      source = data.array();
      sourceOffset = data.arrayOffset() + startPos;
    } else {
      source = new byte[width * height * bytesPerColor];
      data.get(source);
      data.position(startPos);
    }

    float[] dest = new float[width * height];
    for (int i = 0; i < dest.length; ++i) {
      int v = source[sourceOffset + i * bytesPerColor];
      v = v < 0 ? 256 + v : v;
      dest[i] = (float)v / 255f;
    }

    return dest;
  }
}

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

UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity, boolean doFree) {
  super(maxCapacity);
  if (alloc == null) {
    throw new NullPointerException("alloc");
  }
  if (initialBuffer == null) {
    throw new NullPointerException("initialBuffer");
  }
  if (!initialBuffer.isDirect()) {
    throw new IllegalArgumentException("initialBuffer is not a direct buffer.");
  }
  if (initialBuffer.isReadOnly()) {
    throw new IllegalArgumentException("initialBuffer is a read-only buffer.");
  }
  int initialCapacity = initialBuffer.remaining();
  if (initialCapacity > maxCapacity) {
    throw new IllegalArgumentException(String.format(
        "initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
  }
  this.alloc = alloc;
  doNotFree = !doFree;
  setByteBuffer(initialBuffer.order(ByteOrder.BIG_ENDIAN), false);
  writerIndex(initialCapacity);
}

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

public static void setData(ByteBuffer data) {
 if (!data.isDirect() || !data.isReadOnly()) {
  throw new BrotliRuntimeException("data must be a direct read-only byte buffer");
 }
 Dictionary.data = data;
}

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

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

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

public HyperLogLogCollector fold(@Nullable HyperLogLogCollector other)
 if (other == null || other.storageBuffer.remaining() == 0) {
  return this;
 if (storageBuffer.isReadOnly()) {
  convertToMutableByteBuffer();
 if (storageBuffer.remaining() != getNumBytesForDenseStorage()) {
  convertToDenseStorage();
  final ByteBuffer tmpBuffer = ByteBuffer.allocate(storageBuffer.remaining());
  tmpBuffer.put(storageBuffer.asReadOnlyBuffer());
  tmpBuffer.clear();
 final int otherPosition = otherBuffer.position();
  otherBuffer.position(other.getPayloadBytePosition());
  otherBuffer.position(otherPosition);

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

@Override
public IntBuffer compact () {
  if (byteBuffer.isReadOnly()) {
    throw new ReadOnlyBufferException();
  }
  byteBuffer.limit(limit << 2);
  byteBuffer.position(position << 2);
  byteBuffer.compact();
  byteBuffer.clear();
  position = limit - position;
  limit = capacity;
  mark = UNSET_MARK;
  return this;
}

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

ReadOnlyByteBufferBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
  super(buffer.remaining());
  if (!buffer.isReadOnly()) {
    throw new IllegalArgumentException("must be a readonly buffer: " + StringUtil.simpleClassName(buffer));
  }
  this.allocator = allocator;
  this.buffer = buffer.slice().order(ByteOrder.BIG_ENDIAN);
  writerIndex(this.buffer.limit());
}

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

/**
 * Creates a new direct buffer by wrapping the specified initial buffer.
 *
 * @param maxCapacity the maximum capacity of the underlying direct buffer
 */
protected UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
  super(maxCapacity);
  if (alloc == null) {
    throw new NullPointerException("alloc");
  }
  if (initialBuffer == null) {
    throw new NullPointerException("initialBuffer");
  }
  if (!initialBuffer.isDirect()) {
    throw new IllegalArgumentException("initialBuffer is not a direct buffer.");
  }
  if (initialBuffer.isReadOnly()) {
    throw new IllegalArgumentException("initialBuffer is a read-only buffer.");
  }
  int initialCapacity = initialBuffer.remaining();
  if (initialCapacity > maxCapacity) {
    throw new IllegalArgumentException(String.format(
        "initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
  }
  this.alloc = alloc;
  doNotFree = true;
  setByteBuffer(initialBuffer.slice().order(ByteOrder.BIG_ENDIAN));
  writerIndex(initialCapacity);
}

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

@Override
public int copyTo( int sourceOffset, ByteBuffer buf )
{
  if ( buf.getClass() == UnsafeUtil.directByteBufferClass && buf.isDirect() && !buf.isReadOnly() )
  {
    // We expect that the mutable direct byte buffer is implemented with a class that is distinct from the
    // non-mutable (read-only) and non-direct (on-heap) byte buffers. By comparing class object instances,
    // we also implicitly assume that the classes are loaded by the same class loader, which should be
    // trivially true in almost all practical cases.
    // If our expectations are not met, then the additional isDirect and !isReadOnly checks will send all
    // calls to the byte-wise-copy fallback.
    return copyToDirectByteBuffer( sourceOffset, buf );
  }
  return copyToByteBufferByteWise( sourceOffset, buf );
}

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

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

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

throw new IllegalArgumentException("dsts[" + i + "] is null");
if (dst.isReadOnly()) {
  throw new ReadOnlyBufferException();
capacity += dst.remaining();
  throw new IllegalArgumentException("srcs[" + i + "] is null");
len += src.remaining();
  for (;;) {
    ByteBuffer src = srcs[srcsOffset];
    int remaining = src.remaining();
    final ByteBuf bioWriteCopyBuf;
    int pendingEncryptedBytes;
        packetLength -= localBytesConsumed;
        pendingEncryptedBytes -= localBytesConsumed;
        src.position(src.position() + localBytesConsumed);

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

@Override
public CharBuffer compact () {
  if (byteBuffer.isReadOnly()) {
    throw new ReadOnlyBufferException();
  }
  byteBuffer.limit(limit << 1);
  byteBuffer.position(position << 1);
  byteBuffer.compact();
  byteBuffer.clear();
  position = limit - position;
  limit = capacity;
  mark = UNSET_MARK;
  return this;
}

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

ReadOnlyByteBufferBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
  super(buffer.remaining());
  if (!buffer.isReadOnly()) {
    throw new IllegalArgumentException("must be a readonly buffer: " + StringUtil.simpleClassName(buffer));
  }
  this.allocator = allocator;
  this.buffer = buffer.slice().order(ByteOrder.BIG_ENDIAN);
  writerIndex(this.buffer.limit());
}

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

UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity, boolean doFree) {
  super(maxCapacity);
  if (alloc == null) {
    throw new NullPointerException("alloc");
  }
  if (initialBuffer == null) {
    throw new NullPointerException("initialBuffer");
  }
  if (!initialBuffer.isDirect()) {
    throw new IllegalArgumentException("initialBuffer is not a direct buffer.");
  }
  if (initialBuffer.isReadOnly()) {
    throw new IllegalArgumentException("initialBuffer is a read-only buffer.");
  }
  int initialCapacity = initialBuffer.remaining();
  if (initialCapacity > maxCapacity) {
    throw new IllegalArgumentException(String.format(
        "initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
  }
  this.alloc = alloc;
  doNotFree = !doFree;
  setByteBuffer(initialBuffer.order(ByteOrder.BIG_ENDIAN), false);
  writerIndex(initialCapacity);
}

代码示例来源:origin: com.google.protobuf/protobuf-java

/** Create a new {@code CodedOutputStream} that writes to the given {@link ByteBuffer}. */
public static CodedOutputStream newInstance(ByteBuffer buffer) {
 if (buffer.hasArray()) {
  return new HeapNioEncoder(buffer);
 }
 if (buffer.isDirect() && !buffer.isReadOnly()) {
  return UnsafeDirectNioEncoder.isSupported()
    ? newUnsafeInstance(buffer)
    : newSafeInstance(buffer);
 }
 throw new IllegalArgumentException("ByteBuffer is read-only");
}

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

static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer dst) {
  buf.checkIndex(index, dst.remaining());
  if (dst.remaining() == 0) {
    return;
  }
  if (dst.isDirect()) {
    if (dst.isReadOnly()) {
      // We need to check if dst is ready-only so we not write something in it by using Unsafe.
      throw new ReadOnlyBufferException();
    }
    // Copy to direct memory
    long dstAddress = PlatformDependent.directBufferAddress(dst);
    PlatformDependent.copyMemory(addr, dstAddress + dst.position(), dst.remaining());
    dst.position(dst.position() + dst.remaining());
  } else if (dst.hasArray()) {
    // Copy to array
    PlatformDependent.copyMemory(addr, dst.array(), dst.arrayOffset() + dst.position(), dst.remaining());
    dst.position(dst.position() + dst.remaining());
  } else  {
    dst.put(buf.nioBuffer());
  }
}

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

throw new IllegalArgumentException("dsts[" + i + "] is null");
if (dst.isReadOnly()) {
  throw new ReadOnlyBufferException();
capacity += dst.remaining();
  throw new IllegalArgumentException("srcs[" + i + "] is null");
len += src.remaining();
  for (;;) {
    ByteBuffer src = srcs[srcsOffset];
    int remaining = src.remaining();
    final ByteBuf bioWriteCopyBuf;
    int pendingEncryptedBytes;
        packetLength -= localBytesConsumed;
        pendingEncryptedBytes -= localBytesConsumed;
        src.position(src.position() + localBytesConsumed);

相关文章