Java Code Examples for libcore.io.Memory#memmove()

The following examples show how to use libcore.io.Memory#memmove() . 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: ByteBufferAsShortBuffer.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public ShortBuffer compact() {
    if (isReadOnly) {
        throw new ReadOnlyBufferException();
    }
    int pos = position();
    int lim = limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);
    if (!(bb instanceof DirectByteBuffer)) {
        System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 1);
    } else {
        Memory.memmove(this, ix(0), this, ix(pos), rem << 1);
    }
    position(rem);
    limit(capacity());
    discardMark();
    return this;
}
 
Example 2
Source File: ByteBufferAsFloatBuffer.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public FloatBuffer compact() {
    if (isReadOnly) {
        throw new ReadOnlyBufferException();
    }
    int pos = position();
    int lim = limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);
    if (!(bb instanceof DirectByteBuffer)) {
        System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 2);
    } else {
        Memory.memmove(this, ix(0), this, ix(pos), rem << 2);
    }
    position(rem);
    limit(capacity());
    discardMark();
    return this;
}
 
Example 3
Source File: ByteBufferAsIntBuffer.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public IntBuffer compact() {
    if (isReadOnly) {
        throw new ReadOnlyBufferException();
    }
    int pos = position();
    int lim = limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);
    if (!(bb instanceof DirectByteBuffer)) {
        System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 2);
    } else {
        Memory.memmove(this, ix(0), this, ix(pos), rem << 2);
    }
    position(rem);
    limit(capacity());
    discardMark();
    return this;
}
 
Example 4
Source File: ByteBufferAsDoubleBuffer.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public DoubleBuffer compact() {
    if (isReadOnly) {
        throw new ReadOnlyBufferException();
    }
    int pos = position();
    int lim = limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);
    if (!(bb instanceof DirectByteBuffer)) {
        System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 3);
    } else {
        Memory.memmove(this, ix(0), this, ix(pos), rem << 3);
    }
    position(rem);
    limit(capacity());
    discardMark();
    return this;
}
 
Example 5
Source File: ByteBufferAsLongBuffer.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public LongBuffer compact() {
    if (isReadOnly) {
        throw new ReadOnlyBufferException();
    }
    int pos = position();
    int lim = limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);
    if (!(bb instanceof DirectByteBuffer)) {
        System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 3);
    } else {
        Memory.memmove(this, ix(0), this, ix(pos), rem << 3);
    }
    position(rem);
    limit(capacity());
    discardMark();
    return this;
}
 
Example 6
Source File: ByteBufferAsCharBuffer.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public CharBuffer compact() {
    if (isReadOnly) {
        throw new ReadOnlyBufferException();
    }
    int pos = position();
    int lim = limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);
    if (!(bb instanceof DirectByteBuffer)) {
        System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 1);
    } else {
        Memory.memmove(this, ix(0), this, ix(pos), rem << 1);
    }
    position(rem);
    limit(capacity());
    discardMark();
    return this;
}
 
Example 7
Source File: ByteBuffer.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
 *
 * <p> This method transfers the bytes remaining in the given source
 * buffer into this buffer.  If there are more bytes remaining in the
 * source buffer than in this buffer, that is, if
 * <tt>src.remaining()</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
 * then no bytes are transferred and a {@link
 * BufferOverflowException} is thrown.
 *
 * <p> Otherwise, this method copies
 * <i>n</i>&nbsp;=&nbsp;<tt>src.remaining()</tt> bytes from the given
 * buffer into this buffer, starting at each buffer's current position.
 * The positions of both buffers are then incremented by <i>n</i>.
 *
 * <p> In other words, an invocation of this method of the form
 * <tt>dst.put(src)</tt> has exactly the same effect as the loop
 *
 * <pre>
 *     while (src.hasRemaining())
 *         dst.put(src.get()); </pre>
 *
 * except that it first checks that there is sufficient space in this
 * buffer and it is potentially much more efficient.
 *
 * @param  src
 *         The source buffer from which bytes are to be read;
 *         must not be this buffer
 *
 * @return  This buffer
 *
 * @throws  BufferOverflowException
 *          If there is insufficient space in this buffer
 *          for the remaining bytes in the source buffer
 *
 * @throws  IllegalArgumentException
 *          If the source buffer is this buffer
 *
 * @throws  ReadOnlyBufferException
 *          If this buffer is read-only
 */
public ByteBuffer put(ByteBuffer src) {
    if (src == this)
        throw new IllegalArgumentException();
    if (isReadOnly())
        throw new ReadOnlyBufferException();
    int n = src.remaining();
    if (n > remaining())
        throw new BufferOverflowException();

    // Android-changed: improve ByteBuffer.put(ByteBuffer) performance through bulk copy.
    /*
    for (int i = 0; i < n; i++)
        put(src.get());
    */
    // Note that we use offset instead of arrayOffset because arrayOffset is specified to
    // throw for read only buffers. Our use of arrayOffset here is provably safe, we only
    // use it to read *from* readOnly buffers.
    if (this.hb != null && src.hb != null) {
        // System.arraycopy is intrinsified by ART and therefore tiny bit faster than memmove
        System.arraycopy(src.hb, src.position() + src.offset, hb, position() + offset, n);
    } else {
        // Use the buffer object (and the raw memory address) if it's a direct buffer. Note that
        // isDirect() doesn't imply !hasArray(), ByteBuffer.allocateDirect allocated buffer will
        // have a backing, non-gc-movable byte array. JNI allocated direct byte buffers WILL NOT
        // have a backing array.
        final Object srcObject = src.isDirect() ? src : src.hb;
        int srcOffset = src.position();
        if (!src.isDirect()) {
            srcOffset += src.offset;
        }

        final ByteBuffer dst = this;
        final Object dstObject = dst.isDirect() ? dst : dst.hb;
        int dstOffset = dst.position();
        if (!dst.isDirect()) {
            dstOffset += dst.offset;
        }
        Memory.memmove(dstObject, dstOffset, srcObject, srcOffset, n);
    }
    src.position(src.limit());
    this.position(this.position() + n);
    return this;
}