Example usage for io.netty.util.internal PlatformDependent copyMemory

List of usage examples for io.netty.util.internal PlatformDependent copyMemory

Introduction

In this page you can find the example usage for io.netty.util.internal PlatformDependent copyMemory.

Prototype

public static void copyMemory(long srcAddr, long dstAddr, long length) 

Source Link

Usage

From source file:org.apache.activemq.artemis.core.io.mapped.MappedFile.java

License:Apache License

/**
 * Reads a sequence of bytes from this file into the given buffer.
 * <p>/*w ww  .j  a v a2  s .c o m*/
 * <p> Bytes are read starting at this file's current position, and
 * then the position is updated with the number of bytes actually read.
 */
public int read(ByteBuffer dst, int dstStart, int dstLength) throws IOException {
    final int remaining = this.length - this.position;
    final int read = Math.min(remaining, dstLength);
    final long srcAddress = this.address + this.position;
    if (dst.isDirect()) {
        final long dstAddress = PlatformDependent.directBufferAddress(dst) + dstStart;
        PlatformDependent.copyMemory(srcAddress, dstAddress, read);
    } else {
        final byte[] dstArray = dst.array();
        PlatformDependent.copyMemory(srcAddress, dstArray, dstStart, read);
    }
    this.position += read;
    return read;
}

From source file:org.apache.activemq.artemis.core.io.mapped.MappedFile.java

License:Apache License

/**
 * Writes a sequence of bytes to this file from the given buffer.
 * <p>// w ww.ja  va  2  s .c  o  m
 * <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);
}

From source file:org.apache.activemq.artemis.core.io.mapped.MappedFile.java

License:Apache License

/**
 * Writes a sequence of bytes to this file from the given buffer.
 * <p>/*from w w w . j av a2s.co m*/
 * <p> Bytes are written starting at this file's current position,
 */
public void write(ByteBuffer src, int srcStart, int srcLength) throws IOException {
    final int nextPosition = this.position + srcLength;
    checkCapacity(nextPosition);
    final long destAddress = this.address + this.position;
    if (src.isDirect()) {
        final long srcAddress = PlatformDependent.directBufferAddress(src) + srcStart;
        PlatformDependent.copyMemory(srcAddress, destAddress, srcLength);
    } else {
        final byte[] srcArray = src.array();
        PlatformDependent.copyMemory(srcArray, srcStart, destAddress, srcLength);
    }
    rawMovePositionAndLength(nextPosition);
}

From source file:org.apache.activemq.artemis.tests.extras.benchmarks.journal.gcfree.AddJournalRecordEncoder.java

License:Apache License

public AddJournalRecordEncoder record(final ByteBuffer recordBytes, final int recordOffset,
        final int recordLength) {
    this.bytes.putInt(this.limit, recordLength);
    final long dstAddr = PlatformDependent.directBufferAddress(bytes) + this.limit + 4;
    final long srcAddr = PlatformDependent.directBufferAddress(recordBytes) + recordOffset;
    PlatformDependent.copyMemory(srcAddr, dstAddr, recordLength);
    this.limit += (4 + recordLength);
    return this;
}

From source file:org.apache.arrow.algorithm.sort.FixedWidthOutOfPlaceVectorSorter.java

License:Apache License

@Override
public void sortOutOfPlace(V srcVector, V dstVector, VectorValueComparator<V> comparator) {
    comparator.attachVector(srcVector);//from w w w .j a  v  a  2 s .  co  m

    int valueWidth = comparator.getValueWidth();

    // buffers referenced in the sort
    ArrowBuf srcValueBuffer = srcVector.getDataBuffer();
    ArrowBuf dstValidityBuffer = dstVector.getValidityBuffer();
    ArrowBuf dstValueBuffer = dstVector.getDataBuffer();

    // sort value indices
    try (IntVector sortedIndices = new IntVector("", srcVector.getAllocator())) {
        sortedIndices.allocateNew(srcVector.getValueCount());
        sortedIndices.setValueCount(srcVector.getValueCount());
        indexSorter.sort(srcVector, sortedIndices, comparator);

        // copy sorted values to the output vector
        for (int dstIndex = 0; dstIndex < sortedIndices.getValueCount(); dstIndex++) {
            int srcIndex = sortedIndices.get(dstIndex);
            if (srcVector.isNull(srcIndex)) {
                BitVectorHelper.setValidityBit(dstValidityBuffer, dstIndex, 0);
            } else {
                BitVectorHelper.setValidityBit(dstValidityBuffer, dstIndex, 1);
                PlatformDependent.copyMemory(srcValueBuffer.memoryAddress() + srcIndex * valueWidth,
                        dstValueBuffer.memoryAddress() + dstIndex * valueWidth, valueWidth);
            }
        }
    }
}

From source file:org.apache.arrow.algorithm.sort.VariableWidthOutOfPlaceVectorSorter.java

License:Apache License

