Search in sources :

Example 96 with RandomAccessFile

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

the class AbstractService method run.

@Override
public void run() {
    Context applicationContext = getApplicationContext();
    File resultFile = new File(applicationContext.getFilesDir(), getId());
    try {
        // Append a constant value in result file, if services crashed and is relaunched, size
        // of the result file will be too big.
        RandomAccessFile raf = new RandomAccessFile(resultFile, "rw");
        raf.seek(raf.length());
        Log.i(TAG, "Writing 0x42434445 at " + raf.length() + " in " + resultFile.getPath());
        raf.writeInt(0x42434445);
        raf.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    MultiDex.install(applicationContext);
    Log.i(TAG, "Multi dex installation done.");
    int value = getValue();
    Log.i(TAG, "Saving the result (" + value + ") to " + resultFile.getPath());
    try {
        // Append the check value in result file, keeping the constant values already written.
        RandomAccessFile raf = new RandomAccessFile(resultFile, "rw");
        raf.seek(raf.length());
        Log.i(TAG, "Writing result at " + raf.length() + " in " + resultFile.getPath());
        raf.writeInt(value);
        raf.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        // Writing end of processing flags, the existence of the file is the criteria
        RandomAccessFile raf = new RandomAccessFile(new File(applicationContext.getFilesDir(), getId() + ".complete"), "rw");
        Log.i(TAG, "creating complete file " + resultFile.getPath());
        raf.writeInt(0x32333435);
        raf.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Context(android.content.Context) RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 97 with RandomAccessFile

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

the class MiniThumbFile method miniThumbIndexFile.

private RandomAccessFile miniThumbIndexFile() {
    if (mMiniThumbIndexFile == null) {
        removeOldIndexFile();
        String path = randomAccessIndexFilePath(MINI_THUMB_DATA_FILE_VERSION);
        File directory = new File(path).getParentFile();
        if (!directory.isDirectory()) {
            if (!directory.mkdirs()) {
                Log.e(TAG, "Unable to create .thumbnails directory " + directory.toString());
            }
        }
        File f = new File(path);
        try {
            mMiniThumbIndexFile = new RandomAccessFile(f, "rw");
        } catch (IOException ex) {
            // thumbnails.
            try {
                mMiniThumbIndexFile = new RandomAccessFile(f, "r");
            } catch (IOException ex2) {
                // ignore exception
                Log.e(TAG, "miniThumbIndexFile open r exception: " + f);
            }
        }
        if (mMiniThumbIndexFile != null) {
            mIndexChannel = mMiniThumbIndexFile.getChannel();
        }
    }
    return mMiniThumbIndexFile;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 98 with RandomAccessFile

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

the class MiniThumbFile method getIndex.

/**
    * Get the index of thumbnail, which is the real saving location.
    * @param id the raw id in Mediaprovider database.
    * @param create when you want to create a new thumbnail, set to true; when generally query 
    * thumbnail saved index, set to false.
    */
private long getIndex(long id, boolean create) {
    RandomAccessFile r = miniThumbIndexFile();
    ByteBuffer buf = ByteBuffer.allocateDirect(BYTES_PER_MINTHUMB_INDEX);
    if (r != null) {
        long pos = 0;
        // (id+1)*BYTES_PER_MINTHUMB_INDEX.
        if (!create) {
            pos = (id + 1) * BYTES_PER_MINTHUMB_INDEX;
        }
        FileLock lock = null;
        try {
            buf.clear();
            buf.limit(BYTES_PER_MINTHUMB_INDEX);
            lock = mIndexChannel.lock(pos, BYTES_PER_MINTHUMB_INDEX, false);
            //check that we can read the following 8 bytes
            //which is the index position of thumbnail.
            int read = mIndexChannel.read(buf, pos);
            if (read == BYTES_PER_MINTHUMB_INDEX) {
                buf.position(0);
                if (create) {
                    //first, write next index.
                    long now = buf.getLong();
                    buf.clear();
                    buf.position(0);
                    buf.putLong(++now);
                    buf.flip();
                    int write = mIndexChannel.write(buf, pos);
                    //second, write this id's index
                    if (BYTES_PER_MINTHUMB_INDEX == write) {
                        if (lock != null)
                            lock.release();
                        pos = (id + 1) * BYTES_PER_MINTHUMB_INDEX;
                        lock = mIndexChannel.lock(pos, BYTES_PER_MINTHUMB_INDEX, false);
                        buf.flip();
                        write = mIndexChannel.write(buf, pos);
                        if (debug)
                            Log.d(TAG, "getIndex with create. index: " + now + "corresponding id: " + id + ", index is: " + pos);
                    }
                    return now;
                } else {
                    long p = buf.getLong();
                    if (debug)
                        Log.d(TAG, "getIndex with no create. index: " + p);
                    return p;
                }
            } else if (-1 == read) {
                //If the index file is empty, initialize first index to 0.
                if (0 == r.length()) {
                    buf.clear();
                    buf.position(0);
                    buf.putLong(0);
                    buf.flip();
                    int write = mIndexChannel.write(buf, 0);
                    if (debug)
                        Log.d(TAG, "initialize first index");
                    if (BYTES_PER_MINTHUMB_INDEX == write)
                        return 0;
                }
            }
        } catch (IOException ex) {
            Log.e(TAG, "Got exception checking file index: ", ex);
        } catch (RuntimeException ex) {
            // Other NIO related exception like disk full, read only channel..etc
            Log.e(TAG, "Got exception when reading index, id = " + id + ", disk full or mount read-only? " + ex.getClass());
        } finally {
            try {
                if (lock != null)
                    lock.release();
            } catch (IOException ex) {
                // ignore it.
                Log.e(TAG, "release lock: ", ex);
            }
        }
    }
    return 0;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 99 with RandomAccessFile

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

the class MiniThumbFile method getMiniThumbFromFile.

/**
     * Gallery app can use this method to retrieve mini-thumbnail. Full size
     * images share the same IDs with their corresponding thumbnails.
     *
     * @param id the ID of the image (same of full size image).
     * @param data the buffer to store mini-thumbnail.
     */
public synchronized byte[] getMiniThumbFromFile(long id, byte[] data) {
    RandomAccessFile r = miniThumbDataFile();
    if (r == null)
        return null;
    long pos = getIndex(id, false);
    if (pos < 0)
        return null;
    pos *= BYTES_PER_MINTHUMB;
    FileLock lock = null;
    try {
        mBuffer.clear();
        lock = mChannel.lock(pos, BYTES_PER_MINTHUMB, true);
        int size = mChannel.read(mBuffer, pos);
        if (size > 1 + 8 + 4) {
            // flag, magic, length
            mBuffer.position(0);
            byte flag = mBuffer.get();
            long magic = mBuffer.getLong();
            int length = mBuffer.getInt();
            if (size >= 1 + 8 + 4 + length && length != 0 && magic != 0 && flag == 1 && data.length >= length) {
                mBuffer.get(data, 0, length);
                return data;
            }
        }
    } catch (IOException ex) {
        Log.w(TAG, "got exception when reading thumbnail id=" + id + ", exception: " + ex);
    } catch (RuntimeException ex) {
        // Other NIO related exception like disk full, read only channel..etc
        Log.e(TAG, "Got exception when reading thumbnail, id = " + id + ", disk full or mount read-only? " + ex.getClass());
    } finally {
        try {
            if (lock != null)
                lock.release();
        } catch (IOException ex) {
        // ignore it.
        }
    }
    return null;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Example 100 with RandomAccessFile

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

the class MiniThumbFile method getMagic.

// Get the magic number for the specified id in the mini-thumb file.
// Returns 0 if the magic is not available.
public synchronized long getMagic(long id) {
    // check the mini thumb file for the right data.  Right is
    // defined as having the right magic number at the offset
    // reserved for this "id".
    RandomAccessFile r = miniThumbDataFile();
    if (r != null) {
        long pos = getIndex(id, false);
        if (pos < 0)
            return 0;
        pos *= BYTES_PER_MINTHUMB;
        FileLock lock = null;
        try {
            mBuffer.clear();
            mBuffer.limit(1 + 8);
            lock = mChannel.lock(pos, 1 + 8, true);
            // (1 for the "status" and 8 for the long)
            if (mChannel.read(mBuffer, pos) == 9) {
                mBuffer.position(0);
                if (mBuffer.get() == 1) {
                    return mBuffer.getLong();
                }
            }
        } catch (IOException ex) {
            Log.v(TAG, "Got exception checking file magic: ", ex);
        } catch (RuntimeException ex) {
            // Other NIO related exception like disk full, read only channel..etc
            Log.e(TAG, "Got exception when reading magic, id = " + id + ", disk full or mount read-only? " + ex.getClass());
        } finally {
            try {
                if (lock != null)
                    lock.release();
            } catch (IOException ex) {
            // ignore it.
            }
        }
    }
    return 0;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

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