commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ozeigerm...@apache.org
Subject cvs commit: jakarta-commons/transaction/src/java/org/apache/commons/transaction/util FileHelper.java
Date Fri, 19 Nov 2004 17:42:10 GMT
ozeigermann    2004/11/19 09:42:10

  Modified:    transaction/src/java/org/apache/commons/transaction/util
                        FileHelper.java
  Log:
  Took over changes made in Slide version of this file.
  
  Revision  Changes    Path
  1.2       +86 -34    jakarta-commons/transaction/src/java/org/apache/commons/transaction/util/FileHelper.java
  
  Index: FileHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/transaction/src/java/org/apache/commons/transaction/util/FileHelper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FileHelper.java	18 Nov 2004 23:27:18 -0000	1.1
  +++ FileHelper.java	19 Nov 2004 17:42:10 -0000	1.2
  @@ -5,7 +5,7 @@
    *
    * ====================================================================
    *
  - * Copyright 1999-2002 The Apache Software Foundation 
  + * Copyright 2004 The Apache Software Foundation 
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -100,54 +100,106 @@
       }
   
       /**
  -     * Moves one directory to another. Existing files will be replaced.
  +     * Moves one directory or file to another. Existing files will be replaced.
        * 
  -     * @param sourceDir directory to move from
  -     * @param targetDir directory to move to
  +     * @param source file to move from
  +     * @param target file to move to
        * @throws IOException if an I/O error occurs (may result in partially done work) 

        */
  -    public static void moveRec(File sourceDir, File targetDir) throws IOException {
  +    public static void moveRec(File source, File target) throws IOException {
           byte[] sharedBuffer = new byte[BUF_SIZE];
  -        moveRec(sourceDir, targetDir, sharedBuffer);
  +        moveRec(source, target, sharedBuffer);
       }
   
  -    static void moveRec(File sourceDir, File targetDir, byte[] sharedBuffer) throws IOException
{
  -        if (sourceDir.isDirectory() && targetDir.isDirectory()) {
  -            File[] files = sourceDir.listFiles();
  -            for (int i = 0; i < files.length; i++) {
  -                File file = files[i];
  -                File targetFile = new File(targetDir, file.getName());
  -                if (file.isFile()) {
  -                    if (targetFile.exists()) {
  -                        targetFile.delete();
  +    static void moveRec(File source, File target, byte[] sharedBuffer) throws IOException
{
  +        if (source.isDirectory()) {
  +            if (!target.exists()) {
  +                target.mkdirs();
  +            }
  +            if (target.isDirectory()) {
  +
  +                File[] files = source.listFiles();
  +                for (int i = 0; i < files.length; i++) {
  +                    File file = files[i];
  +                    File targetFile = new File(target, file.getName());
  +                    if (file.isFile()) {
  +                        if (targetFile.exists()) {
  +                            targetFile.delete();
  +                        }
  +                        if (!file.renameTo(targetFile)) {
  +                            copy(file, targetFile, sharedBuffer);
  +                            file.delete();
  +                        }
  +                    } else {
  +                        targetFile.mkdirs();
  +                        moveRec(file, targetFile);
                       }
  -                    if (!file.renameTo(targetFile)) {
  +                }
  +                source.delete();
  +            }
  +        } else {
  +            if (!target.isDirectory()) {
  +                copy(source, target, sharedBuffer);
  +                source.delete();
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Copies one directory or file to another. Existing files will be replaced.
  +     * 
  +     * @param source directory or file to copy from
  +     * @param target directory or file to copy to
  +     * @throws IOException if an I/O error occurs (may result in partially done work) 

  +     */
  +    public static void copyRec(File source, File target) throws IOException {
  +        byte[] sharedBuffer = new byte[BUF_SIZE];
  +        copyRec(source, target, sharedBuffer);
  +    }
  +
  +    static void copyRec(File source, File target, byte[] sharedBuffer) throws IOException
{
  +        if (source.isDirectory()) {
  +            if (!target.exists()) {
  +                target.mkdirs();
  +            }
  +            if (target.isDirectory()) {
  +
  +                File[] files = source.listFiles();
  +                for (int i = 0; i < files.length; i++) {
  +                    File file = files[i];
  +                    File targetFile = new File(target, file.getName());
  +                    if (file.isFile()) {
  +                        if (targetFile.exists()) {
  +                            targetFile.delete();
  +                        }
                           copy(file, targetFile, sharedBuffer);
  -                        file.delete();
  +                    } else {
  +                        targetFile.mkdirs();
  +                        copyRec(file, targetFile);
                       }
  -                } else {
  -                    targetFile.mkdirs();
  -                    moveRec(file, targetFile);
                   }
               }
  -            sourceDir.delete();
           } else {
  -            throw new IOException(
  -                "sourceDir '"
  -                    + sourceDir.getCanonicalPath()
  -                    + "' and targetDir '"
  -                    + targetDir.getCanonicalPath()
  -                    + "' must be directories and exist to perform a move");
  +            if (!target.isDirectory()) {
  +                if (!target.exists()) {
  +                    target.getParentFile().mkdirs();
  +                    target.createNewFile();
  +                }
  +                copy(source, target, sharedBuffer);
  +            }
           }
       }
   
       /**
        * Copies one file to another using {@link #copy(InputStream, OutputStream)}.
        * 
  -     * @param input source file
  -     * @param output destination file
  +     * @param input
  +     *            source file
  +     * @param output
  +     *            destination file
        * @return the number of bytes copied
  -     * @throws IOException if an I/O error occurs (may result in partially done work) 

  +     * @throws IOException
  +     *             if an I/O error occurs (may result in partially done work)
        * @see #copy(InputStream, OutputStream)
        */
       public static long copy(File input, File output) throws IOException {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message