Search in sources :

Example 61 with FileChannel

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();
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 62 with FileChannel

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;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) FileInputStream(java.io.FileInputStream)

Example 63 with FileChannel

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);
    }
}
Also used : DownloadRange(zlc.season.rxdownload2.entity.DownloadRange) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel)

Example 64 with FileChannel

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);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel)

Example 65 with FileChannel

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);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) InputStream(java.io.InputStream) DownloadStatus(zlc.season.rxdownload2.entity.DownloadStatus) IOException(java.io.IOException)

Aggregations

FileChannel (java.nio.channels.FileChannel)676 IOException (java.io.IOException)247 ByteBuffer (java.nio.ByteBuffer)215 File (java.io.File)199 FileInputStream (java.io.FileInputStream)177 FileOutputStream (java.io.FileOutputStream)167 RandomAccessFile (java.io.RandomAccessFile)151 Test (org.junit.Test)95 MappedByteBuffer (java.nio.MappedByteBuffer)82 Path (java.nio.file.Path)42 FileLock (java.nio.channels.FileLock)34 FileNotFoundException (java.io.FileNotFoundException)32 ArrayList (java.util.ArrayList)14 Random (java.util.Random)13 OutputStream (java.io.OutputStream)12 ReadableByteChannel (java.nio.channels.ReadableByteChannel)12 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)11 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)10 LinkedList (java.util.LinkedList)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9