use of java.nio.channels.FileChannel in project react-native-image-picker by marcshilling.
the class ImagePickerModule method moveFile.
/**
* Move a file from one location to another.
*
* This is done via copy + deletion, because Android will throw an error
* if you try to move a file across mount points, e.g. to the SD card.
*/
private void moveFile(final File oldFile, final File newFile) throws IOException {
FileChannel oldChannel = null;
FileChannel newChannel = null;
try {
oldChannel = new FileInputStream(oldFile).getChannel();
newChannel = new FileOutputStream(newFile).getChannel();
oldChannel.transferTo(0, oldChannel.size(), newChannel);
oldFile.delete();
} finally {
try {
if (oldChannel != null)
oldChannel.close();
if (newChannel != null)
newChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of java.nio.channels.FileChannel in project dagger by square.
the class BuildLogValidator method getBuildOutput.
private String getBuildOutput(File buildLogfile) throws Throwable {
String buildOutput;
FileInputStream stream = new FileInputStream(buildLogfile);
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
buildOutput = Charset.defaultCharset().decode(buf).toString();
} finally {
stream.close();
}
if (buildOutput == null) {
throw new Exception("Could not read build output");
}
return buildOutput;
}
use of java.nio.channels.FileChannel in project RxDownload by ssseasonnn.
the class FileHelper method readDownloadRange.
public DownloadRange readDownloadRange(File tempFile, int i) throws IOException {
RandomAccessFile record = null;
FileChannel channel = null;
try {
record = new RandomAccessFile(tempFile, ACCESS);
channel = record.getChannel();
MappedByteBuffer buffer = channel.map(READ_WRITE, i * EACH_RECORD_SIZE, (i + 1) * EACH_RECORD_SIZE);
long startByte = buffer.getLong();
long endByte = buffer.getLong();
return new DownloadRange(startByte, endByte);
} finally {
closeQuietly(channel);
closeQuietly(record);
}
}
use of java.nio.channels.FileChannel in project RxDownload by ssseasonnn.
the class FileHelper method tempFileDamaged.
public boolean tempFileDamaged(File tempFile, long fileLength) 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 recordTotalSize = buffer.getLong(RECORD_FILE_TOTAL_SIZE - 8) + 1;
return recordTotalSize != fileLength;
} finally {
closeQuietly(channel);
closeQuietly(record);
}
}
use of java.nio.channels.FileChannel in project RxDownload by ssseasonnn.
the class FileHelper method saveFile.
public void saveFile(FlowableEmitter<DownloadStatus> emitter, int i, File tempFile, File saveFile, ResponseBody response) {
RandomAccessFile record = null;
FileChannel recordChannel = null;
RandomAccessFile save = null;
FileChannel saveChannel = null;
InputStream inStream = null;
try {
try {
int readLen;
byte[] buffer = new byte[2048];
DownloadStatus status = new DownloadStatus();
record = new RandomAccessFile(tempFile, ACCESS);
recordChannel = record.getChannel();
MappedByteBuffer recordBuffer = recordChannel.map(READ_WRITE, 0, RECORD_FILE_TOTAL_SIZE);
int startIndex = i * EACH_RECORD_SIZE;
long start = recordBuffer.getLong(startIndex);
// long end = recordBuffer.getLong(startIndex + 8);
long totalSize = recordBuffer.getLong(RECORD_FILE_TOTAL_SIZE - 8) + 1;
status.setTotalSize(totalSize);
save = new RandomAccessFile(saveFile, ACCESS);
saveChannel = save.getChannel();
inStream = response.byteStream();
while ((readLen = inStream.read(buffer)) != -1 && !emitter.isCancelled()) {
MappedByteBuffer saveBuffer = saveChannel.map(READ_WRITE, start, readLen);
start += readLen;
saveBuffer.put(buffer, 0, readLen);
recordBuffer.putLong(startIndex, start);
status.setDownloadSize(totalSize - getResidue(recordBuffer));
emitter.onNext(status);
}
emitter.onComplete();
} finally {
closeQuietly(record);
closeQuietly(recordChannel);
closeQuietly(save);
closeQuietly(saveChannel);
closeQuietly(inStream);
closeQuietly(response);
}
} catch (IOException e) {
emitter.onError(e);
}
}
Aggregations