use of java.nio.channels.FileChannel in project RxDownload by ssseasonnn.
the class FileHelper method fileNotComplete.
public boolean fileNotComplete(File tempFile) throws IOException {
RandomAccessFile record = null;
FileChannel channel = null;
try {
record = new RandomAccessFile(tempFile, ACCESS);
channel = record.getChannel();
MappedByteBuffer buffer = channel.map(READ_WRITE, 0, RECORD_FILE_TOTAL_SIZE);
long startByte;
long endByte;
for (int i = 0; i < maxThreads; i++) {
startByte = buffer.getLong();
endByte = buffer.getLong();
if (startByte <= endByte) {
return true;
}
}
return false;
} finally {
closeQuietly(channel);
closeQuietly(record);
}
}
use of java.nio.channels.FileChannel in project CoreNLP by stanfordnlp.
the class FileBackedCache method acquireFileLock.
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
protected FileSemaphore acquireFileLock(File f) throws IOException {
assert canonicalFile.intern(f.getCanonicalFile()) == f;
synchronized (f) {
// Check semaphore
synchronized (fileLocks) {
if (fileLocks.containsKey(f)) {
FileSemaphore sem = fileLocks.get(f);
if (sem.isActive()) {
sem.take();
return sem;
} else {
fileLocks.remove(f);
}
}
}
// Get the channel
FileChannel channel = new RandomAccessFile(f, "rw").getChannel();
FileLock lockOrNull = null;
// Try the lock
for (int i = 0; i < 1000; ++i) {
lockOrNull = channel.tryLock();
if (lockOrNull == null || !lockOrNull.isValid()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log(e);
throw new RuntimeInterruptedException(e);
}
if (i % 60 == 59) {
warn("FileBackedCache", "Lock still busy after " + ((i + 1) / 60) + " minutes");
}
//noinspection UnnecessaryContinue
continue;
} else {
break;
}
}
if (lockOrNull == null) {
warn("FileBackedCache", "Could not acquire file lock! Continuing without lock");
}
// Return
FileSemaphore sem = new FileSemaphore(lockOrNull, channel);
synchronized (fileLocks) {
fileLocks.put(f, sem);
}
return sem;
}
}
use of java.nio.channels.FileChannel in project android-common by litesuits.
the class FileUtil method fileChannelCopy.
public static void fileChannelCopy(File s, File t) {
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
//得到对应的文件通道
FileChannel in = fi.getChannel();
//得到对应的文件通道
FileChannel out = fo.getChannel();
//连接两个通道,并且从in通道读取,然后写入out通道
in.transferTo(0, in.size(), out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fo != null)
fo.close();
if (fi != null)
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of java.nio.channels.FileChannel in project min by macournoyer.
the class File method read.
// Taken from http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
public static String read(String path) throws MinException {
try {
FileInputStream stream = new FileInputStream(new java.io.File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return java.nio.charset.Charset.defaultCharset().decode(bb).toString();
} finally {
stream.close();
}
} catch (IOException e) {
throw new MinException(e);
}
}
use of java.nio.channels.FileChannel in project tape by square.
the class QueueFile method expandIfNecessary.
/**
* If necessary, expands the file to accommodate an additional element of the given length.
*
* @param dataLength length of data being added
*/
private void expandIfNecessary(long dataLength) throws IOException {
long elementLength = Element.HEADER_LENGTH + dataLength;
long remainingBytes = remainingBytes();
if (remainingBytes >= elementLength)
return;
// Expand.
long previousLength = fileLength;
long newLength;
// Double the length until we can fit the new data.
do {
remainingBytes += previousLength;
newLength = previousLength << 1;
previousLength = newLength;
} while (remainingBytes < elementLength);
setLength(newLength);
// Calculate the position of the tail end of the data in the ring buffer
long endOfLastElement = wrapPosition(last.position + Element.HEADER_LENGTH + last.length);
long count = 0;
// If the buffer is split, we need to make it contiguous
if (endOfLastElement <= first.position) {
FileChannel channel = raf.getChannel();
// destination position
channel.position(fileLength);
count = endOfLastElement - headerLength;
if (channel.transferTo(headerLength, count, channel) != count) {
throw new AssertionError("Copied insufficient number of bytes!");
}
}
// Commit the expansion.
if (last.position < first.position) {
long newLastPosition = fileLength + last.position - headerLength;
writeHeader(newLength, elementCount, first.position, newLastPosition);
last = new Element(newLastPosition, last.length);
} else {
writeHeader(newLength, elementCount, first.position, last.position);
}
fileLength = newLength;
if (zero) {
ringErase(headerLength, count);
}
}
Aggregations