You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by tw...@apache.org on 2008/08/13 11:54:41 UTC

svn commit: r685496 - in /incubator/uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/util/FileUtils.java test/java/org/apache/uima/util/FileUtilsTest.java

Author: twgoetz
Date: Wed Aug 13 02:54:38 2008
New Revision: 685496

URL: http://svn.apache.org/viewvc?rev=685496&view=rev
Log:
Jira UIMA-1142: add try/finally to FileUtils.saveString2File(), refactor, add test case.

https://issues.apache.org/jira/browse/UIMA-1142

Modified:
    incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java
    incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/FileUtilsTest.java

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java?rev=685496&r1=685495&r2=685496&view=diff
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/FileUtils.java Wed Aug 13 02:54:38 2008
@@ -24,11 +24,11 @@
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileReader;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Random;
 
@@ -161,7 +161,7 @@
   }
 
   /**
-   * Write a string to a file. If the file exists, it is overwritten.
+   * Write a string to a file, using platform encoding. If the file exists, it is overwritten.
    * 
    * @param fileContents
    *          The file contents.
@@ -171,13 +171,11 @@
    *           If for any reason the file can't be written.
    */
   public static void saveString2File(String fileContents, File file) throws IOException {
-    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
-    writer.write(fileContents);
-    writer.close();
+    saveString2File(fileContents, file, Charset.defaultCharset().name());
   }
 
   /**
-   * Write a string to a file. If the file exists, it is overwritten.
+   * Write a string to a file using the specified encoding. If the file exists, it is overwritten.
    * 
    * @param s
    *          The file contents.
@@ -189,10 +187,16 @@
    *           If for any reason the file can't be written.
    */
   public static void saveString2File(String s, File file, String encoding) throws IOException {
-    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),
-        encoding));
-    writer.write(s);
-    writer.close();
+    BufferedWriter writer = null;
+    try {
+      writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
+      writer.write(s);
+      writer.close();
+    } finally {
+      if (writer != null) {
+        writer.close();
+      }
+    }
   }
 
   /**
@@ -289,9 +293,9 @@
   }
 
   /**
-   * Copy file <code>file</code> to location <code>dir</code>. This method will not fail
-   * silently. Anything that prevents the copy from happening will be treated as an error condition.
-   * If the method does not throw an exception, the copy was successful.
+   * Copy file <code>file</code> to location <code>dir</code>. This method will not fail silently.
+   * Anything that prevents the copy from happening will be treated as an error condition. If the
+   * method does not throw an exception, the copy was successful.
    * 
    * <p>
    * <b>Note: </b> this is a completely brain-dead implementation of file copy. The complete file is
@@ -328,23 +332,29 @@
 
       while (true) {
         int count = is.read(bytes);
-        if (0 > count)
+        if (0 > count) {
           break;
+        }
         os.write(bytes, 0, count);
       }
     } finally {
-      if (null != is)
+      if (null != is) {
         is.close();
-      if (null != os)
+      }
+      if (null != os) {
         os.close();
+      }
     }
   }
-  
+
   /**
    * Finds a relative path to a given file, relative to a specified directory.
-   * @param file file that the relative path should resolve to
-   * @param relativeToDir directory that the path should be relative to
-   * @return a relative path.  This always uses / as the separator character.
+   * 
+   * @param file
+   *          file that the relative path should resolve to
+   * @param relativeToDir
+   *          directory that the path should be relative to
+   * @return a relative path. This always uses / as the separator character.
    */
   public static String findRelativePath(File file, File relativeToDir) throws IOException {
     String canonicalFile = file.getCanonicalPath();
@@ -352,8 +362,8 @@
     String[] filePathComponents = getPathComponents(canonicalFile);
     String[] relToPathComponents = getPathComponents(canonicalRelTo);
     int i = 0;
-    while(i < filePathComponents.length && i < relToPathComponents.length &&
-          filePathComponents[i].equals(relToPathComponents[i])) {
+    while (i < filePathComponents.length && i < relToPathComponents.length
+        && filePathComponents[i].equals(relToPathComponents[i])) {
       i++;
     }
     StringBuffer buf = new StringBuffer();
@@ -363,16 +373,16 @@
     for (int j = i; j < filePathComponents.length - 1; j++) {
       buf.append(filePathComponents[j]).append('/');
     }
-    buf.append(filePathComponents[filePathComponents.length-1]);
+    buf.append(filePathComponents[filePathComponents.length - 1]);
     return buf.toString();
   }
-      
 
-    
   /**
-   * Splits a path into components using the OS file separator character.
-   * This can be used on the results of File.getCanonicalPath().
-   * @param canonicalPath a file path that uses the OS file separator character
+   * Splits a path into components using the OS file separator character. This can be used on the
+   * results of File.getCanonicalPath().
+   * 
+   * @param canonicalPath
+   *          a file path that uses the OS file separator character
    * @return an array of strings, one for each component of the path
    */
   public static String[] getPathComponents(String canonicalPath) {
@@ -380,6 +390,6 @@
     if (regex.equals("\\"))
       regex = "\\\\";
     return canonicalPath.split(regex);
-  }  
+  }
 
 }

Modified: incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/FileUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/FileUtilsTest.java?rev=685496&r1=685495&r2=685496&view=diff
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/FileUtilsTest.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/FileUtilsTest.java Wed Aug 13 02:54:38 2008
@@ -19,6 +19,7 @@
 package org.apache.uima.util;
 
 import java.io.File;
+import java.io.IOException;
 
 import junit.framework.TestCase;
 
@@ -30,17 +31,31 @@
     File target = new File("/this/is/a/file.txt");
     File base = new File("/this/is/a/test");
     assertEquals("../file.txt", FileUtils.findRelativePath(target, base));
-    
+
     base = new File("c:/foo/bar/baz/dir/");
     target = new File("c:/foo/d1/d2/d3/blah.xml");
-    assertEquals("../../../d1/d2/d3/blah.xml", 
-            FileUtils.findRelativePath(target, base));
+    assertEquals("../../../d1/d2/d3/blah.xml", FileUtils.findRelativePath(target, base));
 
     if (File.separatorChar == '\\') {
       base = new File("c:\\foo\\bar\\baz\\dir\\");
       target = new File("c:\\foo\\d1\\d2\\d3\\blah.xml");
-      assertEquals("../../../d1/d2/d3/blah.xml", 
-              FileUtils.findRelativePath(target, base));
+      assertEquals("../../../d1/d2/d3/blah.xml", FileUtils.findRelativePath(target, base));
     }
   }
+
+  public void testReadWriteTempFile() throws IOException {
+    final String tmpDirPath = System.getProperty("java.io.tmpdir");
+    assertNotNull("java.io.tmpdir system property not available", tmpDirPath);
+    File tmpDir = FileUtils.createTempDir(new File(tmpDirPath), "fileUtilsTest");
+    File tmpFile1 = FileUtils.createTempFile("test", null, tmpDir);
+    File tmpFile2 = FileUtils.createTempFile("test", null, tmpDir);
+    final String text = "This is some text to test file writing.  Add an Umlaut for encoding tests:"
+        + "\n  Greetings from T\u00FCbingen!\n";
+    final String utf8 = "UTF-8";
+    FileUtils.saveString2File(text, tmpFile1);
+    FileUtils.saveString2File(text, tmpFile2, utf8);
+    assertEquals(text, FileUtils.file2String(tmpFile1));
+    assertEquals(text, FileUtils.file2String(tmpFile2, utf8));
+    FileUtils.deleteRecursive(tmpDir);
+  }
 }