Java Code Examples for sun.misc.Unsafe#ARRAY_BYTE_BASE_OFFSET

The following examples show how to use sun.misc.Unsafe#ARRAY_BYTE_BASE_OFFSET . 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: RenderBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example 2
Source File: RenderBuffer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example 3
Source File: RenderBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example 4
Source File: RenderBuffer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example 5
Source File: RenderBuffer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example 6
Source File: BitBlt.java    From trufflesqueak with MIT License 5 votes vote down vote up
private boolean loadBitBltDestForm() {
    if (!(isPointers(destForm) && slotSizeOf(destForm) >= 4)) {
        return false;
    }
    final Object destBitsValue = fetchPointerofObject(FORM.BITS, destForm);
    destWidth = fetchIntegerofObject(FORM.WIDTH, destForm);
    destHeight = fetchIntegerofObject(FORM.HEIGHT, destForm);
    if (!(destWidth >= 0 && destHeight >= 0)) {
        return false;
    }
    destDepth = fetchIntegerofObject(FORM.DEPTH, destForm);
    if (!(destMSB = destDepth > 0)) {
        destDepth = 0 - destDepth;
    }
    if (!isWordsOrBytes(destBitsValue)) {
        if (destBitsValue instanceof Long) {
            throw SqueakException.create("Not supported: Query for actual surface dimensions");
        } else {
            return false;
        }
    }
    destPPW = div(32, destDepth);
    destPitch = div(destWidth + destPPW - 1, destPPW) * 4;
    final NativeObject destBitsNative = (NativeObject) destBitsValue;
    final long destBitsSize;
    if (isWords(destBitsNative)) {
        destBits = destBitsNative.getIntStorage();
        destBitsSize = destBitsNative.getIntLength() * Integer.BYTES;
        destBitsBaseOffset = Unsafe.ARRAY_INT_BASE_OFFSET;
        destBitsIndexScale = Unsafe.ARRAY_INT_INDEX_SCALE;
    } else {
        destBits = destBitsNative.getByteStorage();
        destBitsSize = destBitsNative.getByteLength();
        destBitsBaseOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
        destBitsIndexScale = Unsafe.ARRAY_BYTE_INDEX_SCALE * Integer.BYTES;
    }
    return destBitsSize >= destPitch * destHeight;
}
 
