Java Code Examples for io.netty.buffer.ByteBuf#hasMemoryAddress()

The following examples show how to use io.netty.buffer.ByteBuf#hasMemoryAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompressionCodecZstd.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf source) {
    int uncompressedLength = source.readableBytes();
    int maxLength = (int) ZSTD_COMPRESSOR.maxCompressedLength(uncompressedLength);

    ByteBuf target = PulsarByteBufAllocator.DEFAULT.buffer(maxLength, maxLength);
    int compressedLength;

    if (source.hasMemoryAddress() && target.hasMemoryAddress()) {
        compressedLength = ZStdRawCompressor.compress(
                source.memoryAddress() + source.readerIndex(),
                source.memoryAddress() + source.writerIndex(),
                target.memoryAddress() + target.writerIndex(),
                target.memoryAddress() + target.writerIndex() + maxLength,
                ZSTD_COMPRESSION_LEVEL);
    } else {
        ByteBuffer sourceNio = source.nioBuffer(source.readerIndex(), source.readableBytes());
        ByteBuffer targetNio = target.nioBuffer(0, maxLength);

        ZSTD_COMPRESSOR.compress(sourceNio, targetNio);
        compressedLength = targetNio.position();
    }

    target.writerIndex(compressedLength);
    return target;
}
 
Example 2
Source File: MappedFile.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a sequence of bytes to this file from the given buffer.
 * <p>
 * <p> Bytes are written starting at this file's current position,
 */
public void write(ByteBuf src, int srcStart, int srcLength) throws IOException {
   final int nextPosition = this.position + srcLength;
   checkCapacity(nextPosition);
   final long destAddress = this.address + this.position;
   if (src.hasMemoryAddress()) {
      final long srcAddress = src.memoryAddress() + srcStart;
      PlatformDependent.copyMemory(srcAddress, destAddress, srcLength);
   } else if (src.hasArray()) {
      final byte[] srcArray = src.array();
      PlatformDependent.copyMemory(srcArray, srcStart, destAddress, srcLength);
   } else {
      throw new IllegalArgumentException("unsupported byte buffer");
   }
   rawMovePositionAndLength(nextPosition);
}
 
Example 3
Source File: ByteUtil.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@code true} if  the {@link SimpleString} encoded content into {@code bytes} is equals to {@code s},
 * {@code false} otherwise.
 * <p>
 * It assumes that the {@code bytes} content is read using {@link SimpleString#readSimpleString(ByteBuf, int)} ie starting right after the
 * length field.
 */
public static boolean equals(final byte[] bytes, final ByteBuf byteBuf, final int offset, final int length) {
   if (bytes.length != length)
      return false;
   if (PlatformDependent.isUnaligned() && PlatformDependent.hasUnsafe()) {
      if ((offset + length) > byteBuf.writerIndex()) {
         throw new IndexOutOfBoundsException();
      }
      if (byteBuf.hasArray()) {
         return equals(bytes, byteBuf.array(), byteBuf.arrayOffset() + offset, length);
      } else if (byteBuf.hasMemoryAddress()) {
         return equalsOffHeap(bytes, byteBuf.memoryAddress(), offset, length);
      }
   }
   return equalsOnHeap(bytes, byteBuf, offset, length);
}
 
Example 4
Source File: AbstractEpollChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the given {@link ByteBuf} and return the amount.
 */
protected final int doReadBytes(ByteBuf byteBuf) throws Exception {
    int writerIndex = byteBuf.writerIndex();
    int localReadAmount;
    if (byteBuf.hasMemoryAddress()) {
        localReadAmount = Native.readAddress(
                fileDescriptor.intValue(), byteBuf.memoryAddress(), writerIndex, byteBuf.capacity());
    } else {
        ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes());
        localReadAmount = Native.read(fileDescriptor.intValue(), buf, buf.position(), buf.limit());
    }
    if (localReadAmount > 0) {
        byteBuf.writerIndex(writerIndex + localReadAmount);
    }
    return localReadAmount;
}
 
