Search in sources :

Example 51 with RandomAccessFile

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

the class MiniThumbFile method saveMiniThumbToFile.

public synchronized void saveMiniThumbToFile(byte[] data, long id, long magic) throws IOException {
    RandomAccessFile r = miniThumbDataFile();
    if (r == null)
        return;
    long pos = id * BYTES_PER_MINTHUMB;
    FileLock lock = null;
    try {
        if (data != null) {
            if (data.length > BYTES_PER_MINTHUMB - HEADER_SIZE) {
                // not enough space to store it.
                return;
            }
            mBuffer.clear();
            mBuffer.put((byte) 1);
            mBuffer.putLong(magic);
            mBuffer.putInt(data.length);
            mBuffer.put(data);
            mBuffer.flip();
            lock = mChannel.lock(pos, BYTES_PER_MINTHUMB, false);
            mChannel.write(mBuffer, pos);
        }
    } catch (IOException ex) {
        Log.e(TAG, "couldn't save mini thumbnail data for " + id + "; ", ex);
        throw ex;
    } catch (RuntimeException ex) {
        // Other NIO related exception like disk full, read only channel..etc
        Log.e(TAG, "couldn't save mini thumbnail data for " + id + "; disk full or mount read-only? " + ex.getClass());
    } finally {
        try {
            if (lock != null)
                lock.release();
        } catch (IOException ex) {
        // ignore it.
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Example 52 with RandomAccessFile

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

the class TestBrokenStoreRecovery method trimFileToSize.

private void trimFileToSize(File theFile, int toSize) throws IOException {
    try (FileChannel theChannel = new RandomAccessFile(theFile, "rw").getChannel()) {
        theChannel.truncate(toSize);
        theChannel.force(false);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel)

Example 53 with RandomAccessFile

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

the class LabelScanStoreTest method scrambleFile.

public static void scrambleFile(Random random, File file) throws IOException {
    try (RandomAccessFile fileAccess = new RandomAccessFile(file, "rw");
        FileChannel channel = fileAccess.getChannel()) {
        // The files will be small, so OK to allocate a buffer for the full size
        byte[] bytes = new byte[(int) channel.size()];
        putRandomBytes(random, bytes);
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        channel.position(0);
        channel.write(buffer);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer)

Example 54 with RandomAccessFile

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

the class ObjectPersister method diskSize.

/**
     * Recursively computes the size on disk of a file or directory.
     *
     * @param file the file or directory to check
     * @return sum of RandomAccessFile.length() for all files at this location
     * @throws IOException if any error occurs while reading file sizes
     */
private static long diskSize(File file) throws IOException {
    if (file.isFile()) {
        // Base case
        long size;
        RandomAccessFile f = null;
        try {
            f = new RandomAccessFile(file, "r");
            size = f.length();
        } finally {
            if (f != null) {
                f.close();
            }
        }
        return size;
    }
    // Recursive case
    File[] files = file.listFiles();
    long sum = 0;
    for (File file2 : files) {
        sum += diskSize(file2);
    }
    return sum;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 55 with RandomAccessFile

use of java.io.RandomAccessFile in project druid by druid-io.

the class LogUtils method streamFile.

/**
   * Open a stream to a file.
   *
   * @param offset If zero, stream the entire log. If positive, read from this byte position onwards. If negative,
   *               read this many bytes from the end of the file.
   *
   * @return input supplier for this log, if available from this provider
   */
public static InputStream streamFile(final File file, final long offset) throws IOException {
    final RandomAccessFile raf = new RandomAccessFile(file, "r");
    final long rafLength = raf.length();
    if (offset > 0) {
        raf.seek(offset);
    } else if (offset < 0 && offset < rafLength) {
        raf.seek(Math.max(0, rafLength + offset));
    }
    return Channels.newInputStream(raf.getChannel());
}
Also used : RandomAccessFile(java.io.RandomAccessFile)

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