Search in sources :

Example 81 with RandomAccessFile

use of java.io.RandomAccessFile in project voltdb by VoltDB.

the class LobStoreRAFile method openFile.

private void openFile() {
    try {
        String name = database.getPath() + ".lobs";
        boolean readonly = database.isReadOnly();
        file = new RandomAccessFile(name, readonly ? "r" : "rwd");
    } catch (Throwable t) {
        throw Error.error(ErrorCode.DATA_FILE_ERROR, t);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile)

Example 82 with RandomAccessFile

use of java.io.RandomAccessFile in project voltdb by VoltDB.

the class NIOLockFile method aquireFileLock.

// ------------------------- Internal Implementation------------------------
// does the real work of aquiring the FileLock
private boolean aquireFileLock() {
    // PRE:
    //
    // raf is never null and is never closed upon entry.
    //
    // Rhetorical question to self: How does one tell if a RandomAccessFile
    // is closed, short of invoking an operation and getting an IOException
    // the says its closed (assuming you can control the Locale of the error
    // message)?
    //
    final RandomAccessFile lraf = super.raf;
    // In an ideal world, we would use a lock region back off approach,
    // starting with region MAX_LOCK_REGION, then MAX_NFS_LOCK_REGION,
    // then MIN_LOCK_REGION.
    //
    // In practice, however, it is just generally unwise to mount network
    // file system database instances.  Be warned.
    //
    // In general, it is probably also unwise to mount removable media
    // database instances that are not read-only.
    boolean success = false;
    try {
        if (this.fileLock != null) {
            // NIO classes...better to be safe than sorry.
            if (this.fileLock.isValid()) {
                return true;
            } else {
                // It's not valid, so releasing it is a no-op.
                //
                // However, we should still clean up the referenceand hope
                // no previous complications exist (a hung FileLock in a
                // flaky JVM) or that gc kicks in and saves the day...
                // (unlikely, though).
                this.releaseFileLock();
            }
        }
        if (isPosixManditoryFileLock()) {
            try {
                Runtime.getRuntime().exec(new String[] { "chmod", "g+s,g-x", file.getPath() });
            } catch (Exception ex) {
            //ex.printStackTrace();
            }
        }
        // Note: from FileChannel.tryLock(...) JavaDoc:
        //
        // @return  A lock object representing the newly-acquired lock,
        //          or <tt>null</tt> if the lock could not be acquired
        //          because another program holds an overlapping lock
        this.fileLock = lraf.getChannel().tryLock(0, MIN_LOCK_REGION, false);
        // According to the API, if it's non-null, it must be valid.
        // This may not actually yet be the full truth of the matter under
        // all commonly available JVM implementations.
        // fileLock.isValid() API says it never throws, though, so
        // with fingers crossed...
        success = (this.fileLock != null && this.fileLock.isValid());
    } catch (Exception e) {
    }
    if (!success) {
        this.releaseFileLock();
    }
    return success;
}
Also used : RandomAccessFile(java.io.RandomAccessFile)

Example 83 with RandomAccessFile

use of java.io.RandomAccessFile in project android_frameworks_base by ResurrectionRemix.

the class HugeAgent method onRestore.

/**
     * This application does not do any "live" restores of its own data,
     * so the only time a restore will happen is when the application is
     * installed.  This means that the activity itself is not going to
     * be running while we change its data out from under it.  That, in
     * turn, means that there is no need to send out any sort of notification
     * of the new data:  we only need to read the data from the stream
     * provided here, build the application's new data file, and then
     * write our new backup state blob that will be consulted at the next
     * backup operation.
     *
     * <p>We don't bother checking the versionCode of the app who originated
     * the data because we have never revised the backup data format.  If
     * we had, the 'appVersionCode' parameter would tell us how we should
     * interpret the data we're about to read.
     */
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
    // way to consume it is using a while() loop
    while (data.readNextHeader()) {
        String key = data.getKey();
        int dataSize = data.getDataSize();
        if (APP_DATA_KEY.equals(key)) {
            // It's our saved data, a flattened chunk of data all in
            // one buffer.  Use some handy structured I/O classes to
            // extract it.
            byte[] dataBuf = new byte[dataSize];
            data.readEntityData(dataBuf, 0, dataSize);
            ByteArrayInputStream baStream = new ByteArrayInputStream(dataBuf);
            DataInputStream in = new DataInputStream(baStream);
            mFilling = in.readInt();
            mAddMayo = in.readBoolean();
            mAddTomato = in.readBoolean();
            // on the data we are restoring from.
            synchronized (HugeBackupActivity.sDataLock) {
                RandomAccessFile file = new RandomAccessFile(mDataFile, "rw");
                file.setLength(0L);
                file.writeInt(mFilling);
                file.writeBoolean(mAddMayo);
                file.writeBoolean(mAddTomato);
            }
        } else {
            // Curious!  This entity is data under a key we do not
            // understand how to process.  Just skip it.
            data.skipEntityData();
        }
    }
    // The last thing to do is write the state blob that describes the
    // app's data as restored from backup.
    writeStateFile(newState);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream)

