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.
}
}
}
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);
}
}
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);
}
}
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;
}
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());
}
Aggregations