Example usage for java.nio.channels FileChannel transferTo

List of usage examples for java.nio.channels FileChannel transferTo

Introduction

In this page you can find the example usage for java.nio.channels FileChannel transferTo.

Prototype

public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;

Source Link

Document

Transfers bytes from this channel's file to the given writable byte channel.

Usage

From source file:org.apache.camel.component.file.FileOperations.java

private void writeFileByFile(File source, File target) throws IOException {
    FileChannel in = new FileInputStream(source).getChannel();
    FileChannel out = null;//  w w  w .  ja  v  a 2  s. c  o  m
    try {
        out = prepareOutputFileChannel(target, out);

        if (LOG.isTraceEnabled()) {
            LOG.trace("Using FileChannel to transfer from: " + in + " to: " + out);
        }

        long size = in.size();
        long position = 0;
        while (position < size) {
            position += in.transferTo(position, endpoint.getBufferSize(), out);
        }
    } finally {
        IOHelper.close(in, source.getName(), LOG);
        IOHelper.close(out, source.getName(), LOG);
    }
}

From source file:org.etudes.jforum.view.install.InstallAction.java

private void copyFile(String from, String to) throws Exception {
    FileChannel source = new FileInputStream(new File(from)).getChannel();
    FileChannel dest = new FileOutputStream(new File(to)).getChannel();

    source.transferTo(0, source.size(), dest);
    source.close();//  www  . java2  s. c  o  m
    dest.close();
}

From source file:org.apache.spark.shuffle.sort.UnsafeShuffleWriter.java

/**
 * Merges spill files by using NIO's transferTo to concatenate spill partitions' bytes.
 * This is only safe when the IO compression codec and serializer support concatenation of
 * serialized streams./* ww w .j  ava 2 s .co  m*/
 *
 * @return the partition lengths in the merged file.
 */
private long[] mergeSpillsWithTransferTo(SpillInfo[] spills, File outputFile) throws IOException {
    assert (spills.length >= 2);
    final int numPartitions = partitioner.numPartitions();
    final long[] partitionLengths = new long[numPartitions];
    final FileChannel[] spillInputChannels = new FileChannel[spills.length];
    final long[] spillInputChannelPositions = new long[spills.length];
    FileChannel mergedFileOutputChannel = null;

    boolean threwException = true;
    try {
        for (int i = 0; i < spills.length; i++) {
            spillInputChannels[i] = new FileInputStream(spills[i].file).getChannel();
        }
        // This file needs to opened in append mode in order to work around a Linux kernel bug that
        // affects transferTo; see SPARK-3948 for more details.
        mergedFileOutputChannel = new FileOutputStream(outputFile, true).getChannel();

        long bytesWrittenToMergedFile = 0;
        for (int partition = 0; partition < numPartitions; partition++) {
            for (int i = 0; i < spills.length; i++) {
                final long partitionLengthInSpill = spills[i].partitionLengths[partition];
                long bytesToTransfer = partitionLengthInSpill;
                final FileChannel spillInputChannel = spillInputChannels[i];
                final long writeStartTime = System.nanoTime();
                while (bytesToTransfer > 0) {
                    final long actualBytesTransferred = spillInputChannel.transferTo(
                            spillInputChannelPositions[i], bytesToTransfer, mergedFileOutputChannel);
                    spillInputChannelPositions[i] += actualBytesTransferred;
                    bytesToTransfer -= actualBytesTransferred;
                }
                writeMetrics.incWriteTime(System.nanoTime() - writeStartTime);
                bytesWrittenToMergedFile += partitionLengthInSpill;
                partitionLengths[partition] += partitionLengthInSpill;
            }
        }
        // Check the position after transferTo loop to see if it is in the right position and raise an
        // exception if it is incorrect. The position will not be increased to the expected length
        // after calling transferTo in kernel version 2.6.32. This issue is described at
        // https://bugs.openjdk.java.net/browse/JDK-7052359 and SPARK-3948.
        if (mergedFileOutputChannel.position() != bytesWrittenToMergedFile) {
            throw new IOException("Current position " + mergedFileOutputChannel.position()
                    + " does not equal expected " + "position " + bytesWrittenToMergedFile
                    + " after transferTo. Please check your kernel"
                    + " version to see if it is 2.6.32, as there is a kernel bug which will lead to "
                    + "unexpected behavior when using transferTo. You can set spark.file.transferTo=false "
                    + "to disable this NIO feature.");
        }
        threwException = false;
    } finally {
        // To avoid masking exceptions that caused us to prematurely enter the finally block, only
        // throw exceptions during cleanup if threwException == false.
        for (int i = 0; i < spills.length; i++) {
            assert (spillInputChannelPositions[i] == spills[i].file.length());
            Closeables.close(spillInputChannels[i], threwException);
        }
        Closeables.close(mergedFileOutputChannel, threwException);
    }
    return partitionLengths;
}