Example 7
Source File: LinuxUserDefinedFileAttributeView.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int write(String name, ByteBuffer src) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), false, true);

    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (src instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)src).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();

        if (src.hasArray()) {
            // copy from backing array into buffer
            int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
            unsafe.copyMemory(src.array(), off, null, address, rem);
        } else {
            // backing array not accessible so transfer via temporary array
            byte[] tmp = new byte[rem];
            src.get(tmp);
            src.position(pos);  // reset position as write may fail
            unsafe.copyMemory(tmp, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
                address, rem);
        }
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            fsetxattr(fd, nameAsBytes(file,name), address, rem);
            src.position(pos + rem);
            return rem;
        } catch (UnixException x) {
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error writing extended attribute '" + name + "': " +
                x.getMessage());
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 8
Source File: LinuxUserDefinedFileAttributeView.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 9
Source File: LinuxUserDefinedFileAttributeView.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 10
Source File: LinuxUserDefinedFileAttributeView.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int write(String name, ByteBuffer src) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), false, true);

    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (src instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)src).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();

        if (src.hasArray()) {
            // copy from backing array into buffer
            int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
            unsafe.copyMemory(src.array(), off, null, address, rem);
        } else {
            // backing array not accessible so transfer via temporary array
            byte[] tmp = new byte[rem];
            src.get(tmp);
            src.position(pos);  // reset position as write may fail
            unsafe.copyMemory(tmp, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
                address, rem);
        }
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            fsetxattr(fd, nameAsBytes(file,name), address, rem);
            src.position(pos + rem);
            return rem;
        } catch (UnixException x) {
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error writing extended attribute '" + name + "': " +
                x.getMessage());
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 11
Source File: LinuxUserDefinedFileAttributeView.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 12
Source File: LinuxUserDefinedFileAttributeView.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int write(String name, ByteBuffer src) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), false, true);

    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (src instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)src).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();

        if (src.hasArray()) {
            // copy from backing array into buffer
            int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
            unsafe.copyMemory(src.array(), off, null, address, rem);
        } else {
            // backing array not accessible so transfer via temporary array
            byte[] tmp = new byte[rem];
            src.get(tmp);
            src.position(pos);  // reset position as write may fail
            unsafe.copyMemory(tmp, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
                address, rem);
        }
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            fsetxattr(fd, nameAsBytes(file,name), address, rem);
            src.position(pos + rem);
            return rem;
        } catch (UnixException x) {
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error writing extended attribute '" + name + "': " +
                x.getMessage());
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 13
Source File: LinuxUserDefinedFileAttributeView.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int write(String name, ByteBuffer src) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), false, true);

    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (src instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)src).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();

        if (src.hasArray()) {
            // copy from backing array into buffer
            int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
            unsafe.copyMemory(src.array(), off, null, address, rem);
        } else {
            // backing array not accessible so transfer via temporary array
            byte[] tmp = new byte[rem];
            src.get(tmp);
            src.position(pos);  // reset position as write may fail
            unsafe.copyMemory(tmp, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
                address, rem);
        }
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            fsetxattr(fd, nameAsBytes(file,name), address, rem);
            src.position(pos + rem);
            return rem;
        } catch (UnixException x) {
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error writing extended attribute '" + name + "': " +
                x.getMessage());
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 14
Source File: LinuxUserDefinedFileAttributeView.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 15
Source File: LinuxUserDefinedFileAttributeView.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int write(String name, ByteBuffer src) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), false, true);

    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (src instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)src).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();

        if (src.hasArray()) {
            // copy from backing array into buffer
            int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
            unsafe.copyMemory(src.array(), off, null, address, rem);
        } else {
            // backing array not accessible so transfer via temporary array
            byte[] tmp = new byte[rem];
            src.get(tmp);
            src.position(pos);  // reset position as write may fail
            unsafe.copyMemory(tmp, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
                address, rem);
        }
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            fsetxattr(fd, nameAsBytes(file,name), address, rem);
            src.position(pos + rem);
            return rem;
        } catch (UnixException x) {
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error writing extended attribute '" + name + "': " +
                x.getMessage());
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 16
Source File: LinuxUserDefinedFileAttributeView.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 17
Source File: LinuxUserDefinedFileAttributeView.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 18
Source File: LinuxUserDefinedFileAttributeView.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int write(String name, ByteBuffer src) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), false, true);

    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (src instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)src).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();

        if (src.hasArray()) {
            // copy from backing array into buffer
            int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
            unsafe.copyMemory(src.array(), off, null, address, rem);
        } else {
            // backing array not accessible so transfer via temporary array
            byte[] tmp = new byte[rem];
            src.get(tmp);
            src.position(pos);  // reset position as write may fail
            unsafe.copyMemory(tmp, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
                address, rem);
        }
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            fsetxattr(fd, nameAsBytes(file,name), address, rem);
            src.position(pos + rem);
            return rem;
        } catch (UnixException x) {
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error writing extended attribute '" + name + "': " +
                x.getMessage());
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 19
Source File: LinuxUserDefinedFileAttributeView.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}
 
Example 20
Source File: LinuxUserDefinedFileAttributeView.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    if (dst.isReadOnly())
        throw new IllegalArgumentException("Read-only buffer");
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);

    NativeBuffer nb;
    long address;
    if (dst instanceof sun.nio.ch.DirectBuffer) {
        nb = null;
        address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
    } else {
        // substitute with native buffer
        nb = NativeBuffers.getNativeBuffer(rem);
        address = nb.address();
    }

    int fd = file.openForAttributeAccess(followLinks);
    try {
        try {
            int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);

            // if remaining is zero then fgetxattr returns the size
            if (rem == 0) {
                if (n > 0)
                    throw new UnixException(ERANGE);
                return 0;
            }

            // copy from buffer into backing array if necessary
            if (nb != null) {
                int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
                unsafe.copyMemory(null, address, dst.array(), off, n);
            }
            dst.position(pos + n);
            return n;
        } catch (UnixException x) {
            String msg = (x.errno() == ERANGE) ?
                "Insufficient space in buffer" : x.getMessage();
            throw new FileSystemException(file.getPathForExceptionMessage(),
                null, "Error reading extended attribute '" + name + "': " + msg);
        } finally {
            close(fd);
        }
    } finally {
        if (nb != null)
            nb.release();
    }
}