@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; }
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()); }
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."); } }
/** 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; } }
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); }
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; }
@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; }
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);
@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; }
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()); }
/** * 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); }
@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 ); }
@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; }
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);
@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; }
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()); }
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); }
/** 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"); }
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()); } }
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);