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

The following examples show how to use io.netty.buffer.ByteBuf#memoryAddress() . 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: NativeVelocityCipher.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void process(ByteBuf source, ByteBuf destination) throws ShortBufferException {
  ensureNotDisposed();
  source.memoryAddress();
  destination.memoryAddress();

  // The exact amount we read in is also the amount we write out.
  int len = source.readableBytes();
  destination.ensureWritable(len);

  impl.process(ctx, source.memoryAddress() + source.readerIndex(), len,
      destination.memoryAddress() + destination.writerIndex(), encrypt);

  source.skipBytes(len);
  destination.writerIndex(destination.writerIndex() + len);
}
 
Example 2
Source File: NativeVelocityCipher.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public ByteBuf process(ChannelHandlerContext ctx, ByteBuf source) throws ShortBufferException {
  ensureNotDisposed();
  source.memoryAddress(); // sanity check

  int len = source.readableBytes();
  ByteBuf out = ctx.alloc().directBuffer(len);

  try {
    impl.process(this.ctx, source.memoryAddress() + source.readerIndex(), len,
        out.memoryAddress(), encrypt);
    source.skipBytes(len);
    out.writerIndex(len);
    return out;
  } catch (Exception e) {
    out.release();
    throw e;
  }
}
 
Example 3
Source File: NativeVelocityCompressor.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void inflate(ByteBuf source, ByteBuf destination, int max) throws DataFormatException {
  ensureNotDisposed();
  source.memoryAddress();
  destination.memoryAddress();

  while (!inflate.finished && source.isReadable()) {
    if (!destination.isWritable()) {
      ensureMaxSize(destination, max);
      destination.ensureWritable(ZLIB_BUFFER_SIZE);
    }
    int produced = inflate.process(inflateCtx, source.memoryAddress() + source.readerIndex(),
        source.readableBytes(), destination.memoryAddress() + destination.writerIndex(),
        destination.writableBytes());
    source.readerIndex(source.readerIndex() + inflate.consumed);
    destination.writerIndex(destination.writerIndex() + produced);
  }

  inflate.reset(inflateCtx);
  inflate.consumed = 0;
  inflate.finished = false;
}
 
Example 4
Source File: NativeVelocityCompressor.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void deflate(ByteBuf source, ByteBuf destination) throws DataFormatException {
  ensureNotDisposed();
  source.memoryAddress();
  destination.memoryAddress();

  while (!deflate.finished) {
    if (!destination.isWritable()) {
      destination.ensureWritable(ZLIB_BUFFER_SIZE);
    }
    int produced = deflate.process(deflateCtx, source.memoryAddress() + source.readerIndex(),
        source.readableBytes(),
        destination.memoryAddress() + destination.writerIndex(), destination.writableBytes(),
        true);
    source.readerIndex(source.readerIndex() + deflate.consumed);
    destination.writerIndex(destination.writerIndex() + produced);
  }

  deflate.reset(deflateCtx);
  deflate.consumed = 0;
  deflate.finished = false;
}
 
Example 5
Source File: IovArray.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Try to add the given {@link ByteBuf}. Returns {@code true} on success,
 * {@code false} otherwise.
 */
boolean add(ByteBuf buf) {
    if (count == Native.IOV_MAX) {
        // No more room!
        return false;
    }

    final int len = buf.readableBytes();
    if (len == 0) {
        // No need to add an empty buffer.
        // We return true here because we want ChannelOutboundBuffer.forEachFlushedMessage() to continue
        // fetching the next buffers.
        return true;
    }

    final long addr = buf.memoryAddress();
    final int offset = buf.readerIndex();
    add(addr, offset, len);
    return true;
}
 
Example 6
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 7
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 8
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 9
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 long value and returns the
 * result. Throws NumberFormatException if the string does not represent an
 * long quantity. The second argument specifies the radix to use when parsing
 * the value.
 *
 * @param bytes  the string byte buffer
 * @param start
 * @param length a UTF-8 encoded string representation of a long quantity.
 * @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 long quantity.
 */
public static long parseLong(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 parseLongInternal(bytes, memoryAddress, start, length, offset, radix, negative);
}
 
Example 10
Source File: BindValueUtil.java    From antsdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static long read(Heap heap, ByteBuf buf, int type) {
	long pValue = 0;
    switch (type & 0xff) {
    case Fields.FIELD_TYPE_TINY:
    	pValue = Int4.allocSet(heap, buf.readByte());
        break;
    case Fields.FIELD_TYPE_SHORT:
    	pValue = Int4.allocSet(heap, (short)BufferUtils.readInt(buf));
        break;
    case Fields.FIELD_TYPE_LONG:
    	pValue = Int4.allocSet(heap, BufferUtils.readLong(buf));
        break;
    case Fields.FIELD_TYPE_LONGLONG:
    	pValue = Int8.allocSet(heap, BufferUtils.readLongLong(buf));
        break;
    case Fields.FIELD_TYPE_FLOAT:
    	pValue = Float4.allocSet(heap, BufferUtils.readFloat(buf));
        break;
    case Fields.FIELD_TYPE_DOUBLE:
    	pValue = Float8.allocSet(heap, BufferUtils.readDouble(buf));
        break;
    case Fields.FIELD_TYPE_TIME:
    case Fields.FIELD_TYPE_TIME2:
    	pValue = FishTime.allocSet(heap, BufferUtils.readTime(buf));
        break;
    case Fields.FIELD_TYPE_DATE:
    	pValue = FishDate.allocSet(heap, BufferUtils.readDate(buf));
        break;
    case Fields.FIELD_TYPE_DATETIME:
    case Fields.FIELD_TYPE_TIMESTAMP:
    case Fields.FIELD_TYPE_DATETIME2:
    case Fields.FIELD_TYPE_TIMESTAMP2:
    	pValue = FishTimestamp.allocSet(heap, BufferUtils.readTimestamp(buf));
        break;
    case Fields.FIELD_TYPE_VAR_STRING:
    case Fields.FIELD_TYPE_STRING:
    case Fields.FIELD_TYPE_VARCHAR:
    	int len = (int)BufferUtils.readLength(buf);
    	long pData = buf.memoryAddress() + buf.readerIndex();
    	pValue = FishUtf8.allocSet(heap, pData, len);
    	buf.readerIndex(buf.readerIndex() + len);
        break;
    case Fields.FIELD_TYPE_DECIMAL:
    case Fields.FIELD_TYPE_NEW_DECIMAL:
    	pValue = FishNumber.allocSet(heap, BufferUtils.readBigDecimal(buf));
        break;
    default:
        throw new IllegalArgumentException("bindValue error,unsupported type:" + type);
    }
    return pValue;
}
 
Example 11
Source File: PacketResultSetColumn.java    From antsdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
public PacketResultSetColumn(ByteBuf buf) {
    this(buf.memoryAddress(), buf.readableBytes());
}
 
Example 12
Source File: Packet.java    From antsdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static Packet createPacket(PacketType type, ByteBuf buf) {
    long addr = buf.memoryAddress();
    int length = buf.readableBytes();
    return createPacket(buf.nioBuffer(), type, addr, length);
}