protected void copyFile()

in commons-transfer/commons-transfer-file/src/main/java/org/apache/archiva/commons/transfer/file/FileTransferBase.java [92:159]


    protected void copyFile( URI uri, File srcFile, File destFile )
        throws IOException
    {
        // Sanity Check.
        if ( srcFile == null )
        {
            throw new NullPointerException( "Source File must not be null" );
        }
        if ( destFile == null )
        {
            throw new NullPointerException( "Destination File must not be null" );
        }

        // Source should exist, as a file.
        if ( srcFile.exists() == false )
        {
            throw new FileNotFoundException( "Source '" + srcFile + "' does not exist" );
        }
        if ( srcFile.isFile() == false )
        {
            throw new IOException( "Source '" + srcFile + "' exists but is not a file (directory?)" );
        }

        // Are they the same file?
        if ( srcFile.getCanonicalPath().equals( destFile.getCanonicalPath() ) )
        {
            throw new IOException( "Source '" + srcFile + "' and destination '" + destFile + "' are the same" );
        }

        // Create the directories on the destination side (if needed)
        if ( ( destFile.getParentFile() != null ) && ( destFile.getParentFile().exists() == false ) )
        {
            if ( destFile.getParentFile().mkdirs() == false )
            {
                throw new IOException( "Destination '" + destFile + "' directory cannot be created" );
            }
        }

        // Can we write to the destination ?
        if ( destFile.exists() && ( destFile.canWrite() == false ) )
        {
            throw new IOException( "Destination '" + destFile + "' exists but is read-only" );
        }

        // Perform the copy
        FileInputStream input = null;
        FileOutputStream output = null;
        try
        {
            input = new FileInputStream( srcFile );
            output = new FileOutputStream( destFile );

            transfer( uri, input, output, srcFile.length() );
        }
        finally
        {
            IOUtils.closeQuietly( output );
            IOUtils.closeQuietly( input );
        }

        if ( srcFile.length() != destFile.length() )
        {
            throw new IOException( "Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'" );
        }

        // Preserve the file date/time
        destFile.setLastModified( srcFile.lastModified() );
    }