Example 5
Source File: AbstractEpollStreamChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}.
 * @param buf           the {@link ByteBuf} from which the bytes should be written
 */
private boolean writeBytes(ChannelOutboundBuffer in, ByteBuf buf, int writeSpinCount) throws Exception {
    int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        in.remove();
        return true;
    }

    if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) {
        int writtenBytes = doWriteBytes(buf, writeSpinCount);
        in.removeBytes(writtenBytes);
        return writtenBytes == readableBytes;
    } else {
        ByteBuffer[] nioBuffers = buf.nioBuffers();
        return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, writeSpinCount);
    }
}
 
Example 6
Source File: CompressionCodecZstdJNI.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PulsarByteBufAllocator.DEFAULT.directBuffer(uncompressedLength, uncompressedLength);

    if (encoded.hasMemoryAddress()) {
        Zstd.decompressUnsafe(uncompressed.memoryAddress(), uncompressedLength,
                encoded.memoryAddress() + encoded.readerIndex(),
                encoded.readableBytes());
    } else {
        ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength);
        ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes());

        Zstd.decompress(uncompressedNio, encodedNio);
    }

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}
 
Example 7
Source File: CompressionCodecZstdJNI.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf source) {
    int uncompressedLength = source.readableBytes();
    int maxLength = (int) Zstd.compressBound(uncompressedLength);

    ByteBuf target = PulsarByteBufAllocator.DEFAULT.directBuffer(maxLength, maxLength);
    int compressedLength;

    if (source.hasMemoryAddress()) {
        compressedLength = (int) Zstd.compressUnsafe(target.memoryAddress(), maxLength,
                source.memoryAddress() + source.readerIndex(),
                uncompressedLength, ZSTD_COMPRESSION_LEVEL);
    } else {
        ByteBuffer sourceNio = source.nioBuffer(source.readerIndex(), source.readableBytes());
        ByteBuffer targetNio = target.nioBuffer(0, maxLength);

        compressedLength = Zstd.compress(targetNio, sourceNio, ZSTD_COMPRESSION_LEVEL);
    }

    target.writerIndex(compressedLength);
    return target;
}
 
Example 8
Source File: CompressionCodecZstd.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PulsarByteBufAllocator.DEFAULT.buffer(uncompressedLength, uncompressedLength);

    if (encoded.hasMemoryAddress() && uncompressed.hasMemoryAddress()) {
        ZSTD_RAW_DECOMPRESSOR.get().decompress(
                null,
                encoded.memoryAddress() + encoded.readerIndex(),
                encoded.memoryAddress() + encoded.writerIndex(),
                null,
                uncompressed.memoryAddress() + uncompressed.writerIndex(),
                uncompressed.memoryAddress() + uncompressed.writerIndex() + uncompressedLength);
    } else {
        ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength);
        ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes());

        ZSTD_DECOMPRESSOR.get().decompress(encodedNio, uncompressedNio);
    }

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}
 
Example 9
Source File: CompressionCodecLZ4.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PulsarByteBufAllocator.DEFAULT.buffer(uncompressedLength, uncompressedLength);

    if (encoded.hasMemoryAddress() && uncompressed.hasMemoryAddress()) {
        Lz4RawDecompressor.decompress(null, encoded.memoryAddress() + encoded.readerIndex(),
                encoded.memoryAddress() + encoded.writerIndex(), null, uncompressed.memoryAddress(),
                uncompressed.memoryAddress() + uncompressedLength);
    } else {
        ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength);
        ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes());

        LZ4_DECOMPRESSOR.get().decompress(encodedNio, uncompressedNio);
    }

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}
 
Example 10
Source File: CompressionCodecSnappy.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PulsarByteBufAllocator.DEFAULT.buffer(uncompressedLength, uncompressedLength);

    if (encoded.hasMemoryAddress() && uncompressed.hasMemoryAddress()) {
        SnappyRawDecompressor.decompress(
                null,
                encoded.memoryAddress() + encoded.readerIndex(),
                encoded.memoryAddress() + encoded.writerIndex(),
                null, uncompressed.memoryAddress(),
                uncompressed.memoryAddress() + uncompressedLength);
    } else {
        ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength);
        ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes());

        SNAPPY_DECOMPRESSOR.get().decompress(encodedNio, uncompressedNio);
    }

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}
 
