Java源码示例:libcore.io.Memory
示例1
private void readAndVerifyDataDescriptor(int inB, int out) throws IOException {
if (hasDD) {
Streams.readFully(in, hdrBuf, 0, EXTHDR);
int sig = Memory.peekInt(hdrBuf, 0, true);
if (sig != (int) EXTSIG) {
throw new ZipException(String.format("unknown format (EXTSIG=%x)", sig));
}
currentEntry.crc = ((long) Memory.peekInt(hdrBuf, EXTCRC, true)) & 0xffffffffL;
currentEntry.compressedSize = ((long) Memory.peekInt(hdrBuf, EXTSIZ, true)) & 0xffffffffL;
currentEntry.size = ((long) Memory.peekInt(hdrBuf, EXTLEN, true)) & 0xffffffffL;
}
if (currentEntry.crc != crc.getValue()) {
throw new ZipException("CRC mismatch");
}
if (currentEntry.compressedSize != inB || currentEntry.size != out) {
throw new ZipException("Size mismatch");
}
}
示例2
private static UUID makeUuid(byte[] hash, int version) {
long msb = Memory.peekLong(hash, 0, false);
long lsb = Memory.peekLong(hash, 8, false);
// Set the version field.
msb &= ~(0xfL << 12);
msb |= ((long) version) << 12;
// Set the variant field to 2. Note that the variant field is variable-width,
// so supporting other variants is not just a matter of changing the constant 2 below!
lsb &= ~(0x3L << 62);
lsb |= 2L << 62;
return new UUID(msb, lsb);
}
示例3
/**
* Write String {@code object} into the receiver. It is assumed the
* String has not been dumped yet. Returns the handle for this object (String) which is dumped here.
* Strings are saved encoded with {@link DataInput modified UTF-8}.
*
* @param object
* the string to dump.
* @return the handle assigned to the String being dumped
*
* @throws IOException
* If an IO exception happened when writing the String.
*/
private int writeNewString(String object, boolean unshared) throws IOException {
long count = ModifiedUtf8.countBytes(object, false);
byte[] buffer;
int offset = 0;
if (count <= 0xffff) {
buffer = new byte[1 + SizeOf.SHORT + (int) count];
buffer[offset++] = TC_STRING;
Memory.pokeShort(buffer, offset, (short) count, false);
offset += SizeOf.SHORT;
} else {
buffer = new byte[1 + SizeOf.LONG + (int) count];
buffer[offset++] = TC_LONGSTRING;
Memory.pokeLong(buffer, offset, count, false);
offset += SizeOf.LONG;
}
ModifiedUtf8.encode(buffer, offset, object);
output.write(buffer, 0, buffer.length);
int handle = nextHandle();
if (!unshared) {
objectsWritten.put(object, handle);
}
return handle;
}
示例4
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;
}
示例5
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;
}
示例6
@Override
public ByteBuffer get(byte[] dst, int dstOffset, int length) {
if (!memoryRef.isAccessible) {
throw new IllegalStateException("buffer is inaccessible");
}
checkBounds(dstOffset, length, dst.length);
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (length > rem)
throw new BufferUnderflowException();
Memory.peekByteArray(ix(pos),
dst, dstOffset, length);
position = pos + length;
return this;
}
示例7
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;
}
示例8
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;
}
示例9
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;
}
示例10
private void writeCommandAndBlock(int cmd, String cmdString) throws IOException {
Memory.pokeInt(mTemp, 0, cmd, ByteOrder.BIG_ENDIAN);
IoBridge.write(mClient, mTemp, 0, MSG_LENGTH);
// Wait for server to ack
if (IoBridge.read(mClient, mTemp, 0, MSG_LENGTH) == MSG_LENGTH) {
if (Memory.peekInt(mTemp, 0, ByteOrder.BIG_ENDIAN) == cmd) {
return;
}
}
throw new IOException("Failed to execute " + cmdString + " across bridge");
}
示例11
@Override
public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException {
Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount);
Memory.pokeInt(mTemp, 0, CMD_WRITE, ByteOrder.BIG_ENDIAN);
Memory.pokeInt(mTemp, 4, byteCount, ByteOrder.BIG_ENDIAN);
IoBridge.write(mClient, mTemp, 0, MSG_LENGTH);
IoBridge.write(mClient, buffer, byteOffset, byteCount);
}
示例12
/**
* Returns an array containing the <i>modified UTF-8</i> form of {@code s}, using a
* big-endian 16-bit length. Throws UTFDataFormatException if {@code s} is too long
* for a two-byte length.
*/
public static byte[] encode(String s) throws UTFDataFormatException {
int utfCount = (int) ModifiedUtf8.countBytes(s, true);
byte[] result = new byte[SizeOf.SHORT + utfCount];
Memory.pokeShort(result, 0, (short) utfCount, ByteOrder.BIG_ENDIAN);
ModifiedUtf8.encode(result, SizeOf.SHORT, s);
return result;
}
示例13
@Override
public boolean isMCGlobal() {
// Check if we have a prefix of 1110
if (!isMulticastAddress()) {
return false;
}
int address = Memory.peekInt(ipaddress, 0, false);
/*
* Now check the boundaries of the global space if we have an address
* that is prefixed by something less than 111000000000000000000001
* (fortunately we don't have to worry about sign after shifting 8 bits
* right) it is not multicast. ( < 224.0.1.0)
*/
if (address >>> 8 < 0xE00001) {
return false;
}
/*
* Now check the high boundary which is prefixed by 11101110 = 0xEE. If
* the value is higher than this than it is not MCGlobal ( >
* 238.255.255.255 )
*/
if (address >>> 24 > 0xEE) {
return false;
}
return true;
}
示例14
@Override
@HaxeMethodBody("return N.i2s(this.tarray.get(p0));")
@JTranscMethodBody(target = "js", value = "return this.tarray[p0];")
@JTranscMethodBody(target = "dart", value = "return this.tarray[p0];")
@JTranscMethodBody(target = "cpp", value = "return this->tarray[p0];")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { return ((short *)ptr)[p0]; } }")
public short get(int index) {
return Memory.peekAlignedShortLE(bytes, index);
}
示例15
@Override
@HaxeMethodBody("this.tarray.set(p0, p1); return this;")
@JTranscMethodBody(target = "js", value = "this.tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "dart", value = "this.tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cpp", value = "this->tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { ((short *)ptr)[p0] = p1; } } return this;")
public ShortBuffer put(int index, short c) {
Memory.pokeAlignedShortLE(bytes, index, c);
return this;
}
示例16
@Override
@HaxeMethodBody("return this.farray.get(p0);")
@JTranscMethodBody(target = "js", value = "return this.farray[p0];")
@JTranscMethodBody(target = "dart", value = "return this.farray[p0];")
@JTranscMethodBody(target = "cpp", value = "return this->farray[p0];")
@JTranscMethodBody(target = "cs", value = "return BitConverter.ToSingle(this.tarray, p0);")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { return ((float *)ptr)[p0]; } }")
public float get(int index) {
return Memory.peekAlignedFloatLE(bytes, index);
}
示例17
@Override
@HaxeMethodBody("this.farray.set(p0, p1); return this;")
@JTranscMethodBody(target = "js", value = "this.farray[p0] = p1; return this;")
@JTranscMethodBody(target = "dart", value = "this.farray[p0] = p1; return this;")
@JTranscMethodBody(target = "cpp", value = "this->farray[p0] = p1; return this;")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { ((float *)ptr)[p0] = p1; } } return this;")
public FloatBuffer put(int index, float c) {
Memory.pokeAlignedFloatLE(bytes, index, c);
return this;
}
示例18
@HaxeMethodBody("return this.iarray.get(p0);")
@JTranscMethodBody(target = "js", value = "return this.iarray[p0];")
@JTranscMethodBody(target = "dart", value = "return this.iarray[p0];")
@JTranscMethodBody(target = "cpp", value = "return this->iarray[p0];")
@JTranscMethodBody(target = "cs", value = "return BitConverter.ToSingle(this.iarray, p0);")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { return ((int *)ptr)[p0]; } }")
public int getInt(int index) {
return Memory.peekAlignedIntLE(bytes, index);
}
示例19
@HaxeMethodBody("this.iarray.set(p0, p1); return this;")
@JTranscMethodBody(target = "js", value = "this.iarray[p0] = p1; return this;")
@JTranscMethodBody(target = "dart", value = "this.iarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cpp", value = "this->iarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { ((int *)ptr)[p0] = p1; } } return this;")
public FloatBuffer putInt(int index, int c) {
Memory.pokeAlignedIntLE(bytes, index, c);
return this;
}
示例20
public void testSetShortArray() {
short[] values = { 0x0001, 0x0020, 0x0300, 0x4000 };
short[] swappedValues = { 0x0100, 0x2000, 0x0003, 0x0040 };
int scale = SizeOf.SHORT;
// VMRuntime runtime = VMRuntime.getRuntime();
// byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, scale * values.length + 1);
// int base_ptr = (int) runtime.addressOf(array);
byte[] array = new byte[scale * values.length + 1];
long base_ptr = addressOf(array);
for (int ptr_offset = 0; ptr_offset < 2; ++ptr_offset) {
long ptr = base_ptr + ptr_offset; // To test aligned and unaligned accesses.
Arrays.fill(array, (byte) 0);
// Regular copy.
Memory.pokeShortArray(ptr, values, 0, values.length, false);
assertShortsEqual(values, ptr, false);
assertShortsEqual(swappedValues, ptr, true);
// Swapped copy.
Memory.pokeShortArray(ptr, values, 0, values.length, true);
assertShortsEqual(values, ptr, true);
assertShortsEqual(swappedValues, ptr, false);
// Swapped copies of slices (to ensure we test non-zero offsets).
for (int i = 0; i < values.length; ++i) {
Memory.pokeShortArray(ptr + i * scale, values, i, 1, true);
}
assertShortsEqual(values, ptr, true);
assertShortsEqual(swappedValues, ptr, false);
}
}
示例21
public void testSetIntArray() {
int[] values = { 3, 7, 31, 127, 8191, 131071, 524287, 2147483647 };
int[] swappedValues = new int[values.length];
for (int i = 0; i < values.length; ++i) {
swappedValues[i] = Integer.reverseBytes(values[i]);
}
int scale = SizeOf.INT;
// VMRuntime runtime = VMRuntime.getRuntime();
// byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, scale * values.length + 1);
// int base_ptr = (int) runtime.addressOf(array);
byte[] array = new byte[scale * values.length + 1];
long base_ptr = addressOf(array);
for (int ptr_offset = 0; ptr_offset < 2; ++ptr_offset) {
long ptr = base_ptr + ptr_offset; // To test aligned and unaligned accesses.
Arrays.fill(array, (byte) 0);
// Regular copy.
Memory.pokeIntArray(ptr, values, 0, values.length, false);
assertIntsEqual(values, ptr, false);
assertIntsEqual(swappedValues, ptr, true);
// Swapped copy.
Memory.pokeIntArray(ptr, values, 0, values.length, true);
assertIntsEqual(values, ptr, true);
assertIntsEqual(swappedValues, ptr, false);
// Swapped copies of slices (to ensure we test non-zero offsets).
for (int i = 0; i < values.length; ++i) {
Memory.pokeIntArray(ptr + i * scale, values, i, 1, true);
}
assertIntsEqual(values, ptr, true);
assertIntsEqual(swappedValues, ptr, false);
}
}
示例22
public final long getLong(int index) {
checkIndex(index, SizeOf.LONG);
return Memory.peekLong(backingArray, arrayOffset + index, isLittleEndian);
}
示例23
@Override
@HaxeMethodBody("return this.tarray.get(p0);")
@JTranscMethodBody(target = "js", value = "return this.tarray[p0];")
@JTranscMethodBody(target = "dart", value = "return this.tarray[p0];")
@JTranscMethodBody(target = "cpp", value = "return this->tarray[p0];")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { return ((int *)ptr)[p0]; } }")
public int get(int index) {
checkIndex(index);
//return byteBuffer.getInt(index * SizeOf.INT);
return Memory.peekAlignedIntLE(bytes, index);
}
示例24
@Override
//@HaxeMethodBody("return this.tarray.get(p0);")
@JTranscMethodBody(target = "js", value = "return this.tarray[p0];")
@JTranscMethodBody(target = "dart", value = "return this.tarray[p0];")
@JTranscMethodBody(target = "cpp", value = "return this->tarray[p0];")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { return ((double *)ptr)[p0]; } }")
public double get(int index) {
return Memory.peekAlignedDoubleLE(bytes, index);
}
示例25
@Override
//@HaxeMethodBody("this.tarray.set(p0, p1); return this;")
@JTranscMethodBody(target = "js", value = "this.tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "dart", value = "this.tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cpp", value = "this->tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { ((double *)ptr)[p0] = p1; } } return this;")
public DoubleBuffer put(int index, double c) {
Memory.pokeAlignedDoubleLE(bytes, index, c);
return this;
}
示例26
@Override
@HaxeMethodBody("var low = this.tarray.get(p0 * 2 + 0); var high = this.tarray.get(p0 * 2 + 1); return N.lnew(high, low);")
@JTranscMethodBody(target = "js", value = "var low = this.tarray[p0 * 2 + 0]; var high = this.tarray[p0 * 2 + 1]; return N.lnew(high, low);")
@JTranscMethodBody(target = "dart", value = "return N.lnew(this.tarray[p0]);")
@JTranscMethodBody(target = "cpp", value = "return this->tarray[p0];")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { return ((long *)ptr)[p0]; } }")
public long get(int index) {
return Memory.peekAlignedLongLE(bytes, index);
}
示例27
@Override
@HaxeMethodBody("this.tarray.set(p0 * 2 + 0, N.llow(p1)); this.tarray.set(p0 * 2 + 1, N.lhigh(p1)); return this;")
@JTranscMethodBody(target = "js", value = "this.tarray[p0 * 2 + 0] = p1.low; this.tarray[p0 * 2 + 1] = p1.high; return this;")
@JTranscMethodBody(target = "dart", value = "this.tarray[p0] = p1.toInt(); return this;")
@JTranscMethodBody(target = "cpp", value = "this->tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { ((long *)ptr)[p0] = p1; } } return this;")
public LongBuffer put(int index, long c) {
Memory.pokeAlignedLongLE(bytes, index, c);
return this;
}
示例28
@Override
@HaxeMethodBody("return N.i2s(this.tarray.get(p0));")
@JTranscMethodBody(target = "js", value = "return this.tarray[p0];")
@JTranscMethodBody(target = "dart", value = "return this.tarray[p0];")
@JTranscMethodBody(target = "cpp", value = "return this->tarray[p0];")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { return ((ushort *)ptr)[p0]; } }")
public char get(int index) {
return Memory.peekAlignedCharLE(bytes, index);
}
示例29
@Override
@HaxeMethodBody("this.tarray.set(p0, p1); return this;")
@JTranscMethodBody(target = "js", value = "this.tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "dart", value = "this.tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cpp", value = "this->tarray[p0] = p1; return this;")
@JTranscMethodBody(target = "cs", value = "unsafe { fixed (byte* ptr = this.tarray) { ((ushort *)ptr)[p0] = p1; } } return this;")
public CharBuffer put(int index, char c) {
Memory.pokeAlignedCharLE(bytes, index, c);
return this;
}
示例30
@Override
public final char getChar() {
if (!memoryRef.isAccessible) {
throw new IllegalStateException("buffer is inaccessible");
}
int newPosition = position + Character.BYTES;
if (newPosition > limit()) {
throw new BufferUnderflowException();
}
char x = (char) Memory.peekShort(ix(position), !nativeByteOrder);
position = newPosition;
return x;
}