@Override
public void sortOutOfPlace(V srcVector, V dstVector, VectorValueComparator<V> comparator) {
    comparator.attachVector(srcVector);/* w  ww. j a v a 2 s  .co m*/

    // buffers referenced in the sort
    ArrowBuf srcValueBuffer = srcVector.getDataBuffer();
    ArrowBuf srcOffsetBuffer = srcVector.getOffsetBuffer();
    ArrowBuf dstValidityBuffer = dstVector.getValidityBuffer();
    ArrowBuf dstValueBuffer = dstVector.getDataBuffer();
    ArrowBuf dstOffsetBuffer = dstVector.getOffsetBuffer();

    // sort value indices
    try (IntVector sortedIndices = new IntVector("", srcVector.getAllocator())) {
        sortedIndices.allocateNew(srcVector.getValueCount());
        sortedIndices.setValueCount(srcVector.getValueCount());
        indexSorter.sort(srcVector, sortedIndices, comparator);

        int dstOffset = 0;
        dstOffsetBuffer.setInt(0, 0);

        // copy sorted values to the output vector
        for (int dstIndex = 0; dstIndex < sortedIndices.getValueCount(); dstIndex++) {
            int srcIndex = sortedIndices.get(dstIndex);
            if (srcVector.isNull(srcIndex)) {
                BitVectorHelper.setValidityBit(dstValidityBuffer, dstIndex, 0);
            } else {
                BitVectorHelper.setValidityBit(dstValidityBuffer, dstIndex, 1);
                int srcOffset = srcOffsetBuffer.getInt(srcIndex * BaseVariableWidthVector.OFFSET_WIDTH);
                int valueLength = srcOffsetBuffer.getInt((srcIndex + 1) * BaseVariableWidthVector.OFFSET_WIDTH)
                        - srcOffset;
                PlatformDependent.copyMemory(srcValueBuffer.memoryAddress() + srcOffset,
                        dstValueBuffer.memoryAddress() + dstOffset, valueLength);
                dstOffset += valueLength;
            }
            dstOffsetBuffer.setInt((dstIndex + 1) * BaseVariableWidthVector.OFFSET_WIDTH, dstOffset);
        }
    }
}

From source file:org.apache.arrow.vector.BaseFixedWidthVector.java

License:Apache License

/**
 * Copy a cell value from a particular index in source vector to a particular
 * position in this vector./*from   www.ja v a  2 s.  co m*/
 *
 * @param fromIndex position to copy from in source vector
 * @param thisIndex position to copy to in this vector
 * @param from      source vector
 */
public void copyFrom(int fromIndex, int thisIndex, BaseFixedWidthVector from) {
    if (from.isNull(fromIndex)) {
        BitVectorHelper.setValidityBit(this.getValidityBuffer(), thisIndex, 0);
    } else {
        BitVectorHelper.setValidityBit(this.getValidityBuffer(), thisIndex, 1);
        PlatformDependent.copyMemory(from.getDataBuffer().memoryAddress() + fromIndex * typeWidth,
                this.getDataBuffer().memoryAddress() + thisIndex * typeWidth, typeWidth);
    }
}

From source file:org.apache.arrow.vector.DecimalVector.java

License:Apache License

/**
 * Sets the element at given index using the buffer whose size maybe <= 16 bytes.
 * @param index index to write the decimal to
 * @param start start of value in the buffer
 * @param buffer contains the decimal in little endian bytes
 * @param length length of the value in the buffer
 *///  ww w.j av  a 2s. c o  m
public void setSafe(int index, int start, ArrowBuf buffer, int length) {
    handleSafe(index);
    BitVectorHelper.setValidityBitToOne(validityBuffer, index);

    // do the bound checks.
    buffer.checkBytes(start, start + length);
    valueBuffer.checkBytes(index * TYPE_WIDTH, (index + 1) * TYPE_WIDTH);

    long inAddress = buffer.memoryAddress() + start;
    long outAddress = valueBuffer.memoryAddress() + index * TYPE_WIDTH;
    PlatformDependent.copyMemory(inAddress, outAddress, length);
    // sign extend
    if (length < 16) {
        byte msb = PlatformDependent.getByte(inAddress + length - 1);
        final byte pad = (byte) (msb < 0 ? 0xFF : 0x00);
        PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad);
    }
}

From source file:org.apache.tajo.tuple.memory.OffHeapRowWriter.java

License:Apache License

protected void addTuple(UnSafeTuple tuple) {
    int length = tuple.getLength();
    ensureSize(length);//  w w  w .  j  ava  2 s .  co  m
    PlatformDependent.copyMemory(tuple.address(), address() + position(), length);
    forward(length);
}

From source file:org.apache.tajo.tuple.memory.UnSafeTuple.java

License:Apache License

public void writeTo(ByteBuffer bb) {
    if (bb.remaining() < getLength()) {
        throw new IndexOutOfBoundsException(
                "remaining length: " + bb.remaining() + ", tuple length: " + getLength());
    }/*  w ww  .  j  a  v  a 2s. co m*/

    if (getLength() > 0) {
        if (bb.isDirect()) {
            PlatformDependent.copyMemory(address(), PlatformDependent.directBufferAddress(bb) + bb.position(),
                    getLength());
            bb.position(bb.position() + getLength());
        } else {
            PlatformDependent.copyMemory(address(), bb.array(), bb.arrayOffset() + bb.position(), getLength());
            bb.position(bb.position() + getLength());
        }
    }
}