Example 11
Source File: AbstractEpollChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the given {@link ByteBuf} and return the amount.将字节读入给定的ByteBuf并返回数量。
 */
protected final int doReadBytes(ByteBuf byteBuf) throws Exception {
    int writerIndex = byteBuf.writerIndex();
    int localReadAmount;
    unsafe().recvBufAllocHandle().attemptedBytesRead(byteBuf.writableBytes());
    if (byteBuf.hasMemoryAddress()) {
        localReadAmount = socket.readAddress(byteBuf.memoryAddress(), writerIndex, byteBuf.capacity());
    } else {
        ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes());
        localReadAmount = socket.read(buf, buf.position(), buf.limit());
    }
    if (localReadAmount > 0) {
        byteBuf.writerIndex(writerIndex + localReadAmount);
    }
    return localReadAmount;
}
 
Example 12
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}.
 * @param in the collection which contains objects to write.
 * @param buf the {@link ByteBuf} from which the bytes should be written
 * @return The value that should be decremented from the write quantum which starts at
 * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows:
 * <ul>
 *     <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content)
 *     is encountered</li>
 *     <li>1 - if a single call to write data was made to the OS</li>
 *     <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but
 *     no data was accepted</li>
 * </ul>
 */
private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception {
    int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        in.remove();
        return 0;
    }

    if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) {
        return doWriteBytes(in, buf);
    } else {
        ByteBuffer[] nioBuffers = buf.nioBuffers();
        return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes,
                config().getMaxBytesPerGatheringWrite());
    }
}
 
Example 13
Source File: AbstractKQueueStreamChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}.
 * @param in the collection which contains objects to write.
 * @param buf the {@link ByteBuf} from which the bytes should be written
 * @return The value that should be decremented from the write quantum which starts at
 * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows:
 * <ul>
 *     <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content)
 *     is encountered</li>
 *     <li>1 - if a single call to write data was made to the OS</li>
 *     <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no
 *     data was accepted</li>
 * </ul>
 */
private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception {
    int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        in.remove();
        return 0;
    }

    if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) {
        return doWriteBytes(in, buf);
    } else {
        ByteBuffer[] nioBuffers = buf.nioBuffers();
        return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes,
                config().getMaxBytesPerGatheringWrite());
    }
}
 
Example 14
Source File: AbstractKQueueChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the given {@link ByteBuf} and return the amount.
 */
protected final int doReadBytes(ByteBuf byteBuf) throws Exception {
    int writerIndex = byteBuf.writerIndex();
    int localReadAmount;
    unsafe().recvBufAllocHandle().attemptedBytesRead(byteBuf.writableBytes());
    if (byteBuf.hasMemoryAddress()) {
        localReadAmount = socket.readAddress(byteBuf.memoryAddress(), writerIndex, byteBuf.capacity());
    } else {
        ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes());
        localReadAmount = socket.read(buf, buf.position(), buf.limit());
    }
    if (localReadAmount > 0) {
        byteBuf.writerIndex(writerIndex + localReadAmount);
    }
    return localReadAmount;
}
 
Example 15
Source File: NumberUtil.java    From tajo with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the byte buffer argument as if it was an int value and returns the
 * result. Throws NumberFormatException if the byte array does not represent an
 * int quantity. The second argument specifies the radix to use when parsing
 * the value.
 *
 * @param radix the base to use for conversion.
 * @return the value represented by the argument
 * @throws NumberFormatException if the argument could not be parsed as an int quantity.
 */
