Java Code Examples for java.io.RandomAccessFile.readBoolean()

The following are Jave code examples for showing how to use readBoolean() of the java.io.RandomAccessFile class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: buildAPKsSamples   File: MultiRecordExampleAgent.java   Source Code and License Vote up 4 votes
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
        ParcelFileDescriptor newState) throws IOException {
    // First, get the current data from the application's file.  This
    // may throw an IOException, but in that case something has gone
    // badly wrong with the app's data on disk, and we do not want
    // to back up garbage data.  If we just let the exception go, the
    // Backup Manager will handle it and simply skip the current
    // backup operation.
    synchronized (BackupRestoreActivity.sDataLock) {
        RandomAccessFile file = new RandomAccessFile(mDataFile, "r");
        mFilling = file.readInt();
        mAddMayo = file.readBoolean();
        mAddTomato = file.readBoolean();
    }

    // If this is the first backup ever, we have to back up everything
    boolean forceBackup = (oldState == null);

    // Now read the state as of the previous backup pass, if any
    int lastFilling = 0;
    boolean lastMayo = false;
    boolean lastTomato = false;

    if (!forceBackup) {

        FileInputStream instream = new FileInputStream(oldState.getFileDescriptor());
        DataInputStream in = new DataInputStream(instream);

        try {
            // Read the state as of the last backup
            lastFilling = in.readInt();
            lastMayo = in.readBoolean();
            lastTomato = in.readBoolean();
        } catch (IOException e) {
            // If something went wrong reading the state file, be safe and
            // force a backup of all the data again.
            forceBackup = true;
        }
    }

    // Okay, now check each datum to see whether we need to back up a new value.  We'll
    // reuse the bytearray buffering stream for each datum.  We also use a little
    // helper routine to avoid some code duplication when writing the two boolean
    // records.
    ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(bufStream);

    if (forceBackup || (mFilling != lastFilling)) {
        // bufStream.reset();   // not necessary the first time, but good to remember
        out.writeInt(mFilling);
        writeBackupEntity(data, bufStream, FILLING_KEY);
    }

    if (forceBackup || (mAddMayo != lastMayo)) {
        bufStream.reset();
        out.writeBoolean(mAddMayo);
        writeBackupEntity(data, bufStream, MAYO_KEY);
    }

    if (forceBackup || (mAddTomato != lastTomato)) {
        bufStream.reset();
        out.writeBoolean(mAddTomato);
        writeBackupEntity(data, bufStream, TOMATO_KEY);
    }

    // Finally, write the state file that describes our data as of this backup pass
    writeStateFile(newState);
}
 
Example 2
Project: buildAPKsSamples   File: ExampleAgent.java   Source Code and License Vote up 4 votes
/**
 * The set of data backed up by this application is very small: just
 * two booleans and an integer.  With such a simple dataset, it's
 * easiest to simply store a copy of the backed-up data as the state
 * blob describing the last dataset backed up.  The state file
 * contents can be anything; it is private to the agent class, and
 * is never stored off-device.
 *
 * <p>One thing that an application may wish to do is tag the state
 * blob contents with a version number.  This is so that if the
 * application is upgraded, the next time it attempts to do a backup,
 * it can detect that the last backup operation was performed by an
 * older version of the agent, and might therefore require different
 * handling.
 */
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
        ParcelFileDescriptor newState) throws IOException {
    // First, get the current data from the application's file.  This
    // may throw an IOException, but in that case something has gone
    // badly wrong with the app's data on disk, and we do not want
    // to back up garbage data.  If we just let the exception go, the
    // Backup Manager will handle it and simply skip the current
    // backup operation.
    synchronized (BackupRestoreActivity.sDataLock) {
        RandomAccessFile file = new RandomAccessFile(mDataFile, "r");
        mFilling = file.readInt();
        mAddMayo = file.readBoolean();
        mAddTomato = file.readBoolean();
    }

    // If the new state file descriptor is null, this is the first time
    // a backup is being performed, so we know we have to write the
    // data.  If there <em>is</em> a previous state blob, we want to
    // double check whether the current data is actually different from
    // our last backup, so that we can avoid transmitting redundant
    // data to the storage backend.
    boolean doBackup = (oldState == null);
    if (!doBackup) {
        doBackup = compareStateFile(oldState);
    }

    // If we decided that we do in fact need to write our dataset, go
    // ahead and do that.  The way this agent backs up the data is to
    // flatten it into a single buffer, then write that to the backup
    // transport under the single key string.
    if (doBackup) {
        ByteArrayOutputStream bufStream = new ByteArrayOutputStream();

        // We use a DataOutputStream to write structured data into
        // the buffering stream
        DataOutputStream outWriter = new DataOutputStream(bufStream);
        outWriter.writeInt(mFilling);
        outWriter.writeBoolean(mAddMayo);
        outWriter.writeBoolean(mAddTomato);

        // Okay, we've flattened the data for transmission.  Pull it
        // out of the buffering stream object and send it off.
        byte[] buffer = bufStream.toByteArray();
        int len = buffer.length;
        data.writeEntityHeader(APP_DATA_KEY, len);
        data.writeEntityData(buffer, len);
    }

    // Finally, in all cases, we need to write the new state blob
    writeStateFile(newState);
}