From source file:org.fhcrc.cpl.viewer.quant.gui.QuantitationReviewer.java

/**
 * Remove all the events the user has designated as 'bad' from the pepXML file they came from
 * TODO: report how many events weren't found
 * @param quantEvents/*from   w w  w.j ava 2 s  . c  o m*/
 * @param pepXmlFile
 * @param outFile
 * @throws IOException
 * @throws XMLStreamException
 */
public static void filterBadEventsFromFile(List<QuantEvent> quantEvents, File pepXmlFile, File outFile)
        throws IOException, XMLStreamException {
    Map<String, List<Integer>> fractionBadQuantScanListMap = new HashMap<String, List<Integer>>();
    Map<String, List<Integer>> fractionBadIDScanListMap = new HashMap<String, List<Integer>>();
    Map<String, Map<Integer, Float>> fractionScanNewRatioMap = new HashMap<String, Map<Integer, Float>>();

    for (QuantEvent quantEvent : quantEvents) {
        String fraction = quantEvent.getFraction();

        if (quantEvent.getIdCurationStatus() == QuantEvent.CURATION_STATUS_BAD) {
            List<Integer> thisFractionList = fractionBadIDScanListMap.get(fraction);
            if (thisFractionList == null) {
                thisFractionList = new ArrayList<Integer>();
                fractionBadIDScanListMap.put(fraction, thisFractionList);
            }
            thisFractionList.add(quantEvent.getScan());
            for (QuantEvent otherEvent : quantEvent.getOtherEvents())
                if (!thisFractionList.contains(otherEvent.getScan()))
                    thisFractionList.add(otherEvent.getScan());
            _log.debug("Stripping ID for " + (quantEvent.getOtherEvents().size() + 1) + " events for peptide "
                    + quantEvent.getPeptide() + " from fraction " + fraction);
        }
        //only if the ID was unknown or good do we check quant -- quant is automatically
        //filtered for bad IDs
        else if (quantEvent.getQuantCurationStatus() == QuantEvent.CURATION_STATUS_BAD) {
            List<Integer> thisFractionList = fractionBadQuantScanListMap.get(fraction);
            if (thisFractionList == null) {
                thisFractionList = new ArrayList<Integer>();
                fractionBadQuantScanListMap.put(fraction, thisFractionList);
            }
            thisFractionList.add(quantEvent.getScan());
            int numOtherEvents = 0;
            if (quantEvent.getOtherEvents() != null) {
                for (QuantEvent otherEvent : quantEvent.getOtherEvents())
                    if (!thisFractionList.contains(otherEvent.getScan()))
                        thisFractionList.add(otherEvent.getScan());
                numOtherEvents = quantEvent.getOtherEvents().size();
            }
            ApplicationContext.infoMessage("Stripping Quantitation for " + (numOtherEvents + 1)
                    + " events for peptide " + quantEvent.getPeptide() + " from fraction " + fraction);
        }
        //dhmay adding 20090723
        else if (quantEvent.getQuantCurationStatus() == QuantEvent.CURATION_STATUS_RATIO_ONEPEAK) {
            Map<Integer, Float> thisFractionMap = fractionScanNewRatioMap.get(fraction);
            if (thisFractionMap == null) {
                thisFractionMap = new HashMap<Integer, Float>();
                fractionScanNewRatioMap.put(fraction, thisFractionMap);
            }
            float newRatio = quantEvent.getRatioOnePeak();
            thisFractionMap.put(quantEvent.getScan(), newRatio);
            for (QuantEvent otherEvent : quantEvent.getOtherEvents())
                if (!thisFractionMap.containsKey(otherEvent.getScan()))
                    thisFractionMap.put(otherEvent.getScan(), newRatio);
            ApplicationContext.infoMessage(
                    "Changing ratios to " + newRatio + " for " + (quantEvent.getOtherEvents().size() + 1)
                            + " events for peptide " + quantEvent.getPeptide() + " from fraction " + fraction);
        }
    }
    ApplicationContext.infoMessage("OK, decided what to do.  Now to strip the IDs from the pepXML file....");
    File actualOutFile = outFile;
    String dummyOwner = "DUMMY_OWNER_QURATE_STRIP";
    boolean sameInputAndOutput = pepXmlFile.getAbsolutePath().equals(outFile.getAbsolutePath());
    if (sameInputAndOutput)
        actualOutFile = TempFileManager.createTempFile("temp_quratestrip_" + outFile.getName(), dummyOwner);

    StripQuantOrIDPepXmlRewriter quantStripper = new StripQuantOrIDPepXmlRewriter(pepXmlFile, actualOutFile,
            fractionBadIDScanListMap, fractionBadQuantScanListMap, fractionScanNewRatioMap);
    quantStripper.rewrite();
    quantStripper.close();
    if (sameInputAndOutput) {
        //file-copying.  Java 1.6 still doesn't have a reasonable and concise way to do it.
        FileChannel inChannel = new FileInputStream(actualOutFile).getChannel();
        FileChannel outChannel = new FileOutputStream(outFile).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e) {
            throw e;
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
        TempFileManager.deleteTempFiles(dummyOwner);
    }
    ApplicationContext.infoMessage("Done!  Saved stripped file to " + outFile.getAbsolutePath());
}