Example 84 with RandomAccessFile

use of java.io.RandomAccessFile in project android_frameworks_base by ResurrectionRemix.

the class HugeBackupActivity method recordNewUIState.

/**
     * Another helper; this one reads the current UI state and writes that
     * to the persistent store, then tells the backup manager that we need
     * a backup.
     */
void recordNewUIState() {
    boolean addMayo = mAddMayoCheckbox.isChecked();
    boolean addTomato = mAddTomatoCheckbox.isChecked();
    int whichFilling = mFillingGroup.getCheckedRadioButtonId();
    try {
        synchronized (HugeBackupActivity.sDataLock) {
            RandomAccessFile file = new RandomAccessFile(mDataFile, "rw");
            writeDataToFileLocked(file, addMayo, addTomato, whichFilling);
        }
    } catch (IOException e) {
        Log.e(TAG, "Unable to record new UI state");
    }
    mBackupManager.dataChanged();
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException)

Example 85 with RandomAccessFile

use of java.io.RandomAccessFile in project android_frameworks_base by ResurrectionRemix.

the class MemoryMappedFile_Delegate method mmapRO.

@LayoutlibDelegate
static MemoryMappedFile mmapRO(String path) throws ErrnoException {
    if (!path.startsWith(TARGET_PATH)) {
        throw new ErrnoException("Custom timezone data files are not supported.", 1);
    }
    if (sRootPath == null) {
        throw new ErrnoException("Bridge has not been initialized properly.", 1);
    }
    path = path.substring(TARGET_PATH.length());
    try {
        File f = new File(sRootPath, path);
        if (!f.exists()) {
            throw new ErrnoException("File not found: " + f.getPath(), 1);
        }
        RandomAccessFile file = new RandomAccessFile(f, "r");
        try {
            long size = file.length();
            MemoryMappedFile_Delegate newDelegate = new MemoryMappedFile_Delegate(file);
            long filePointer = file.getFilePointer();
            MemoryMappedFile mmFile = new MemoryMappedFile(filePointer, size);
            long delegateIndex = sManager.addNewDelegate(newDelegate);
            sMemoryMappedFileMap.put(mmFile, delegateIndex);
            return mmFile;
        } finally {
            file.close();
        }
    } catch (IOException e) {
        throw new ErrnoException("mmapRO", 1, e);
    }
}
Also used : ErrnoException(android.system.ErrnoException) RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Aggregations

RandomAccessFile (java.io.RandomAccessFile)1607 IOException (java.io.IOException)761 File (java.io.File)691 FileChannel (java.nio.channels.FileChannel)284 ByteBuffer (java.nio.ByteBuffer)174 FileNotFoundException (java.io.FileNotFoundException)167 Test (org.junit.Test)152 FileOutputStream (java.io.FileOutputStream)91 FileLock (java.nio.channels.FileLock)91 MappedByteBuffer (java.nio.MappedByteBuffer)83 InputStream (java.io.InputStream)68 FileInputStream (java.io.FileInputStream)63 EOFException (java.io.EOFException)55 ArrayList (java.util.ArrayList)45 ByteArrayOutputStream (java.io.ByteArrayOutputStream)39 BufferedInputStream (java.io.BufferedInputStream)35 ByteArrayInputStream (java.io.ByteArrayInputStream)35 Random (java.util.Random)33 HashMap (java.util.HashMap)24 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)23