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

The following examples show how to use libcore.io.Memory#peekInt() . 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: ZipInputStream.java    From jtransc with Apache License 2.0 6 votes vote down vote up
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");
    }
}
 
Example 2
Source File: FileBridge.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    final byte[] temp = new byte[8192];
    try {
        while (IoBridge.read(mServer, temp, 0, MSG_LENGTH) == MSG_LENGTH) {
            final int cmd = Memory.peekInt(temp, 0, ByteOrder.BIG_ENDIAN);
            if (cmd == CMD_WRITE) {
                // Shuttle data into local file
                int len = Memory.peekInt(temp, 4, ByteOrder.BIG_ENDIAN);
                while (len > 0) {
                    int n = IoBridge.read(mServer, temp, 0, Math.min(temp.length, len));
                    if (n == -1) {
                        throw new IOException(
                                "Unexpected EOF; still expected " + len + " bytes");
                    }
                    IoBridge.write(mTarget, temp, 0, n);
                    len -= n;
                }

            } else if (cmd == CMD_FSYNC) {
                // Sync and echo back to confirm
                Os.fsync(mTarget);
                IoBridge.write(mServer, temp, 0, MSG_LENGTH);

            } else if (cmd == CMD_CLOSE) {
                // Close and echo back to confirm
                Os.fsync(mTarget);
                Os.close(mTarget);
                mClosed = true;
                IoBridge.write(mServer, temp, 0, MSG_LENGTH);
                break;
            }
        }

    } catch (ErrnoException | IOException e) {
        Log.wtf(TAG, "Failed during bridge", e);
    } finally {
        forceClose();
    }
}
 
Example 3
Source File: FileBridge.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example 4
Source File: Inet4Address.java    From jtransc with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 5
Source File: ZipInputStream.java    From jtransc with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the next entry from this {@code ZipInputStream} or {@code null} if
 * no more entries are present.
 *
 * @throws IOException if an {@code IOException} occurs.
 */
public ZipEntry getNextEntry() throws IOException {
    closeEntry();
    if (entriesEnd) {
        return null;
    }

    // Read the signature to see whether there's another local file header.
    Streams.readFully(in, hdrBuf, 0, 4);
    int hdr = Memory.peekInt(hdrBuf, 0, true);
    if (hdr == CENSIG) {
        entriesEnd = true;
        return null;
    }
    if (hdr != LOCSIG) {
        return null;
    }

    // Read the local file header.
    Streams.readFully(in, hdrBuf, 0, (LOCHDR - LOCVER));
    int version = peekShort(0) & 0xff;
    if (version > ZIPLocalHeaderVersionNeeded) {
        throw new ZipException("Cannot read local header version " + version);
    }
    int flags = peekShort(LOCFLG - LOCVER);
    if ((flags & ZipFile.GPBF_UNSUPPORTED_MASK) != 0) {
        throw new ZipException("Invalid General Purpose Bit Flag: " + flags);
    }

    hasDD = ((flags & ZipFile.GPBF_DATA_DESCRIPTOR_FLAG) != 0);
    int ceLastModifiedTime = peekShort(LOCTIM - LOCVER);
    int ceLastModifiedDate = peekShort(LOCTIM - LOCVER + 2);
    int ceCompressionMethod = peekShort(LOCHOW - LOCVER);
    long ceCrc = 0, ceCompressedSize = 0, ceSize = -1;
    if (!hasDD) {
        ceCrc = ((long) Memory.peekInt(hdrBuf, LOCCRC - LOCVER, true)) & 0xffffffffL;
        ceCompressedSize = ((long) Memory.peekInt(hdrBuf, LOCSIZ - LOCVER, true)) & 0xffffffffL;
        ceSize = ((long) Memory.peekInt(hdrBuf, LOCLEN - LOCVER, true)) & 0xffffffffL;
    }
    int nameLength = peekShort(LOCNAM - LOCVER);
    if (nameLength == 0) {
        throw new ZipException("Entry is not named");
    }
    int extraLength = peekShort(LOCEXT - LOCVER);

    if (nameLength > nameBuf.length) {
        nameBuf = new byte[nameLength];
        // The bytes are modified UTF-8, so the number of chars will always be less than or
        // equal to the number of bytes. It's fine if this buffer is too long.
    }
    Streams.readFully(in, nameBuf, 0, nameLength);
    currentEntry = createZipEntry(ModifiedUtf8.decode(nameBuf, 0, nameLength));
    currentEntry.time = ceLastModifiedTime;
    currentEntry.modDate = ceLastModifiedDate;
    currentEntry.setMethod(ceCompressionMethod);
    if (ceSize != -1) {
        currentEntry.setCrc(ceCrc);
        currentEntry.setSize(ceSize);
        currentEntry.setCompressedSize(ceCompressedSize);
    }
    if (extraLength > 0) {
        byte[] extraData = new byte[extraLength];
        Streams.readFully(in, extraData, 0, extraLength);
        currentEntry.setExtra(extraData);
    }
    return currentEntry;
}
 
Example 6
Source File: ByteBuffer.java    From jtransc with Apache License 2.0 4 votes vote down vote up
public final int getInt(int index) {
	checkIndex(index, SizeOf.INT);
	return Memory.peekInt(backingArray, arrayOffset + index, isLittleEndian);
}
 
Example 7
Source File: Socks4Message.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the IP address of the request as an integer.
 */
public int getIP() {
    return Memory.peekInt(buffer, INDEX_IP, ByteOrder.BIG_ENDIAN);
}
 
Example 8
Source File: DirectByteBuffer.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private int getInt(long a) {
    return Memory.peekInt(a, !nativeByteOrder);
}
 
Example 9
Source File: DirectByteBuffer.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private float getFloat(long a) {
    int x = Memory.peekInt(a, !nativeByteOrder);
    return Float.intBitsToFloat(x);
}
 
Example 10
Source File: DataInputStream.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public final int readInt() throws IOException {
    Streams.readFully(in, scratch, 0, SizeOf.INT);
    return Memory.peekInt(scratch, 0, ByteOrder.BIG_ENDIAN);
}
 
Example 11
Source File: RandomAccessFile.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * Reads a big-endian 32-bit integer from the current position in this file. Blocks
 * until four bytes have been read, the end of the file is reached or an
 * exception is thrown.
 *
 * @return the next int value from this file.
 * @throws EOFException
 *             if the end of this file is detected.
 * @throws IOException
 *             if this file is closed or another I/O error occurs.
 * @see #writeInt(int)
 */
public final int readInt() throws IOException {
    readFully(scratch, 0, SizeOf.INT);
    return Memory.peekInt(scratch, 0, ByteOrder.BIG_ENDIAN);
}