本文整理匯總了Java中sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET屬性的典型用法代碼示例。如果您正苦於以下問題:Java Unsafe.ARRAY_BYTE_BASE_OFFSET屬性的具體用法?Java Unsafe.ARRAY_BYTE_BASE_OFFSET怎麽用?Java Unsafe.ARRAY_BYTE_BASE_OFFSET使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類sun.misc.Unsafe
的用法示例。
在下文中一共展示了Unsafe.ARRAY_BYTE_BASE_OFFSET屬性的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: put
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;
}
示例2: read
@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();
}
}
示例3: write
@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();
}
}
示例4: entryIndexForOffset
public static int entryIndexForOffset(long constantOffset, JavaKind expectedEntryKind, ResolvedJavaType componentType, int length) {
int baseOffset;
int indexScale;
switch (componentType.getJavaKind()) {
case Boolean:
baseOffset = Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
indexScale = Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
break;
case Byte:
baseOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
indexScale = Unsafe.ARRAY_BYTE_INDEX_SCALE;
break;
case Short:
baseOffset = Unsafe.ARRAY_SHORT_BASE_OFFSET;
indexScale = Unsafe.ARRAY_SHORT_INDEX_SCALE;
break;
case Char:
baseOffset = Unsafe.ARRAY_CHAR_BASE_OFFSET;
indexScale = Unsafe.ARRAY_CHAR_INDEX_SCALE;
break;
case Int:
baseOffset = Unsafe.ARRAY_INT_BASE_OFFSET;
indexScale = Unsafe.ARRAY_INT_INDEX_SCALE;
break;
case Long:
baseOffset = Unsafe.ARRAY_LONG_BASE_OFFSET;
indexScale = Unsafe.ARRAY_LONG_INDEX_SCALE;
break;
case Float:
baseOffset = Unsafe.ARRAY_FLOAT_BASE_OFFSET;
indexScale = Unsafe.ARRAY_FLOAT_INDEX_SCALE;
break;
case Double:
baseOffset = Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
indexScale = Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
break;
case Object:
baseOffset = Unsafe.ARRAY_OBJECT_BASE_OFFSET;
indexScale = Unsafe.ARRAY_OBJECT_INDEX_SCALE;
break;
default:
return -1;
}
long offset;
if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN && componentType.isPrimitive()) {
// On big endian, we do just get expect the type be right aligned in this memory slot
offset = constantOffset - (componentType.getJavaKind().getByteCount() - Math.min(componentType.getJavaKind().getByteCount(), 4 + expectedEntryKind.getByteCount()));
} else {
offset = constantOffset;
}
long index = offset - baseOffset;
if (index % indexScale != 0) {
return -1;
}
long elementIndex = index / indexScale;
if (elementIndex < 0 || elementIndex >= length) {
return -1;
}
return (int) elementIndex;
}