ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/util FileUtils.java
Date Thu, 21 Mar 2002 08:09:19 GMT
bodewig     02/03/21 00:09:19

  Modified:    src/main/org/apache/tools/ant/util FileUtils.java
  Log:
  make sure files get closed during copy.
  
  PR: 7302
  
  Revision  Changes    Path
  1.20      +66 -48    jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- FileUtils.java	18 Mar 2002 02:44:30 -0000	1.19
  +++ FileUtils.java	21 Mar 2002 08:09:19 -0000	1.20
  @@ -95,7 +95,7 @@
    * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
    * @author <a href="mailto:jtulley@novell.com">Jeff Tulley</a> 
    *
  - * @version $Revision: 1.19 $
  + * @version $Revision: 1.20 $
    */
   
   public class FileUtils {
  @@ -338,59 +338,77 @@
                   BufferedReader in = null;
                   BufferedWriter out = null;
   
  -                if (encoding == null) {
  -                    in = new BufferedReader(new FileReader(sourceFile));
  -                    out = new BufferedWriter(new FileWriter(destFile));
  -                } else {
  -                    in = new BufferedReader(new InputStreamReader(
  -                            new FileInputStream(sourceFile), encoding));
  -                    out = new BufferedWriter(new OutputStreamWriter(
  -                            new FileOutputStream(destFile), encoding));
  -                }
  -
  -                if (filterChainsAvailable) {
  -                    ChainReaderHelper crh = new ChainReaderHelper();
  -                    crh.setBufferSize(8192);
  -                    crh.setPrimaryReader(in);
  -                    crh.setFilterChains(filterChains);
  -                    crh.setProject(project);
  -                    Reader rdr = crh.getAssembledReader();
  -                    in = new BufferedReader(rdr);
  -                }
  -
  -                int length;
  -                String newline = null;
  -                String line = in.readLine();
  -                while (line != null) {
  -                    if (line.length() == 0) {
  -                        out.newLine();
  +                try {
  +                    if (encoding == null) {
  +                        in = new BufferedReader(new FileReader(sourceFile));
  +                        out = new BufferedWriter(new FileWriter(destFile));
                       } else {
  -                        if (filterSetsAvailable) {
  -                            newline = filters.replaceTokens(line);
  +                        in = 
  +                            new BufferedReader(new InputStreamReader(
  +                                                 new FileInputStream(sourceFile), 
  +                                                 encoding));
  +                        out = 
  +                            new BufferedWriter(new OutputStreamWriter(
  +                                                 new FileOutputStream(destFile), 
  +                                                 encoding));
  +                    }
  +
  +                    if (filterChainsAvailable) {
  +                        ChainReaderHelper crh = new ChainReaderHelper();
  +                        crh.setBufferSize(8192);
  +                        crh.setPrimaryReader(in);
  +                        crh.setFilterChains(filterChains);
  +                        crh.setProject(project);
  +                        Reader rdr = crh.getAssembledReader();
  +                        in = new BufferedReader(rdr);
  +                    }
  +                    
  +                    int length;
  +                    String newline = null;
  +                    String line = in.readLine();
  +                    while (line != null) {
  +                        if (line.length() == 0) {
  +                            out.newLine();
                           } else {
  -                            newline = line;
  +                            if (filterSetsAvailable) {
  +                                newline = filters.replaceTokens(line);
  +                            } else {
  +                                newline = line;
  +                            }
  +                            out.write(newline);
  +                            out.newLine();
                           }
  -                        out.write(newline);
  -                        out.newLine();
  +                        line = in.readLine();
  +                    }
  +                } finally {
  +                    if (out != null) {
  +                        out.close();
  +                    }
  +                    if (in != null) {
  +                        in.close();
                       }
  -                    line = in.readLine();
                   }
  -
  -                out.close();
  -                in.close();
               } else {
  -                FileInputStream in = new FileInputStream(sourceFile);
  -                FileOutputStream out = new FileOutputStream(destFile);
  -
  -                byte[] buffer = new byte[8 * 1024];
  -                int count = 0;
  -                do {
  -                    out.write(buffer, 0, count);
  -                    count = in.read(buffer, 0, buffer.length);
  -                } while (count != -1);
  -
  -                in.close();
  -                out.close();
  +                FileInputStream in = null;
  +                FileOutputStream out = null;
  +                try {
  +                    in = new FileInputStream(sourceFile);
  +                    out = new FileOutputStream(destFile);
  +
  +                    byte[] buffer = new byte[8 * 1024];
  +                    int count = 0;
  +                    do {
  +                        out.write(buffer, 0, count);
  +                        count = in.read(buffer, 0, buffer.length);
  +                    } while (count != -1);
  +                } finally {
  +                    if (out != null) {
  +                        out.close();
  +                    }
  +                    if (in != null) {
  +                        in.close();
  +                    }
  +                }
               }
   
               if (preserveLastModified) {
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message