public static int parseInt(ByteBuf bytes, int start, int length, int radix) {
  if (bytes == null) {
    throw new NumberFormatException("String is null");
  }

  if (!bytes.hasMemoryAddress()) {
    return parseInt(bytes.array(), start, length);
  }

  if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
    throw new NumberFormatException("Invalid radix: " + radix);
  }
  if (length == 0 || bytes.writerIndex() < start + length) {
    throw new NumberFormatException("Empty string or Invalid buffer!");
  }

  long memoryAddress = bytes.memoryAddress();

  int offset = start;
  boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-';
  if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') {
    offset++;
    if (length == 1) {
      throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset()));
    }
  }

  return parseIntInternal(bytes, memoryAddress, start, length, offset, radix, negative);
}
 
Example 16
Source File: CompressionCodecSnappy.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf source) {
    int uncompressedLength = source.readableBytes();
    int maxLength = SnappyRawCompressor.maxCompressedLength(uncompressedLength);

    ByteBuf target = PulsarByteBufAllocator.DEFAULT.buffer(maxLength, maxLength);
    int compressedLength;

    if (source.hasMemoryAddress() && target.hasMemoryAddress()) {
        compressedLength = SnappyRawCompressor.compress(
                null,
                source.memoryAddress() + source.readerIndex(),
                source.memoryAddress() + source.writerIndex(),
                null,
                target.memoryAddress(),
                target.memoryAddress() + maxLength,
                SNAPPY_TABLE.get());
    } else {
        ByteBuffer sourceNio = source.nioBuffer(source.readerIndex(), source.readableBytes());
        ByteBuffer targetNio = target.nioBuffer(0, maxLength);

        SNAPPY_COMPRESSOR.get().compress(sourceNio, targetNio);
        compressedLength = targetNio.position();
    }

    target.writerIndex(compressedLength);
    return target;
}
 
Example 17
Source File: MoreByteBufUtils.java    From Velocity with MIT License 5 votes vote down vote up
/**
 * Ensures the {@code buf} will work with the specified {@code nativeStuff}. After this function
 * is called, you should decrement the reference count on the {@code buf} with
 * {@link ByteBuf#release()}.
 *
 * @param alloc the {@link ByteBufAllocator} to use
 * @param nativeStuff the native we are working with
 * @param buf the buffer we are working with
 * @return a buffer compatible with the native
 */
public static ByteBuf ensureCompatible(ByteBufAllocator alloc, Native nativeStuff, ByteBuf buf) {
  if (!nativeStuff.isNative() || buf.hasMemoryAddress()) {
    // Will always work in either case. JNI code demands a memory address, and if we have a Java
    // fallback, it uses byte arrays in all cases.
    return buf.retain();
  }

  // It's not, so we must make a direct copy.
  ByteBuf newBuf = alloc.directBuffer(buf.readableBytes());
  newBuf.writeBytes(buf);
  return newBuf;
}
 
Example 18
Source File: OpenSslEngine.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static long memoryAddress(ByteBuf buf) {
    if (buf.hasMemoryAddress()) {
        return buf.memoryAddress();
    } else {
        return Buffer.address(buf.nioBuffer());
    }
}
 
Example 19
Source File: AbstractByteBufPool.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Batch hash code implementation that works at its best if {@code bytes}
 * contains a {@link org.apache.activemq.artemis.api.core.SimpleString} encoded.
 */
private static int hashCode(final ByteBuf bytes, final int offset, final int length) {
   if (PlatformDependent.isUnaligned() && PlatformDependent.hasUnsafe()) {
      //if the platform allows it, the hash code could be computed without bounds checking
      if (bytes.hasArray()) {
         return onHeapHashCode(bytes.array(), bytes.arrayOffset() + offset, length);
      } else if (bytes.hasMemoryAddress()) {
         return offHeapHashCode(bytes.memoryAddress(), offset, length);
      }
   }
   return byteBufHashCode(bytes, offset, length);
}
 
Example 20
Source File: UnixChannelUtil.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
static boolean isBufferCopyNeededForWrite(ByteBuf byteBuf, int iovMax) {
    return !byteBuf.hasMemoryAddress() && (!byteBuf.isDirect() || byteBuf.nioBufferCount() > iovMax);
}