Search in sources :

Example 31 with RandomAccessFile

use of java.io.RandomAccessFile in project platform_frameworks_base by android.

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 32 with RandomAccessFile

use of java.io.RandomAccessFile in project graphhopper by graphhopper.

the class UnsafeDataAccess method loadExisting.

@Override
public boolean loadExisting() {
    if (isClosed())
        throw new IllegalStateException("already closed");
    File file = new File(getFullName());
    if (!file.exists() || file.length() == 0)
        return false;
    try {
        RandomAccessFile raFile = new RandomAccessFile(getFullName(), "r");
        try {
            long byteCount = readHeader(raFile) - HEADER_OFFSET;
            if (byteCount < 0)
                return false;
            raFile.seek(HEADER_OFFSET);
            int segmentCount = (int) (byteCount / segmentSizeInBytes);
            if (byteCount % segmentSizeInBytes != 0)
                segmentCount++;
            ensureCapacity(byteCount, false);
            byte[] bytes = new byte[segmentSizeInBytes];
            for (int s = 0; s < segmentCount; s++) {
                int read = raFile.read(bytes);
                if (read <= 0)
                    throw new IllegalStateException("segment " + s + " is empty? " + toString());
                // is there a faster method?
                setBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes);
            }
            return true;
        } finally {
            raFile.close();
        }
    } catch (IOException ex) {
        throw new RuntimeException("Problem while loading " + getFullName(), ex);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 33 with RandomAccessFile

use of java.io.RandomAccessFile in project graphhopper by graphhopper.

the class UnsafeDataAccess method flush.

@Override
public void flush() {
    if (isClosed())
        throw new IllegalStateException("already closed");
    try {
        RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw");
        try {
            long len = getCapacity();
            writeHeader(raFile, len, segmentSizeInBytes);
            raFile.seek(HEADER_OFFSET);
            byte[] bytes = new byte[segmentSizeInBytes];
            int segs = getSegments();
            for (int s = 0; s < segs; s++) {
                getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes);
                raFile.write(bytes);
            }
        } finally {
            raFile.close();
        }
    } catch (Exception ex) {
        throw new RuntimeException("Couldn't store bytes to " + toString(), ex);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException)

Example 34 with RandomAccessFile

use of java.io.RandomAccessFile in project graphhopper by graphhopper.

the class NativeFSLockFactory method main.

public static void main(String[] args) throws IOException {
    // trying FileLock mechanics in different processes
    File file = new File("tmp.lock");
    file.createNewFile();
    FileChannel channel = new RandomAccessFile(file, "r").getChannel();
    boolean shared = true;
    FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared);
    System.out.println("locked " + lock1);
    System.in.read();
    System.out.println("release " + lock1);
    lock1.release();
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 35 with RandomAccessFile

use of java.io.RandomAccessFile in project graphhopper by graphhopper.

the class RAMDataAccess method flush.

@Override
public void flush() {
    if (closed)
        throw new IllegalStateException("already closed");
    if (!store)
        return;
    try {
        RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw");
        try {
            long len = getCapacity();
            writeHeader(raFile, len, segmentSizeInBytes);
            raFile.seek(HEADER_OFFSET);
            // raFile.writeInt() <- too slow, so copy into byte array
            for (int s = 0; s < segments.length; s++) {
                byte[] area = segments[s];
                raFile.write(area);
            }
        } finally {
            raFile.close();
        }
    } catch (Exception ex) {
        throw new RuntimeException("Couldn't store bytes to " + toString(), ex);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException)

Aggregations

RandomAccessFile (java.io.RandomAccessFile)916 IOException (java.io.IOException)446 File (java.io.File)366 FileChannel (java.nio.channels.FileChannel)139 FileNotFoundException (java.io.FileNotFoundException)88 Test (org.junit.Test)82 ByteBuffer (java.nio.ByteBuffer)81 FileLock (java.nio.channels.FileLock)66 EOFException (java.io.EOFException)50 FileOutputStream (java.io.FileOutputStream)50 FileInputStream (java.io.FileInputStream)39 InputStream (java.io.InputStream)36 MappedByteBuffer (java.nio.MappedByteBuffer)36 Random (java.util.Random)27 ByteArrayInputStream (java.io.ByteArrayInputStream)25 BufferedInputStream (java.io.BufferedInputStream)22 DataInputStream (java.io.DataInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 Configuration (org.apache.hadoop.conf.Configuration)17 DataOutputStream (java.io.DataOutputStream)14