From source file:com.vmware.photon.controller.deployer.deployengine.DockerProvisioner.java

public void createImageFromTar(String filePath, String imageName) {
    if (StringUtils.isBlank(imageName)) {
        throw new IllegalArgumentException("imageName field cannot be null or blank");
    }/*from ww w .j  a  v  a2  s.  c  om*/
    if (StringUtils.isBlank(filePath)) {
        throw new IllegalArgumentException("filePath field cannot be null or blank");
    }

    File tarFile = new File(filePath);
    try (FileInputStream fileInputStream = new FileInputStream(tarFile)) {
        FileChannel in = fileInputStream.getChannel();

        String endpoint = getImageLoadEndpoint();
        URL url = new URL(endpoint);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept", "application/json");
        connection.setDoOutput(true);
        WritableByteChannel out = Channels.newChannel(connection.getOutputStream());
        in.transferTo(0, tarFile.length(), out);
        in.close();
        out.close();

        int responseCode = connection.getResponseCode();
        if (!String.valueOf(responseCode).startsWith("2")) {
            throw new RuntimeException(
                    "Docker Endpoint cannot load image from tar. Failed with response code " + responseCode);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Xtext2SonarM2T.launcher.popupMenus.AcceleoGenerateGenerateSonarQubePluginAction.java

@SuppressWarnings("resource")
private void copyFile(File source, File target, String name) throws IOException {
    if (!target.exists()) {
        target.createNewFile();//from   w  w  w  . j ava 2  s.c  om
    }

    //updateNaming(source, name);

    FileChannel sourceChannel;
    FileChannel targetChannel;

    sourceChannel = new FileInputStream(source).getChannel();
    targetChannel = new FileOutputStream(target).getChannel();
    sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);

    if (sourceChannel != null) {
        sourceChannel.close();
    }

    if (targetChannel != null) {
        targetChannel.close();
    }
}

From source file:org.alfresco.repo.content.AbstractContentWriter.java

/**
 * {@inheritDoc}//from   w  ww  .  j av  a2 s .  c o  m
 */
public FileChannel getFileChannel(boolean truncate) throws ContentIOException {
    /*
     * By calling this method, clients indicate that they wish to make random
     * changes to the file.  It is possible that the client might only want
     * to update a tiny proportion of the file (truncate == false) or
     * start afresh (truncate == true).
     * 
     * Where the underlying support is not present for this method, a temporary
     * file will be used as a substitute.  When the write is complete, the
     * results are copied directly to the underlying channel.
     */

    // get the underlying implementation's best writable channel
    channel = getWritableChannel();
    // now use this channel if it can provide the random access, otherwise spoof it
    FileChannel clientFileChannel = null;
    if (channel instanceof FileChannel) {
        // all the support is provided by the underlying implementation
        clientFileChannel = (FileChannel) channel;
        // copy over the existing content, if required
        if (!truncate && existingContentReader != null) {
            ReadableByteChannel existingContentChannel = existingContentReader.getReadableChannel();
            long existingContentLength = existingContentReader.getSize();
            // copy the existing content
            try {
                clientFileChannel.transferFrom(existingContentChannel, 0, existingContentLength);
                // copy complete
                if (logger.isDebugEnabled()) {
                    logger.debug("Copied content for random access: \n" + "   writer: " + this + "\n"
                            + "   existing: " + existingContentReader);
                }
            } catch (IOException e) {
                throw new ContentIOException("Failed to copy from existing content to enable random access: \n"
                        + "   writer: " + this + "\n" + "   existing: " + existingContentReader, e);
            } finally {
                try {
                    existingContentChannel.close();
                } catch (IOException e) {
                }
            }
        }
        // debug
        if (logger.isDebugEnabled()) {
            logger.debug("Content writer provided direct support for FileChannel: \n" + "   writer: " + this);
        }
    } else {
        // No random access support is provided by the implementation.
        // Spoof it by providing a 2-stage write via a temp file
        File tempFile = TempFileProvider.createTempFile("random_write_spoof_", ".bin");
        final FileContentWriter spoofWriter = new FileContentWriter(tempFile, // the file to write to
                getExistingContentReader()); // this ensures that the existing content is pulled in
        // Attach a listener
        // - to ensure that the content gets loaded from the temp file once writing has finished
        // - to ensure that the close call gets passed on to the underlying channel
        ContentStreamListener spoofListener = new ContentStreamListener() {
            public void contentStreamClosed() throws ContentIOException {
                // the spoofed temp channel has been closed, so get a new reader for it
                ContentReader spoofReader = spoofWriter.getReader();
                FileChannel spoofChannel = spoofReader.getFileChannel();
                // upload all the temp content to the real underlying channel
                try {
                    long spoofFileSize = spoofChannel.size();
                    spoofChannel.transferTo(0, spoofFileSize, channel);
                } catch (IOException e) {
                    throw new ContentIOException(
                            "Failed to copy from spoofed temporary channel to permanent channel: \n"
                                    + "   writer: " + this + "\n" + "   temp: " + spoofReader,
                            e);
                } finally {
                    try {
                        spoofChannel.close();
                    } catch (Throwable e) {
                    }
                    try {
                        channel.close();
                    } catch (IOException e) {
                        throw new ContentIOException("Failed to close underlying channel", e);
                    }
                }
            }
        };
        spoofWriter.addListener(spoofListener);
        // we now have the spoofed up channel that the client can work with
        clientFileChannel = spoofWriter.getFileChannel(truncate);
        // debug
        if (logger.isDebugEnabled()) {
            logger.debug("Content writer provided indirect support for FileChannel: \n" + "   writer: " + this
                    + "\n" + "   temp writer: " + spoofWriter);
        }
    }
    // the file is now available for random access
    return clientFileChannel;
}

From source file:dk.netarkivet.common.utils.FileUtils.java

/**
 * Copy file from one location to another. Will silently overwrite an
 * already existing file.//from   w ww .jav a 2s .  c o  m
 *
 * @param from
 *            original to copy
 * @param to
 *            destination of copy
 * @throws IOFailure if an io error occurs while copying file,
 * or the original file does not exist.
 */
public static void copyFile(File from, File to) {
    ArgumentNotValid.checkNotNull(from, "File from");
    ArgumentNotValid.checkNotNull(to, "File to");
    if (!from.exists()) {
        String errMsg = "Original file '" + from.getAbsolutePath() + "' does not exist";
        log.warn(errMsg);
        throw new IOFailure(errMsg);
    }
    try {
        FileInputStream inStream = null;
        FileOutputStream outStream = null;
        FileChannel in = null;
        FileChannel out = null;
        try {
            inStream = new FileInputStream(from);
            outStream = new FileOutputStream(to);
            in = inStream.getChannel();
            out = outStream.getChannel();
            long bytesTransferred = 0;
            do {
                //Note: in.size() is called every loop, because if it should
                //change size, we might end up in an infinite loop trying to
                //copy more bytes than are actually available.
                bytesTransferred += in.transferTo(bytesTransferred,
                        Math.min(Constants.IO_CHUNK_SIZE, in.size() - bytesTransferred), out);
            } while (bytesTransferred < in.size());
        } finally {
            if (inStream != null) {
                inStream.close();
            }
            if (outStream != null) {
                outStream.close();
            }
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    } catch (IOException e) {
        final String errMsg = "Error copying file '" + from.getAbsolutePath() + "' to '" + to.getAbsolutePath()
                + "'";
        log.warn(errMsg, e);
        throw new IOFailure(errMsg, e);
    }
}

From source file:edu.harvard.iq.dataverse.dataaccess.DataFileIO.java

public void copyPath(Path fileSystemPath) throws IOException {
    long newFileSize = -1;
    // if this is a local fileystem file, we'll use a 
    // quick Files.copy method: 
    if (isLocalFile()) {
        Path outputPath = null;/*from   w w  w. ja v  a2 s.c  o  m*/
        try {
            outputPath = getFileSystemPath();
        } catch (IOException ex) {
            outputPath = null;
        }
        if (outputPath != null) {
            Files.copy(fileSystemPath, outputPath, StandardCopyOption.REPLACE_EXISTING);
            newFileSize = outputPath.toFile().length();
        }

    } else {
        // otherwise we'll open a writable byte channel and 
        // copy the source file bytes using Channel.transferTo():

        WritableByteChannel writeChannel = null;
        FileChannel readChannel = null;
        String failureMsg = null;

        try {

            open(DataAccessOption.WRITE_ACCESS);
            writeChannel = getWriteChannel();
            readChannel = new FileInputStream(fileSystemPath.toFile()).getChannel();

            long bytesPerIteration = 16 * 1024; // 16K bytes
            long start = 0;
            while (start < readChannel.size()) {
                readChannel.transferTo(start, bytesPerIteration, writeChannel);
                start += bytesPerIteration;
            }
            newFileSize = readChannel.size();

        } catch (IOException ioex) {
            failureMsg = ioex.getMessage();
            if (failureMsg == null) {
                failureMsg = "Unknown exception occured.";
            }
        } finally {
            if (readChannel != null) {
                try {
                    readChannel.close();
                } catch (IOException e) {
                }
            }
            if (writeChannel != null) {
                try {
                    writeChannel.close();
                } catch (IOException e) {
                }
            }
        }

        if (failureMsg != null) {
            throw new IOException(failureMsg);
        }
    }

    // if it has worked successfully, we also need to reset the size
    // of the object. 
    setSize(newFileSize);
}

From source file:com.cerema.cloud2.operations.UploadFileOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    boolean localCopyPassed = false, nameCheckPassed = false;
    File temporalFile = null, originalFile = new File(mOriginalStoragePath), expectedFile = null;
    try {/*from  ww  w  .j a  v a 2s. c om*/
        // / rename the file to upload, if necessary
        if (!mForceOverwrite) {
            String remotePath = getAvailableRemotePath(client, mRemotePath);
            mWasRenamed = !remotePath.equals(mRemotePath);
            if (mWasRenamed) {
                createNewOCFile(remotePath);
            }
        }
        nameCheckPassed = true;

        String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile); // /
                                                                                            // not
                                                                                            // before
                                                                                            // getAvailableRemotePath()
                                                                                            // !!!
        expectedFile = new File(expectedPath);

        // check location of local file; if not the expected, copy to a
        // temporal file before upload (if COPY is the expected behaviour)
        if (!mOriginalStoragePath.equals(expectedPath)
                && mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_COPY) {

            if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) {
                result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_FULL);
                return result; // error condition when the file should be
                               // copied

            } else {

                String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
                mFile.setStoragePath(temporalPath);
                temporalFile = new File(temporalPath);

                File temporalParent = temporalFile.getParentFile();
                temporalParent.mkdirs();
                if (!temporalParent.isDirectory()) {
                    throw new IOException("Unexpected error: parent directory could not be created");
                }
                temporalFile.createNewFile();
                if (!temporalFile.isFile()) {
                    throw new IOException("Unexpected error: target file could not be created");
                }

                InputStream in = null;
                OutputStream out = null;

                try {

                    // In case document provider schema as 'content://'
                    if (mOriginalStoragePath.startsWith(UriUtils.URI_CONTENT_SCHEME)) {

                        Uri uri = Uri.parse(mOriginalStoragePath);

                        in = MainApp.getAppContext().getContentResolver().openInputStream(uri);
                        out = new FileOutputStream(temporalFile);

                        int nRead;
                        byte[] data = new byte[16384];

                        while (!mCancellationRequested.get() && (nRead = in.read(data, 0, data.length)) != -1) {
                            out.write(data, 0, nRead);
                        }
                        out.flush();

                    } else {
                        if (!mOriginalStoragePath.equals(temporalPath)) { // preventing
                            // weird
                            // but
                            // possible
                            // situation

                            in = new FileInputStream(originalFile);
                            out = new FileOutputStream(temporalFile);
                            byte[] buf = new byte[1024];
                            int len;
                            while (!mCancellationRequested.get() && (len = in.read(buf)) > 0) {
                                out.write(buf, 0, len);
                            }
                        }
                    }

                    if (mCancellationRequested.get()) {
                        result = new RemoteOperationResult(new OperationCancelledException());
                    }

                } catch (Exception e) {
                    result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_COPIED);
                    return result;

                } finally {
                    try {
                        if (in != null)
                            in.close();
                    } catch (Exception e) {
                        Log_OC.d(TAG, "Weird exception while closing input stream for " + mOriginalStoragePath
                                + " (ignoring)", e);
                    }
                    try {
                        if (out != null)
                            out.close();
                    } catch (Exception e) {
                        Log_OC.d(TAG, "Weird exception while closing output stream for " + expectedPath
                                + " (ignoring)", e);
                    }
                }
            }
        }
        localCopyPassed = (result == null);

        /// perform the upload
        if (mChunked
                && (new File(mFile.getStoragePath())).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE) {
            mUploadOperation = new ChunkedUploadRemoteFileOperation(mFile.getStoragePath(),
                    mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict());
        } else {
            mUploadOperation = new UploadRemoteFileOperation(mFile.getStoragePath(), mFile.getRemotePath(),
                    mFile.getMimetype(), mFile.getEtagInConflict());
        }
        Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
        while (listener.hasNext()) {
            mUploadOperation.addDatatransferProgressListener(listener.next());
        }
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }

        result = mUploadOperation.execute(client);

        /// move local temporal file or original file to its corresponding
        // location in the ownCloud local folder
        if (result.isSuccess()) {
            if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_FORGET) {
                mFile.setStoragePath("");
            } else {
                mFile.setStoragePath(expectedPath);
                File fileToMove = null;
                if (temporalFile != null) { // FileUploader.LOCAL_BEHAVIOUR_COPY
                    // ; see where temporalFile was
                    // set
                    fileToMove = temporalFile;
                } else { // FileUploader.LOCAL_BEHAVIOUR_MOVE
                    fileToMove = originalFile;
                }
                if (!expectedFile.equals(fileToMove)) {
                    File expectedFolder = expectedFile.getParentFile();
                    expectedFolder.mkdirs();

                    if (expectedFolder.isDirectory()) {
                        if (!fileToMove.renameTo(expectedFile)) {
                            // try to copy and then delete
                            expectedFile.createNewFile();
                            FileChannel inChannel = new FileInputStream(fileToMove).getChannel();
                            FileChannel outChannel = new FileOutputStream(expectedFile).getChannel();

                            try {
                                inChannel.transferTo(0, inChannel.size(), outChannel);
                                fileToMove.delete();
                            } catch (Exception e) {
                                mFile.setStoragePath(null); // forget the local file
                                // by now, treat this as a success; the file was
                                // uploaded; the user won't like that the local file
                                // is not linked, but this should be a very rare
                                // fail;
                                // the best option could be show a warning message
                                // (but not a fail)
                                // result = new
                                // RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
                                // return result;
                            } finally {
                                if (inChannel != null)
                                    inChannel.close();
                                if (outChannel != null)
                                    outChannel.close();
                            }
                        }

                    } else {
                        mFile.setStoragePath(null);
                    }
                }
            }
            FileDataStorageManager.triggerMediaScan(originalFile.getAbsolutePath());
            FileDataStorageManager.triggerMediaScan(expectedFile.getAbsolutePath());
        } else if (result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED) {
            result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
        }

    } catch (Exception e) {
        result = new RemoteOperationResult(e);

    } finally {
        if (temporalFile != null && !originalFile.equals(temporalFile)) {
            temporalFile.delete();
        }
        if (result == null) {
            result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
        }
        if (result.isSuccess()) {
            Log_OC.i(TAG,
                    "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
        } else {
            if (result.getException() != null) {
                String complement = "";
                if (!nameCheckPassed) {
                    complement = " (while checking file existence in server)";
                } else if (!localCopyPassed) {
                    complement = " (while copying local file to " + FileStorageUtils.getSavePath(mAccount.name)
                            + ")";
                }
                Log_OC.e(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": "
                        + result.getLogMessage() + complement, result.getException());
            } else {
                Log_OC.e(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": "
                        + result.getLogMessage());
            }
        }
    }

    return result;
}