当前位置: 首页>>代码示例>>Java>>正文


Java RandomAccessFile.readBoolean方法代码示例

本文整理汇总了Java中java.io.RandomAccessFile.readBoolean方法的典型用法代码示例。如果您正苦于以下问题:Java RandomAccessFile.readBoolean方法的具体用法?Java RandomAccessFile.readBoolean怎么用?Java RandomAccessFile.readBoolean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.io.RandomAccessFile的用法示例。


在下文中一共展示了RandomAccessFile.readBoolean方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: onBackup

import java.io.RandomAccessFile; //导入方法依赖的package包/类
@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);
}
 
开发者ID:sdrausty,项目名称:buildAPKsSamples,代码行数:70,代码来源:MultiRecordExampleAgent.java

示例2: onBackup

import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:sdrausty,项目名称:buildAPKsSamples,代码行数:68,代码来源:ExampleAgent.java


注:本文中的java.io.RandomAccessFile.readBoolean方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。